From 75e84ece09ce9440d42032c4e67752d2f6e89e93 Mon Sep 17 00:00:00 2001 From: Stas Degteff Date: Thu, 17 Feb 2011 19:51:28 +0000 Subject: [PATCH] Add C++ version of the function GetFilesize() --- goldlib/gall/gfilutil.h | 8 ++++++-- goldlib/gall/gfilutl2.cpp | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/goldlib/gall/gfilutil.h b/goldlib/gall/gfilutil.h index ee82e59..2518f37 100644 --- a/goldlib/gall/gfilutil.h +++ b/goldlib/gall/gfilutil.h @@ -179,10 +179,14 @@ inline time32_t GetFiletime(const std::string& file) { return GetFiletime(file.c inline long FiletimeCmp(const char* file1, const char* file2) { return long(GetFiletime(file1) - GetFiletime(file2)); } inline long FiletimeCmp(const std::string& file1, const std::string& file2) { return FiletimeCmp(file1.c_str(), file2.c_str()); } -// Get size of open file +// Get size of open file handle long fsize(FILE* fp); -// Get size of file + +// Get size of disk file. Return values: +// positive number or zero - size of file, +// negative number - error (and error code is stored in errno) long GetFilesize(const char* file); +long GetFilesize(const std::string& filename); // Add path to filename if no path is present. Uses static string and don't chech size of 'path', be careful! const char* AddPath(const char* path, const char* file); diff --git a/goldlib/gall/gfilutl2.cpp b/goldlib/gall/gfilutl2.cpp index 526f728..60e6526 100644 --- a/goldlib/gall/gfilutl2.cpp +++ b/goldlib/gall/gfilutl2.cpp @@ -62,6 +62,24 @@ std::string& StripBackslash(std::string& p) { return p; } +// ------------------------------------------------------------------ +// Get size of file. Return values: +// positive number or zero - size of file, +// negative number - error (and error code is stored in errno) + +long GetFilesize(const std::string& filename) { + + if(filename.empty()) { + errno = EINVAL; + return -1; + } + + struct stat info; + if(stat(filename.c_str(), &info) == 0) + return info.st_size; + return -1; +} + // ------------------------------------------------------------------ // Add path to filename, if no path is set. Don't chech size of 'path', be careful!