Recent

Author Topic: Get processd bytes in TCSVParser  (Read 2150 times)

Maris

  • New Member
  • *
  • Posts: 18
Get processd bytes in TCSVParser
« on: February 12, 2015, 06:39:00 pm »
Hello!

I couldn't find any way how to find out currently processed bytes while using TCSVParser from CSVDocument component.

Imagine such code:

Code: [Select]
  Parser:=TCSVParser.Create;
  FileStream := TFileStream.Create(FileName, fmOpenRead + fmShareDenyWrite);
  ProgressBarLoadCSV.Max := FileStream.Size; {Progress bar is based on file size}
  StrSize := 0;
  try
    Parser.Delimiter := ',';
    Parser.SetSource(FileStream);
    while Parser.ParseNextCell do
    begin
      ... {Checking if additional rows, columns should be added}
      ListGrid.Cells[Parser.CurrentCol, Parser.CurrentRow] := Parser.CurrentCellText;
      StrSize := StrSize + CurrentlyProcessedBytes; {Is it possible to get currently processed bytes}
      ProgressBarLoadCSV.Position := StrSize;
    end;
  finally
    Parser.Free;
    FileStream.Free;
  end;

Problem is about variable "CurrentlyProcessedBytes". Idea is to make progress bar which is based on csv file size and currently processed bytes by TCSVParser. I couldn't find any way to find this value from TCSVParser. Most of all there is no such thing in this component.

I'm just curious is there any way to make workaround or something else?

Getting String size from TCSVParser.CurrentCellText is not a solution, because this content does not contain separators, quotes, new lines and similar symbols (bytes) which are not part of readable text.

Making progress bar based on row and column count is not a solution either, because this count is unknown before csv is parsed.

Any ideas would be greatly appreciated!

Thanks!

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Get processd bytes in TCSVParser
« Reply #1 on: February 12, 2015, 08:15:45 pm »
Yes, TCSVParser was not designed for extensibility - it does not introduce any virtual or protected methods at all. I think you just have to parse twice. Try something like this:

Code: [Select]
procedure TForm1.LoadCSVProgress(const aFilename: TFilename; aPBar: TProgressBar);
var
  parser: TCSVParser;
  fs: TFileStream;
  sl: TStringList;
  rowCount: integer;
begin
  sl:=TStringList.Create;
  try
    sl.LoadFromFile(aFilename);
    rowCount:=sl.Count-1;
  finally
    sl.Free;
  end;
  Parser:=TCSVParser.Create;
  Parser.Delimiter := ',';
  try
  fs := TFileStream.Create(aFileName, fmOpenRead + fmShareDenyWrite);
    try
      aPBar.Max:=rowCount;
      Parser.SetSource(fs);
      while Parser.ParseNextCell do
        aPBar.Position:=parser.CurrentRow;
    finally
      fs.Free;
    end;
  finally
    Parser.Free;
  end;
end;

 

TinyPortal © 2005-2018