From 65a1158e40b30d8c06d6398bc142c6ae9e783006 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 10 Apr 2015 21:40:45 +0200 Subject: Added proper implementation of cFile::ChangeFileExt(). --- src/OSSupport/File.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'src/OSSupport/File.cpp') diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp index 7ad1b3f81..fcd5ec027 100644 --- a/src/OSSupport/File.cpp +++ b/src/OSSupport/File.cpp @@ -456,10 +456,25 @@ AString cFile::ReadWholeFile(const AString & a_FileName) AString cFile::ChangeFileExt(const AString & a_FileName, const AString & a_NewExt) { auto res = a_FileName; + + // If the path separator is the last character of the string, return the string unmodified (refers to a folder): + #ifdef _WIN32 + auto LastPathSep = res.find_last_of("/\\"); // Find either path separator - Windows accepts slashes as separators, too + #else + auto LastPathSep = res.rfind('/'); + #endif + if ((LastPathSep != AString::npos) && (LastPathSep + 1 == res.size())) + { + return res; + } + auto DotPos = res.rfind('.'); - if (DotPos == AString::npos) + if ( + (DotPos == AString::npos) || // No dot found + ((LastPathSep != AString::npos) && (LastPathSep > DotPos)) // Last dot is before the last path separator (-> in folder name) + ) { - // No extension, just append it: + // No extension, just append the new one: res.push_back('.'); res.append(a_NewExt); } -- cgit v1.2.3 From c4842cb9aa81634799dce0a5cfa76654b9f17ab4 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 11 Apr 2015 10:06:08 +0200 Subject: Added more cFile API functions. GetLastModificationTime, GetPathSeparator() and GetExecutableExt() --- src/OSSupport/File.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) (limited to 'src/OSSupport/File.cpp') diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp index fcd5ec027..fe72f34b4 100644 --- a/src/OSSupport/File.cpp +++ b/src/OSSupport/File.cpp @@ -458,9 +458,14 @@ AString cFile::ChangeFileExt(const AString & a_FileName, const AString & a_NewEx auto res = a_FileName; // If the path separator is the last character of the string, return the string unmodified (refers to a folder): - #ifdef _WIN32 - auto LastPathSep = res.find_last_of("/\\"); // Find either path separator - Windows accepts slashes as separators, too + #if defined(_MSC_VER) + // Find either path separator - MSVC CRT accepts slashes as separators, too + auto LastPathSep = res.find_last_of("/\\"); + #elif defined(_WIN32) + // Windows with different CRTs support only the backslash separator + auto LastPathSep = res.rfind('\\'); #else + // Linux supports only the slash separator auto LastPathSep = res.rfind('/'); #endif if ((LastPathSep != AString::npos) && (LastPathSep + 1 == res.size())) @@ -468,6 +473,7 @@ AString cFile::ChangeFileExt(const AString & a_FileName, const AString & a_NewEx return res; } + // Append or replace the extension: auto DotPos = res.rfind('.'); if ( (DotPos == AString::npos) || // No dot found @@ -491,6 +497,52 @@ AString cFile::ChangeFileExt(const AString & a_FileName, const AString & a_NewEx +unsigned cFile::GetLastModificationTime(const AString & a_FileName) +{ + struct stat st; + if (stat(a_FileName.c_str(), &st) < 0) + { + return 0; + } + #ifdef _WIN32 + // Windows returns times in local time already + return static_cast(st.st_mtime); + #else + // Linux returns UTC time, convert to local timezone: + return static_cast(mktime(localtime(&st.st_mtime))); + #endif +} + + + + + +AString cFile::GetPathSeparator(void) +{ + #ifdef _WIN32 + return "\\"; + #else + return "/"; + #endif +} + + + + + +AString cFile::GetExecutableExt(void) +{ + #ifdef _WIN32 + return ".exe"; + #else + return ""; + #endif +} + + + + + int cFile::Printf(const char * a_Fmt, ...) { AString buf; -- cgit v1.2.3 From 79e8f8fb20f9c2a4aebd6178a99b40f4e7b4fccc Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 11 Apr 2015 17:42:32 +0200 Subject: cFile:ChangeFileExt now accepts extensions with leading dot, too. --- src/OSSupport/File.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/OSSupport/File.cpp') diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp index fe72f34b4..43105b230 100644 --- a/src/OSSupport/File.cpp +++ b/src/OSSupport/File.cpp @@ -481,13 +481,25 @@ AString cFile::ChangeFileExt(const AString & a_FileName, const AString & a_NewEx ) { // No extension, just append the new one: - res.push_back('.'); + if (!a_NewExt.empty() && (a_NewExt[0] != '.')) + { + // a_NewExt doesn't start with a dot, insert one: + res.push_back('.'); + } res.append(a_NewExt); } else { // Replace existing extension: - res.erase(DotPos + 1, AString::npos); + if (!a_NewExt.empty() && (a_NewExt[0] != '.')) + { + // a_NewExt doesn't start with a dot, keep the current one: + res.erase(DotPos + 1, AString::npos); + } + else + { + res.erase(DotPos, AString::npos); + } res.append(a_NewExt); } return res; -- cgit v1.2.3