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
Even Lua integration now does use the main GetInputFile to archive the
filenames and get stable c_str() pointers.

Some other FIXME resolved/verified.
  • Loading branch information
ped7g committed Jan 24, 2025
1 parent f3df4db commit 0d46a8e
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 30 deletions.
5 changes: 5 additions & 0 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ ARM! https://cirrus-ci.org/guide/linux/
> Cirrus CI supports container and arm_container instances in order to run your CI workloads on amd64 and arm64 platforms respectively.

@Janko at Next discord: `--raw-fill-gaps` to produce fill when second ORG is used and --raw output. (like org $2000 ... org $2100 -> test.dot)
@Janko: I would rather prefer a new -o option which by default also sets the device to "just 64K" model and does what SAVE would do
^ some implicit device thing? like NOSLOT64K? But needs modified regions (or min/max program counter) - should NOT depend on zeroed values :(

ad APPENDBIN - also maybe extend SAVENEX to get var-arg list, not just single file.
(but then it may not fit on single line, so some extra appendbin would be still useful)

-------------
can you add a --utc option to sjasmplus so that I don't have to change my hardware time if I want __DATE__ to use UTC.
Expand Down
2 changes: 2 additions & 0 deletions docs/documentation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
<synopsis>
- C++17 standard is now required minimum (to compile sjasmplus sources) (GCC9 for example)
- <link linkend="po_incbin">`INCBIN`</link>, <link linkend="po_inchob">`INCHOB`</link> and <link linkend="po_inctrd">`INCTRD`</link> support now include paths priority (angle &lt;filename&gt; vs "filename")
- also <link linkend="po_incbin">`SAVENEX`</link> will respect include paths for source files (BMP, append)
- include paths which can't be opened before assembling are now reported in error message
- include paths which start with literal tilde '~' are reported in error message
- include options <link linkend="s_cli">`-i, -I, --inc`</link> can take path from next CLI argument (new recommended syntax)
Expand Down Expand Up @@ -5043,6 +5044,7 @@ $ cmake --install build</programlisting></para>
</term>
<listitem>
<para>
The NEX format V1.3 feature.
Can be used after <link linkend="nex_open">OPEN</link> and before first <link
linkend="nex_auto">AUTO</link> or <link linkend="nex_bank">BANK</link>
command (the copper data are stored between screen and bank data).
Expand Down
7 changes: 4 additions & 3 deletions sjasm/lua_sjasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ static int addLuaSourcePositions() {
if (isInlinedScript(levelErrorPos, ar.short_src)) {
levelErrorPos.line += ar.currentline;
} else {
levelErrorPos.newFile(ArchiveFilename(ar.short_src));
fullpath_ref_t archivedFname = GetInputFile(delim_string_t(ar.short_src, DT_COUNT));
levelErrorPos.newFile(archivedFname.fullStr.c_str());
levelErrorPos.line = ar.currentline;
}
luaPosTemp.push_back(levelErrorPos);
Expand Down Expand Up @@ -132,8 +133,8 @@ static TextFilePos lua_impl_splitLuaErrorMessage(const char*& LuaError) {
luaErrorPos.line += lineNumber;
} else {
// standalone script, use file name and line number as is (if provided by lua error)
STRNCPY(internal_script_name, LINEMAX, LuaError, colonPos - LuaError);
luaErrorPos.newFile(ArchiveFilename(internal_script_name));
fullpath_ref_t archFname = GetInputFile(delim_string_t(std::string(LuaError, colonPos), DT_COUNT));
luaErrorPos.newFile(archFname.fullStr.c_str());
luaErrorPos.line = lineNumber;
}

Expand Down
2 changes: 0 additions & 2 deletions sjasm/sjasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ void FreeRAM() {
PreviousIsLabel = nullptr;
}
if (Options::IncludeDirsList) delete Options::IncludeDirsList;
ReleaseArchivedFilenames();
}


Expand Down Expand Up @@ -662,7 +661,6 @@ int main(int argc, char **argv) {
sourcePosStack.reserve(32);
smartSmcLines.reserve(64);
sourceFiles.reserve(32);
archivedFileNames.reserve(64);

CHECK_UNIT_TESTS // UnitTest++ extra handling in specially built executable

Expand Down
21 changes: 2 additions & 19 deletions sjasm/sjio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ static constexpr char pathBadSlash = '\\';
static constexpr char pathGoodSlash = '/';

int ListAddress;
std::vector<const char*> archivedFileNames; // archive of filename strings (Lua scripts only?)

static constexpr int LIST_EMIT_BYTES_BUFFER_SIZE = 1024 * 64;
static constexpr int DESTBUFLEN = 8192;
Expand Down Expand Up @@ -143,21 +142,6 @@ fullpath_ref_t GetInputFile(char*& p) {
return GetInputFile(std::move(name_in));
}

// returns permanent C-string pointer to the fullpathname (if new, it is added to archive)
const char* ArchiveFilename(const char* fullpathname) {
for (auto fname : archivedFileNames) { // search whole archive for identical full name
if (!strcmp(fname, fullpathname)) return fname;
}
archivedFileNames.push_back(STRDUP(fullpathname));
return archivedFileNames.back();
}

// does release all archived filenames, making all pointers (and archive itself) invalid
void ReleaseArchivedFilenames() {
for (auto filename : archivedFileNames) free((void*)filename);
archivedFileNames.clear();
}

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 @@ -630,7 +614,6 @@ void OpenFile(fullpath_ref_t nfilename, stdin_log_t* fStdinLog)
// refresh pre-defined values related to file/include
DefineTable.Replace("__INCLUDE_LEVEL__", IncludeLevel);
DefineTable.Replace("__FILE__", fileNameFull ? fileNameFull->fullStr.c_str() : "<none>");
//FIXME add in some include tests test of __FILE__ just to verify this has still valid c_str pointer
if (-1 == IncludeLevel) DefineTable.Replace("__BASE_FILE__", "<none>");
}

Expand Down Expand Up @@ -964,11 +947,11 @@ void OpenTapFile(const std::filesystem::path & tapename, int flagbyte)
}
}

