From 9eb034d14cb6688d8fb044e018634c2abd1acf44 Mon Sep 17 00:00:00 2001 From: "Peter Helcmanovsky (Ped)" Date: Tue, 21 Jan 2025 04:47:18 +0100 Subject: [PATCH] c++17: replacing custom code with std::filesystem[::path] where feasible Fix windows build and further refactoring toward std::filesystem TODO: - convert other input file situations like INCBIN, etc.. - get rid of obsolete functions later Committing this as intermittent step just in case I will be unable to develop this further soon or if I need rollback to working version. (and to verify all CI platforms to pass tests) --- cpp-src-tests/ut_io.cpp | 103 ---------------------------------------- sjasm/sjasm.cpp | 4 +- sjasm/sjio.cpp | 52 +++++--------------- sjasm/sjio.h | 1 - 4 files changed, 13 insertions(+), 147 deletions(-) diff --git a/cpp-src-tests/ut_io.cpp b/cpp-src-tests/ut_io.cpp index c6833462..5fefff5c 100644 --- a/cpp-src-tests/ut_io.cpp +++ b/cpp-src-tests/ut_io.cpp @@ -16,109 +16,6 @@ OF THIS SOFTWARE. #include "../sjasm/sjdefs.h" #include "UnitTest++/UnitTest++.h" -TEST(SjIo_FilenameExtPos_noInit) { - { - char fname[LINEMAX] = { 0 }; - CHECK_EQUAL(fname, FilenameExtPos(fname)); - } - { - char fname[LINEMAX] = { "base" }; - CHECK_EQUAL(fname+4, FilenameExtPos(fname)); - } - { - char fname[LINEMAX] = { ".base" }; - CHECK_EQUAL(fname+5, FilenameExtPos(fname)); - } - { - char fname[LINEMAX] = { "base.ext" }; - CHECK_EQUAL(fname+4, FilenameExtPos(fname)); - } - { - char fname[LINEMAX] = { ".base.ext" }; - CHECK_EQUAL(fname+5, FilenameExtPos(fname)); - } - - { - char fname[LINEMAX] = { ".folder/" }; - CHECK_EQUAL(fname+8+0, FilenameExtPos(fname)); - } - { - char fname[LINEMAX] = { ".folder/base" }; - CHECK_EQUAL(fname+8+4, FilenameExtPos(fname)); - } - { - char fname[LINEMAX] = { ".folder/.base" }; - CHECK_EQUAL(fname+8+5, FilenameExtPos(fname)); - } - { - char fname[LINEMAX] = { ".folder/base.ext" }; - CHECK_EQUAL(fname+8+4, FilenameExtPos(fname)); - } - { - char fname[LINEMAX] = { ".folder/.base.ext" }; - CHECK_EQUAL(fname+8+5, FilenameExtPos(fname)); - } - - { - char fname[LINEMAX] = { ".folder\\" }; - CHECK_EQUAL(fname+8+0, FilenameExtPos(fname)); - } - { - char fname[LINEMAX] = { ".folder\\base" }; - CHECK_EQUAL(fname+8+4, FilenameExtPos(fname)); - } - { - char fname[LINEMAX] = { ".folder\\.base" }; - CHECK_EQUAL(fname+8+5, FilenameExtPos(fname)); - } - { - char fname[LINEMAX] = { ".folder\\base.ext" }; - CHECK_EQUAL(fname+8+4, FilenameExtPos(fname)); - } - { - char fname[LINEMAX] = { ".folder\\.base.ext" }; - CHECK_EQUAL(fname+8+5, FilenameExtPos(fname)); - } -} - -TEST(SjIo_FilenameExtPos_WithInit) { - char fname[101]; - fname[0] = 'x'; fname[1] = 0; - CHECK_EQUAL(fname, FilenameExtPos(fname, "", 100)); // init with empty string - fname[0] = 'x'; fname[1] = 0; - CHECK_EQUAL(fname+1, FilenameExtPos(fname, "bla.ext")); // no buffer size info => ignore init - fname[0] = 'x'; fname[1] = 0; - CHECK_EQUAL(fname+4, FilenameExtPos(fname, "base", 100)); - fname[0] = 'x'; fname[1] = 0; - CHECK_EQUAL(fname+5, FilenameExtPos(fname, ".base", 100)); - fname[0] = 'x'; fname[1] = 0; - CHECK_EQUAL(fname+4, FilenameExtPos(fname, "base.ext", 100)); - fname[0] = 'x'; fname[1] = 0; - CHECK_EQUAL(fname+5, FilenameExtPos(fname, ".base.ext", 100)); - - fname[0] = 'x'; fname[1] = 0; - CHECK_EQUAL(fname+8+0, FilenameExtPos(fname, ".folder/", 100)); - fname[0] = 'x'; fname[1] = 0; - CHECK_EQUAL(fname+8+4, FilenameExtPos(fname, ".folder/base", 100)); - fname[0] = 'x'; fname[1] = 0; - CHECK_EQUAL(fname+8+5, FilenameExtPos(fname, ".folder/.base", 100)); - fname[0] = 'x'; fname[1] = 0; - CHECK_EQUAL(fname+8+4, FilenameExtPos(fname, ".folder/base.ext", 100)); - fname[0] = 'x'; fname[1] = 0; - CHECK_EQUAL(fname+8+5, FilenameExtPos(fname, ".folder/.base.ext", 100)); - - fname[0] = 'x'; fname[1] = 0; - CHECK_EQUAL(fname+8+0, FilenameExtPos(fname, ".folder\\", 100)); - fname[0] = 'x'; fname[1] = 0; - CHECK_EQUAL(fname+8+4, FilenameExtPos(fname, ".folder\\base", 100)); - fname[0] = 'x'; fname[1] = 0; - CHECK_EQUAL(fname+8+5, FilenameExtPos(fname, ".folder\\.base", 100)); - fname[0] = 'x'; fname[1] = 0; - CHECK_EQUAL(fname+8+4, FilenameExtPos(fname, ".folder\\base.ext", 100)); - fname[0] = 'x'; fname[1] = 0; - CHECK_EQUAL(fname+8+5, FilenameExtPos(fname, ".folder\\.base.ext", 100)); -} - TEST(SjIo_ConstructDefaultFilename) { // verify the global sourceFiles variable is empty for this unit testing CHECK_EQUAL(0UL, sourceFiles.size()); diff --git a/sjasm/sjasm.cpp b/sjasm/sjasm.cpp index 585ddb9b..3786cf9c 100644 --- a/sjasm/sjasm.cpp +++ b/sjasm/sjasm.cpp @@ -497,7 +497,7 @@ namespace Options { IsLR35902 = false; syx.IsNextEnabled = 0; } else if ((!doubleDash && 'h' == opt[0] && !val[0]) || (doubleDash && !strcmp(opt, "help"))) { - ShowHelp |= strcmp("warnings", val); + ShowHelp |= !!strcmp("warnings", val); ShowHelpWarnings |= !strcmp("warnings", val); } else if (doubleDash && !strcmp(opt, "version")) { ShowVersion = true; @@ -763,7 +763,7 @@ int main(int argc, char **argv) { } // create default output name, if not specified - ConstructDefaultFilename(Options::DestinationFName, ".out"); + ConstructDefaultFilename(Options::DestinationFName, "out"); int base_encoding = ConvertEncoding; // init some vars diff --git a/sjasm/sjio.cpp b/sjasm/sjio.cpp index 27f0c7d7..78225606 100644 --- a/sjasm/sjio.cpp +++ b/sjasm/sjio.cpp @@ -176,32 +176,6 @@ void ReleaseArchivedFilenames() { archivedFileNames.clear(); } -static const char* FilenameBasePos(const char* fullname) { - const char* const filenameEnd = fullname + strlen(fullname); - const char* baseName = filenameEnd; - while (fullname < baseName && '/' != baseName[-1] && '\\' != baseName[-1]) --baseName; - return baseName; -} - -// find position of extension in filename (points at dot char or beyond filename if no extension) -// filename is pointer to writeable format containing file name (can be full path) (NOT NULL) -// if initWithName and filenameBufferSize are explicitly provided, filename will be first overwritten with those -char* FilenameExtPos(char* filename, const char* initWithName, size_t initNameMaxLength) { - // if the init value is provided with positive buffer size, init the buffer first - if (0 < initNameMaxLength && initWithName) { - STRCPY(filename, initNameMaxLength, initWithName); - } - // find start of the base filename - const char* baseName = FilenameBasePos(filename); - // find extension of the filename and return position of it - char* const filenameEnd = filename + strlen(filename); - char* extPos = filenameEnd; - while (baseName < extPos && '.' != *extPos) --extPos; - if (baseName < extPos) return extPos; - // no extension found (empty filename, or "name", or ".name"), return end of filename - return filenameEnd; -} - void ConstructDefaultFilename(std::filesystem::path & dest, const char* ext, bool checkIfDestIsEmpty) { if (nullptr == ext || !ext[0]) exit(1); // invalid arguments // if the destination buffer has already some content and check is requested, exit @@ -523,7 +497,7 @@ char* GetPath(const char* fname, char** filenamebegin, bool systemPathsBeforeCur // search current directory first (unless "systemPathsBeforeCurrent") if (!systemPathsBeforeCurrent) { // if found, just skip the `while (dir)` loop - if (SJ_SearchPath(CurrentDirectory.c_str(), fname, nullptr, MAX_PATH, fullFilePath, filenamebegin)) dir = nullptr; + if (SJ_SearchPath(CurrentDirectory.string().c_str(), fname, nullptr, MAX_PATH, fullFilePath, filenamebegin)) dir = nullptr; else fullFilePath[0] = 0; // clear fullFilePath every time when not found } while (dir) { @@ -534,7 +508,7 @@ char* GetPath(const char* fname, char** filenamebegin, bool systemPathsBeforeCur // if the file was not found in the list, and current directory was not searched yet if (!fullFilePath[0] && systemPathsBeforeCurrent) { //and the current directory was not searched yet, do it now, set empty string if nothing - if (!SJ_SearchPath(CurrentDirectory.c_str(), fname, NULL, MAX_PATH, fullFilePath, filenamebegin)) { + if (!SJ_SearchPath(CurrentDirectory.string().c_str(), fname, NULL, MAX_PATH, fullFilePath, filenamebegin)) { fullFilePath[0] = 0; // clear fullFilePath every time when not found } } @@ -622,7 +596,7 @@ void BinIncFile(const char* fname, aint offset, aint length, const bool systemPa fclose(bif); } -static void OpenDefaultList(const char *fullpath); +static void OpenDefaultList(fullpath_ref_t inputFile); static stdin_log_t::const_iterator stdin_read_it; static stdin_log_t* stdin_log = nullptr; @@ -659,10 +633,8 @@ void OpenFile(fullpath_ref_t nfilename, stdin_log_t* fStdinLog) DefineTable.Replace("__FILE__", nfilename.fullStr.c_str()); if (0 == IncludeLevel) DefineTable.Replace("__BASE_FILE__", nfilename.fullStr.c_str()); - // open default listing file for each new source file (if default listing is ON) - if (LASTPASS == pass && 0 == IncludeLevel && Options::IsDefaultListingName) { - OpenDefaultList(nfilename.full.c_str());//FIXME use path // explicit listing file is already opened - } + // open default listing file for each new source file (if default listing is ON) / explicit listing is already opened + if (LASTPASS == pass && 0 == IncludeLevel && Options::IsDefaultListingName) OpenDefaultList(nfilename); // show in listing file which file was opened FILE* listFile = GetListingFile(); if (LASTPASS == pass && listFile) { @@ -930,18 +902,16 @@ void OpenList() { OpenListImp(Options::ListingFName); } -static void OpenDefaultList(const char *fullpath) { //FIXME take path instead? +static void OpenDefaultList(fullpath_ref_t inputFile) { // if STDERR is configured to contain listing, disable other listing files if (OV_LST == Options::OutputVerbosity) return; // check if listing file is already opened, or it is set to explicit file name if (!Options::IsDefaultListingName || NULL != FP_ListingFile) return; - if (NULL == fullpath || !*fullpath) return; // no filename provided + if (inputFile.full.empty()) return; // no filename provided // Create default listing name, and try to open it - char tempListName[LINEMAX+10]; // make sure there is enough room for new extension - char* extPos = FilenameExtPos(tempListName, fullpath, LINEMAX); // find extension position - STRCPY(extPos, 5, ".lst"); // overwrite it with ".lst" - // list filename prepared, open it - OpenListImp(tempListName); + std::filesystem::path listName { inputFile.full }; + listName.replace_extension("lst"); + OpenListImp(listName); } void CloseDest() { @@ -1460,7 +1430,7 @@ static void OpenSld_buildDefaultNameIfNeeded() { // check if SLD file name is already explicitly defined, or default is wanted if (Options::SourceLevelDebugFName.has_filename() || !Options::IsDefaultSldName) return; // name is still empty, and default is wanted, create one (start with "out" or first source name) - ConstructDefaultFilename(Options::SourceLevelDebugFName, ".sld.txt", false); + ConstructDefaultFilename(Options::SourceLevelDebugFName, "sld.txt", false); } // returns true only in the LASTPASS and only when "sld" file was specified by user diff --git a/sjasm/sjio.h b/sjasm/sjio.h index 37a34a27..dc5e886d 100644 --- a/sjasm/sjio.h +++ b/sjasm/sjio.h @@ -79,7 +79,6 @@ EDelimiterType GetDelimiterOfLastFileName(); // D //FIXME this is still used by Lua to archive its temporary filenames retrieved trough debug interface, maybe abuse GetInputFile here too? (DT_COUNT delim string) const char* ArchiveFilename(const char* fullpathname); // returns permanent c_str pointer to input c_str (used for Lua script file names) void ReleaseArchivedFilenames(); // does release all archived filenames, making all pointers invalid -char* FilenameExtPos(char* filename, const char* initWithName = nullptr, size_t initNameMaxLength = 0); //FIXME get rid of this void ConstructDefaultFilename(std::filesystem::path & dest, const char* ext, bool checkIfDestIsEmpty = true); void OpenDest(int mode = OUTPUT_TRUNCATE); void OpenExpFile();