From 6952f2295a4ba95f75fe10f2ed8c23ed304dd59a Mon Sep 17 00:00:00 2001 From: Mattes D Date: Mon, 6 Apr 2015 22:00:54 +0200 Subject: Added cFile:ChangeFileExt() function. --- MCServer/Plugins/APIDump/APIDesc.lua | 1 + src/OSSupport/File.cpp | 23 +++++++++++++++++++++++ src/OSSupport/File.h | 4 ++++ 3 files changed, 28 insertions(+) diff --git a/MCServer/Plugins/APIDump/APIDesc.lua b/MCServer/Plugins/APIDump/APIDesc.lua index 9ee818a2c..a892adbcd 100644 --- a/MCServer/Plugins/APIDump/APIDesc.lua +++ b/MCServer/Plugins/APIDump/APIDesc.lua @@ -973,6 +973,7 @@ cFile:Delete("/usr/bin/virus.exe"); ]], Functions = { + ChangeFileExt = { Params = "FileName, NewExt", Return = "string", Notes = "(STATIC) Returns FileName with its extension changed to NewExt. FileName may contain path elements, extension is recognized as the last dot in the string." }, Copy = { Params = "SrcFileName, DstFileName", Return = "bool", Notes = "(STATIC) Copies a single file to a new destination. Returns true if successful. Fails if the destination already exists." }, CreateFolder = { Params = "FolderName", Return = "bool", Notes = "(STATIC) Creates a new folder. Returns true if successful." }, Delete = { Params = "FileName", Return = "bool", Notes = "(STATIC) Deletes the specified file. Returns true if successful." }, diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp index 8957dfaef..7ad1b3f81 100644 --- a/src/OSSupport/File.cpp +++ b/src/OSSupport/File.cpp @@ -453,6 +453,29 @@ AString cFile::ReadWholeFile(const AString & a_FileName) +AString cFile::ChangeFileExt(const AString & a_FileName, const AString & a_NewExt) +{ + auto res = a_FileName; + auto DotPos = res.rfind('.'); + if (DotPos == AString::npos) + { + // No extension, just append it: + res.push_back('.'); + res.append(a_NewExt); + } + else + { + // Replace existing extension: + res.erase(DotPos + 1, AString::npos); + res.append(a_NewExt); + } + return res; +} + + + + + int cFile::Printf(const char * a_Fmt, ...) { AString buf; diff --git a/src/OSSupport/File.h b/src/OSSupport/File.h index ac6d1ab21..1cf5c71d7 100644 --- a/src/OSSupport/File.h +++ b/src/OSSupport/File.h @@ -127,6 +127,10 @@ public: /** Returns the entire contents of the specified file as a string. Returns empty string on error. */ static AString ReadWholeFile(const AString & a_FileName); + /** Returns a_FileName with its extension changed to a_NewExt. + a_FileName may contain path specification. */ + static AString ChangeFileExt(const AString & a_FileName, const AString & a_NewExt); + // tolua_end /** Returns the list of all items in the specified folder (files, folders, nix pipes, whatever's there). */ -- cgit v1.2.3