From 95a19394a4a584eb6e71d4047a07b16f16416d37 Mon Sep 17 00:00:00 2001 From: Graeme Geldenhuys Date: Sun, 22 Aug 2010 13:48:20 +0200 Subject: Introduced a new cross-platform fpgFileSize() helper function. --- src/corelib/fpg_utils.pas | 1 + src/corelib/gdi/fpg_utils_impl.inc | 21 ++++++++++++++++++++- src/corelib/x11/fpg_utils_impl.inc | 10 +++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/corelib/fpg_utils.pas b/src/corelib/fpg_utils.pas index d4d7886c..43f73028 100644 --- a/src/corelib/fpg_utils.pas +++ b/src/corelib/fpg_utils.pas @@ -31,6 +31,7 @@ uses function fpgToOSEncoding(aString: TfpgString): string; function fpgFromOSEncoding(aString: string): TfpgString; procedure fpgOpenURL(const aURL: TfpgString); +function fpgFileSize(const AFilename: TfpgString): integer; // *** Common functions for all platforms *** diff --git a/src/corelib/gdi/fpg_utils_impl.inc b/src/corelib/gdi/fpg_utils_impl.inc index d3bb2f0c..08a3c3ad 100644 --- a/src/corelib/gdi/fpg_utils_impl.inc +++ b/src/corelib/gdi/fpg_utils_impl.inc @@ -1,7 +1,7 @@ {%mainunit fpg_utils.pas} uses - Shellapi; + Shellapi, Windows; // GDI specific implementations of encoding functions @@ -26,3 +26,22 @@ begin end; end; +function fpgFileSize(const AFilename: TfpgString): integer; +var + FindData: TWIN32FindDataW; + FindHandle: THandle; + Str: widestring; +begin + // Don't assign the widestring to TSearchRec.name because it is of type + // string, which will generate a conversion to the system encoding + Str := UTF8Decode(Filename); + FindHandle:=Windows.FindFirstFileW(PWideChar(Str), FindData); + if FindHandle=Windows.Invalid_Handle_value then + begin + Result:=-1; + exit; + end; + Result := (int64(FindData.nFileSizeHigh) shl 32)+FindData.nFileSizeLow; + Windows.FindClose(FindHandle); +end; + diff --git a/src/corelib/x11/fpg_utils_impl.inc b/src/corelib/x11/fpg_utils_impl.inc index d8625b8c..908f411a 100644 --- a/src/corelib/x11/fpg_utils_impl.inc +++ b/src/corelib/x11/fpg_utils_impl.inc @@ -1,7 +1,7 @@ {%mainunit fpg_utils.pas} uses - Unix; + Unix, BaseUnix; // X11 specific filesystem implementations of encoding functions @@ -40,3 +40,11 @@ begin fpSystem(Helper + ' ' + aURL + '&'); end; +function fpgFileSize(const AFilename: TfpgString): integer; +var + st: baseunix.stat; +begin + if not fpstat(pointer(AFilename),st) >= 0 then + exit(-1); + Result := st.st_size; +end; -- cgit v1.2.3-70-g09d2