CSV scanner: fix crash when selected filename cannot be opened

This commit is contained in:
Ansgar Becker
2023-10-11 06:42:43 +02:00
parent 62f27dbf0a
commit fa571f72da

View File

@ -125,6 +125,7 @@ begin
Screen.Cursor := crHourGlass; Screen.Cursor := crHourGlass;
btnScan.ImageIndex := 150; btnScan.ImageIndex := 150;
btnScan.Enabled := False; btnScan.Enabled := False;
btnSave.Enabled := False;
// Parse contents to a TGridRows instance // Parse contents to a TGridRows instance
GridRows := TGridRows.Create(True); GridRows := TGridRows.Create(True);
GridRow := nil; GridRow := nil;
@ -144,57 +145,62 @@ begin
InEncl := False; InEncl := False;
MainForm.ShowStatusMsg(f_('Reading textfile (%s) ...', [FormatByteNumber(TestChunkSize)])); try
Encoding := FLoadDataFrm.FileEncoding; MainForm.ShowStatusMsg(f_('Reading textfile (%s) ...', [FormatByteNumber(TestChunkSize)]));
OpenTextfile(FLoadDataFrm.editFilename.Text, Stream, Encoding); Encoding := FLoadDataFrm.FileEncoding;
Contents := ReadTextfileChunk(Stream, Encoding, TestChunkSize); OpenTextfile(FLoadDataFrm.editFilename.Text, Stream, Encoding);
Stream.Free; Contents := ReadTextfileChunk(Stream, Encoding, TestChunkSize);
ContentLen := Length(Contents); Stream.Free;
MainForm.ShowStatusMsg; ContentLen := Length(Contents);
MainForm.ShowStatusMsg;
P := 0; P := 0;
ProgressCharsPerStep := ContentLen div FLoadDataFrm.ProgressBarSteps; ProgressCharsPerStep := ContentLen div FLoadDataFrm.ProgressBarSteps;
ProgressChars := 0; ProgressChars := 0;
MainForm.EnableProgress(FLoadDataFrm.ProgressBarSteps); MainForm.EnableProgress(FLoadDataFrm.ProgressBarSteps);
IgnoreLines := FLoadDataFrm.updownIgnoreLines.Position; IgnoreLines := FLoadDataFrm.updownIgnoreLines.Position;
NextChar; NextChar;
while P <= ContentLen do begin while P <= ContentLen do begin
// Check characters left-side from current position // Check characters left-side from current position
IsEncl := TestLeftChars(EnclTest, Encl, EnclLen); IsEncl := TestLeftChars(EnclTest, Encl, EnclLen);
IsTerm := TestLeftChars(TermTest, Term, TermLen); IsTerm := TestLeftChars(TermTest, Term, TermLen);
IsLineTerm := TestLeftChars(LineTermTest, LineTerm, LineTermLen); IsLineTerm := TestLeftChars(LineTermTest, LineTerm, LineTermLen);
IsEof := P = ContentLen; IsEof := P = ContentLen;
Value := Value + Contents[P]; Value := Value + Contents[P];
if IsEncl then if IsEncl then
InEncl := not InEncl; InEncl := not InEncl;
if IsEof or (not InEncl) then begin if IsEof or (not InEncl) then begin
if IsLineTerm then begin if IsLineTerm then begin
SetLength(Value, Length(Value)-LineTermLen); SetLength(Value, Length(Value)-LineTermLen);
AddValue; AddValue;
end else if IsEof then begin end else if IsEof then begin
AddValue; AddValue;
end else if IsTerm then begin end else if IsTerm then begin
SetLength(Value, Length(Value)-TermLen); SetLength(Value, Length(Value)-TermLen);
AddValue; AddValue;
end;
end; end;
if IsLineTerm and (not InEncl) then
AddRow;
NextChar;
end; end;
if IsLineTerm and (not InEncl) then Contents := '';
AddRow;
NextChar; // Find matching column types for values
Columns := DetectColumnAttributes(GridRows, IgnoreLines);
SynMemoCreateTable.Text := ComposeCreateStatement(Columns);
btnSave.Enabled := True;
except
on E:EFOpenError do
ErrorDialog(E.Message);
end; end;
Contents := '';
// Find matching column types for values
Columns := DetectColumnAttributes(GridRows, IgnoreLines);
SynMemoCreateTable.Text := ComposeCreateStatement(Columns);
GridRows.Free; GridRows.Free;
MainForm.ShowStatusMsg; MainForm.ShowStatusMsg;