• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

z00m128 / sjasmplus / 1460

21 Jan 2025 03:16AM UTC coverage: 96.3% (-0.2%) from 96.454%
1460

push

cirrus-ci

ped7g
c++17: replacing custom code with std::filesystem[::path] where feasible

Adding GetInputFileName using std::filesystem::path and making it work
for main OpenFile and IncludeFile.

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)

100 of 105 new or added lines in 6 files covered. (95.24%)

12 existing lines in 2 files now uncovered.

9631 of 10001 relevant lines covered (96.3%)

169323.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.79
/sjasm/support.cpp
1
/*
2

3
  SjASMPlus Z80 Cross Compiler
4

5
  This is modified sources of SjASM by Aprisobal - aprisobal@tut.by
6

7
  Copyright (c) 2005 Sjoerd Mastijn
8

9
  This software is provided 'as-is', without any express or implied warranty.
10
  In no event will the authors be held liable for any damages arising from the
11
  use of this software.
12

13
  Permission is granted to anyone to use this software for any purpose,
14
  including commercial applications, and to alter it and redistribute it freely,
15
  subject to the following restrictions:
16

17
  1. The origin of this software must not be misrepresented; you must not claim
18
         that you wrote the original software. If you use this software in a product,
19
         an acknowledgment in the product documentation would be appreciated but is
20
         not required.
21

22
  2. Altered source versions must be plainly marked as such, and must not be
23
         misrepresented as being the original software.
24

25
  3. This notice may not be removed or altered from any source distribution.
26

27
*/
28

29
// support.cpp
30

31
#include "sjdefs.h"
32

33
FILE* SJ_fopen(const std::filesystem::path & fname, const char* mode) {
4,216✔
34
        if (nullptr == mode || fname.empty()) return nullptr;
4,216✔
35
        return fopen(fname.string().c_str(), mode);
4,192✔
36
}
37

38
FILE* SJ_fopen(const char* fname, const char* mode) {
525✔
39
        if (nullptr == fname) return nullptr;
525✔
40
        return SJ_fopen(std::filesystem::path(fname), mode);
525✔
41
}
42

43
/*
44
FILE* dbg_fopen(const char* fname, const char* modes) {
45
        FILE* f = fopen(fname, modes);
46
        printf("fopen = %p modes [%s]\tname (%lu) [%s]\n", (void*)f, modes, strlen(fname), fname);
47
        return f;
48
}
49
*/
50

51
void SJ_GetCurrentDirectory(int whatever, char* pad) {
516✔
52
        pad[0] = 0;
516✔
53
        //TODO implement this one? And decide what to do with it?
54
        // Will affect "--fullpath" paths if implemented correctly (as GetCurrentDirectory on windows)
55
        //FIXME double-check with new std::filesystem usage, I think there may be API for this
56
}
516✔
57

58
static bool isAnySlash(const char c) {
324✔
59
        return pathGoodSlash == c || pathBadSlash == c;
324✔
60
}
61

62
/**
63
 * @brief Check if the path does start with MS windows drive-letter and colon, but accepts
64
 * only absolute form with slash after colon, otherwise warns about relative way not supported.
65
 *
66
 * @param filePath p_filePath: filename to check
67
 * @return bool true if the filename contains drive-letter with ABSOLUTE path
68
 */
69
static bool isWindowsDrivePathStart(const char* filePath) {
270✔
70
        if (!filePath || !filePath[0] || ':' != filePath[1]) return false;
270✔
UNCOV
71
        const char driveLetter = toupper(filePath[0]);
×
UNCOV
72
        if (driveLetter < 'A' || 'Z' < driveLetter) return false;
×
UNCOV
73
        if (!isAnySlash(filePath[2])) {
×
UNCOV
74
                Warning("Relative file path with drive letter detected (not supported)", filePath, W_EARLY);
×
UNCOV
75
                return false;
×
76
        }
UNCOV
77
        return true;
×
78
}
79

80
int SJ_SearchPath(const char* oudzp, const char* filename, const char*, int maxlen, char* nieuwzp, char** ach) {
270✔
81
        assert(nieuwzp);
270✔
82
        *nieuwzp = 0;
270✔
83
        if (nullptr == filename) return 0;
270✔
84
        if (isAnySlash(filename[0]) || isWindowsDrivePathStart(filename)) {
270✔
UNCOV
85
                STRCPY(nieuwzp, maxlen, filename);
×
86
        } else {
87
                STRCPY(nieuwzp, maxlen, oudzp);
270✔
88
                if (*nieuwzp) {
270✔
89
                        char *lastChar = nieuwzp + strlen(nieuwzp) - 1;
54✔
90
                        if (!isAnySlash(*lastChar)) {
54✔
91
                                lastChar[1] = pathGoodSlash;
39✔
92
                                lastChar[2] = 0;
39✔
93
                        }
94
                }
95
                STRCAT(nieuwzp, maxlen, filename);
270✔
96
        }
97
        if (ach) {
270✔
UNCOV
98
                char* p = *ach = nieuwzp;
×
UNCOV
99
                while (*p) {
×
UNCOV
100
                        if (isAnySlash(*p++)) *ach = p;
×
101
                }
102
        }
103
        FILE* fp;
104
        if (FOPEN_ISOK(fp, nieuwzp, "r")) {
270✔
105
                fclose(fp);
243✔
106
                return 1;
243✔
107
        }
108
        return 0;
27✔
109
}
110

