fpspreadsheet: Avoid format detection pick csv reader when fpsAllFormats is used and the provided xlsx file contains an error.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@6820 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz 2019-02-27 12:13:19 +00:00
parent 8e86543dde
commit 17d4e8186a
3 changed files with 25 additions and 25 deletions

View File

@ -364,7 +364,7 @@ end;
class function TsCustomSpreadReader.CheckFileFormat(AStream: TStream): boolean;
begin
Unused(AStream);
Result := true;
Result := not HasZipHeader(AStream);
end;
{@@ ----------------------------------------------------------------------------

View File

@ -212,6 +212,7 @@ procedure InitHeaderFooterImageRecord(out AImage: TsHeaderFooterImage);
//procedure CopyCellValue(AFromCell, AToCell: PCell);
function HasFormula(ACell: PCell): Boolean;
function Has3dFormula(ACell: PCell): Boolean;
function HasZipHeader(AStream: TStream): Boolean;
function SameCellBorders(AFormat1, AFormat2: PsCellFormat): Boolean;
function SameFont(AFont1, AFont2: TsFont): Boolean; overload;
function SameFont(AFont: TsFont; AFontName: String; AFontSize: Single;
@ -2552,6 +2553,29 @@ begin
end;
{@@ ----------------------------------------------------------------------------
Returns true if the file begins with a ZIP header *PK'#03#04.
Needed for file format detection.
-------------------------------------------------------------------------------}
function HasZipHeader(AStream: TStream): Boolean;
const
ZIP_HEADER: packed array[0..3] of char = ('P', 'K', #03, #04);
var
P: Int64;
buf: packed array[0..3] of char = (#0, #0, #0, #0);
begin
Result := false;
P := AStream.Position;
try
AStream.Position := 0;
if AStream.Read(buf, 4) < 4 then
exit;
Result := CompareMem(@buf[0], @ZIP_HEADER[0], 4);
finally
AStream.Position := P;
end;
end;
{-------------------------------------------------------------------------------
Checks whether two format records have same border attributes
@param AFormat1 Pointer to the first one of the two format records to be compared

View File

@ -57,8 +57,6 @@ function CreateTempStream(AWorkbook: TsBasicWorkbook;
AFileNameBase: String): TStream;
procedure DestroyTempStream(AStream: TStream);
function HasZipHeader(AStream: TStream): Boolean;
implementation
@ -394,7 +392,6 @@ begin
Result := TMemoryStream.Create;
end;
procedure DestroyTempStream(AStream: TStream);
var
fn: String;
@ -412,26 +409,5 @@ begin
end;
{ Returns true if the file begins with a ZIP header *PK'. Needed for
file format detection. }
function HasZipHeader(AStream: TStream): Boolean;
const
ZIP_HEADER: packed array[0..1] of char = ('P', 'K');
var
P: Int64;
buf: packed array[0..1] of char = (#0, #0);
begin
Result := false;
P := AStream.Position;
try
AStream.Position := 0;
if AStream.Read(buf, 2) < 2 then
exit;
Result := (buf[0] = ZIP_HEADER[0]) and (buf[1] = ZIP_HEADER[1]);
finally
AStream.Position := P;
end;
end;
end.