// check if file exists and seems to be regular file (maybe character works too? stdin is what? FIXME)
// check if file exists and can be read for content
bool FileExists(const std::filesystem::path & file_name) {
return std::filesystem::exists(file_name) && (
std::filesystem::is_regular_file(file_name) ||
std::filesystem::is_character_file(file_name) //FIXME verify with stdin if this is needed
std::filesystem::is_character_file(file_name) // true for very rare files like /dev/null
);
}

Expand Down
6 changes: 1 addition & 5 deletions sjasm/sjio.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
enum EReturn { END, ELSE, ENDIF, ENDTEXTAREA, ENDM, ELSEIF };

extern int ListAddress;
extern std::vector<const char*> archivedFileNames; // archive of all files opened (also includes!) (fullname!)

constexpr int BYTES_END_MARKER = -1;
constexpr int INSTRUCTION_START_MARKER = -2;
Expand Down Expand Up @@ -71,11 +70,8 @@ using files_in_map_t = std::map<const delim_string_t, const SInputFile>;
fullpath_ref_t GetInputFile(delim_string_t && in);
fullpath_ref_t GetInputFile(char*& p);
std::filesystem::path GetOutputFileName(char*& p);

//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
void ConstructDefaultFilename(std::filesystem::path & dest, const char* ext, bool checkIfDestIsEmpty = true);

void OpenDest(int mode = OUTPUT_TRUNCATE);
void OpenExpFile();
void NewDest(const std::filesystem::path & newfilename, int mode = OUTPUT_TRUNCATE);
Expand Down
2 changes: 1 addition & 1 deletion sjasm/tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct TextFilePos {
void nextSegment(bool endsWithColon = false, size_t advanceColumns = 0);

inline bool operator == (const TextFilePos & b) const {
// compares pointers to filenames (!), as they should be stable, provided by ArchiveFilename
// compares pointers to filenames (!), as pointers should be stable (archived by GetInputFile)
return filename == b.filename && line == b.line;
}
inline bool operator != (const TextFilePos & b) const {
Expand Down

0 comments on commit 0d46a8e

Please sign in to comment.