Skip to content

Commit

Permalink
c++17: replacing custom code with std::filesystem[::path] where feasible
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
ped7g committed Jan 21, 2025
1 parent 1885378 commit 9eb034d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 147 deletions.
103 changes: 0 additions & 103 deletions cpp-src-tests/ut_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions sjasm/sjasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
52 changes: 11 additions & 41 deletions sjasm/sjio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion sjasm/sjio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9eb034d

Please sign in to comment.