111
#ifndef WIN32
112

113
long GetTickCount() {
519✔
114
        struct timeval tv1[1];
115
        gettimeofday(tv1, 0);
519✔
116
        return tv1->tv_sec * 1000 + tv1->tv_usec / 1000;
519✔
117
}
118

119
#endif        // #ifndef WIN32
120

121
#if defined (_WIN32) || defined (__CYGWIN__)
122
        // cygwin: O_BINARY is in fcntl.h, setmode is in io.h
123
        // MSVC: _O_BINARY and _setmode
124
        #include <fcntl.h>
125
        #include <io.h>
126
#endif
127

128
void switchStdOutIntoBinaryMode() {
1✔
129
#ifdef __CYGWIN__
130
        setmode(1, O_BINARY);
131
#elif _WIN32
132
        _setmode(1, _O_BINARY);
133
#else
134
        // nothing on systems with no text-vs-binary mode
135
#endif
136
}
1✔
137

138
#if defined (_WIN32)
139
static bool restoreWinMode = false;
140
static HANDLE hWinOut;
141
static HANDLE hWinIn;
142
static DWORD dwOriginalOutMode = 0, dwOriginalInMode = 0;
143

144
void restoreOriginalConsoleMode() {
145
        if (!restoreWinMode) return;
146
        SetConsoleMode(hWinIn, dwOriginalInMode);
147
        SetConsoleMode(hWinOut, dwOriginalOutMode);
148
}
149
#endif
150

151
bool autoColorsDetection() {
517✔
152
        // existence of NO_COLOR env.var. disables auto-colors: http://no-color.org/
153
        const char* envNoColor = std::getenv("NO_COLOR");
517✔
154
        if (envNoColor) return false;
517✔
155
        // check either TERM variable or in windows try to enable virtual terminal emulation
156
#if defined (_WIN32)
157
        // check if running inside console with isatty
158
        if (!_isatty(_fileno(stderr))) return false;        // redirected to file? don't color
159
        // Try to set output mode to handle virtual terminal sequences (VT100)
160
        hWinOut = GetStdHandle(STD_OUTPUT_HANDLE);
161
        hWinIn = GetStdHandle(STD_INPUT_HANDLE);
162
        if (hWinOut != INVALID_HANDLE_VALUE && hWinIn != INVALID_HANDLE_VALUE) {
163
                DWORD dwOutMode = 0;
164
                DWORD dwInMode = 0;
165
                if (GetConsoleMode(hWinOut, &dwOutMode) && GetConsoleMode(hWinIn, &dwInMode)) {
166
                        dwOriginalInMode = dwInMode;
167
                        dwOriginalOutMode = dwOutMode;
168
                        restoreWinMode = true;
169
                        std::atexit(restoreOriginalConsoleMode);
170
                        dwOutMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
171
                        dwInMode |= ENABLE_VIRTUAL_TERMINAL_INPUT;
172
                        return SetConsoleMode(hWinOut, dwOutMode) && SetConsoleMode(hWinIn, dwInMode);
173
                }
174
        }
175
        return false;
176
#else
177
        // check if running inside console with isatty
178
        if (!isatty(STDERR_FILENO)) return false;                // redirected to file? don't color
511✔
179
        // try to auto-detect ANSI-colour support (true if env.var. TERM exist and contains "color" substring)
180
        const char* envTerm = std::getenv("TERM");
×
181
        return envTerm && strstr(envTerm, "color");
×
182
#endif
183
}
184

185
#ifdef USE_LUA
186

187
void LuaShellExec(const char *command) {
1✔
188
#ifdef WIN32
189
        WinExec(command, SW_SHOWNORMAL);
190
#else
191
        int ret = system(command);
1✔
192
        if ( ret == -1 ) {
1✔
193
                Error("[LUASHELEXEC] Unable to start child process for command", command, IF_FIRST);
×
194
        }
195
#endif
196
}
1✔
197
#endif //USE_LUA
198

199
//eof support.cpp
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc