From b2ca3098a4d06467de7d8133aa0ecad9417f88e8 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Sun, 22 Nov 2015 12:52:00 +0000 Subject: [PATCH] fpspreadsheet: Reintroduce function GetFormatFromFileName. Fixes compilation error of LazReport addition lrSpreadsheetExport. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4395 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../fpspreadsheet/fpsregfileformats.pas | 9 ++-- components/fpspreadsheet/fpsutils.pas | 41 +++++++++++++++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/components/fpspreadsheet/fpsregfileformats.pas b/components/fpspreadsheet/fpsregfileformats.pas index 051e6c374..d4eb6a257 100644 --- a/components/fpspreadsheet/fpsregfileformats.pas +++ b/components/fpspreadsheet/fpsregfileformats.pas @@ -458,7 +458,7 @@ end; AFormat identifies the file format, see sfXXXX declarations in built-in fpstypes. - The system is open for user-defined formats. In this case, AFormat must have + The system is open to user-defined formats. In this case, AFormat must have the value "sfUser". The format identifier is calculated as a negative number, stored in the TsSpreadFormatData class and returned as function result. This value is needed when calling fpspreadsheet's ReadFromXXXX and WriteToXXXX @@ -484,10 +484,13 @@ begin raise Exception.Create('[RegisterSpreadFormat] File format name is not specified.'); fmt := TsSpreadFormatData.Create(ord(AFormat), AReaderClass, AWriterClass, - AFormatName, ATechnicalName, AFileExtensions); //, ACanReadFromClipboard, ACanWriteToClipboard); + AFormatName, ATechnicalName, AFileExtensions); n := SpreadFormatRegistry.Add(fmt); - if AFormat = sfUser then + if (AFormat = sfUser) then + begin + if (n <= ord(sfUser)) then n := n + ord(sfUser) + 1; fmt.FFormatID := -n; + end; Result := fmt.FormatID; end; diff --git a/components/fpspreadsheet/fpsutils.pas b/components/fpspreadsheet/fpsutils.pas index 6e4ebfae1..e3b8f6bd5 100644 --- a/components/fpspreadsheet/fpsutils.pas +++ b/components/fpspreadsheet/fpsutils.pas @@ -105,8 +105,10 @@ function TryStrToErrorValue(AErrorStr: String; out AErr: TsErrorValue): boolean; //function GetFileFormatName(AFormat: TsSpreadsheetFormat): string; //function GetFileFormatExt(AFormat: TsSpreadsheetFormat): String; -//function GetFormatFromFileName(const AFileName: TFileName; -// out SheetType: TsSpreadsheetFormat): Boolean; +function GetFormatFromFileName(const AFileName: TFileName; + out AFormatID: TsSpreadFormatID): Boolean; overload; +function GetFormatFromFileName(const AFileName: TFileName; + out SheetType: TsSpreadsheetFormat): Boolean; overload; deprecated 'Use overloaded function with TsSpreadsheetID'; function IfThen(ACondition: Boolean; AValue1,AValue2: TsNumberFormat): TsNumberFormat; overload; @@ -191,7 +193,7 @@ var implementation uses - Math, lazutf8, lazfileutils, fpsStrings; + Math, lazutf8, lazfileutils, fpsStrings, fpsRegFileFormats; {******************************************************************************} { Endianess helper functions } @@ -1087,19 +1089,43 @@ begin else raise Exception.Create(rsUnknownSpreadsheetFormat); end; end; + *) +{@@ ---------------------------------------------------------------------------- + Determines the spreadsheet type from the file type extension + + @param AFileName Name of the file to be considered + @param AFormatID File format ID found from analysis of the extension (output) + @return True if the file matches any of the registered formats, false otherwise +-------------------------------------------------------------------------------} +function GetFormatFromFileName(const AFileName: TFileName; + out AFormatID: TsSpreadFormatID): Boolean; +var + suffix: String; + fileformats: TsSpreadFormatIDArray; +begin + fileFormats := GetSpreadFormatsFromFileName(faRead, AFileName, ord(sfExcel8)); + Result := (Length(fileFormats) > 0) and (fileFormats[0] <= sfidUnknown); + if Result then AFormatID := fileFormats[0]; +end; {@@ ---------------------------------------------------------------------------- Determines the spreadsheet type from the file type extension @param AFileName Name of the file to be considered - @param SheetType File format found from analysis of the extension (output) - @return True if the file matches any of the known formats, false otherwise + @param SheetType Built-in file format found from analysis of the extension + (output) + @return True if the file matches any of the built-in formats, false otherwise -------------------------------------------------------------------------------} function GetFormatFromFileName(const AFileName: TFileName; out SheetType: TsSpreadsheetFormat): Boolean; var - suffix: String; + fmtID: TsSpreadFormatID; begin + Result := GetFormatFromFileName(AFileName, fmtID); + if Result and (fmtID < 0) then Result := false; +end; + +{ Result := true; suffix := Lowercase(ExtractFileExt(AFileName)); case suffix of @@ -1113,7 +1139,8 @@ begin else Result := False; end; end; - *) + } + {@@ ---------------------------------------------------------------------------- Helper function to reduce typing: "if a conditions is true return the first number format, otherwise return the second format"