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

ParadoxGameConverters / commonItems / 12846535547

18 Jan 2025 06:41PM UTC coverage: 75.846% (-1.7%) from 77.556%
12846535547

Pull #274

github

web-flow
Merge 8b9494010 into b007cb890
Pull Request #274: Eliminate warnings

279 of 358 new or added lines in 13 files covered. (77.93%)

38 existing lines in 2 files now uncovered.

1837 of 2422 relevant lines covered (75.85%)

240.46 hits per line

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

38.81
/OSCommonLayer.cpp
1
#include "Log.h"
2
#include "OSCompatibilityLayer.h"
3
#include <algorithm>
4
#include <codecvt>
5
#include <filesystem>
6

7

8

9
using std::filesystem::copy_options;
10
using std::filesystem::current_path;
11
using std::filesystem::directory_iterator;
12
using std::filesystem::path;
13
using std::filesystem::recursive_directory_iterator;
14
using std::filesystem::u8path;
15

16

17

18
namespace commonItems
19
{
20

UNCOV
21
bool TryCreateFolder(const std::string& path)
×
22
{
23
#pragma warning(push)
24
#pragma warning(disable : 4996)
NEW
25
        if (exists(u8path(path)))
×
UNCOV
26
                return true;
×
NEW
27
        if (create_directory(u8path(path)))
×
UNCOV
28
                return true;
×
29

30
        Log(LogLevel::Error) << "Could not create directory: " << path << " : " << GetLastErrorString();
×
31
        return false;
×
32
#pragma warning(pop)
33
}
34

35
std::wstring GetCurrentDirectoryWString()
×
36
{
37
        // Tried straight returning wstring, but on Linux it will break if filesystem uses characters
38
        // outside ascii, apparently inherent conversion is broken.
39
        try
40
        {
NEW
41
                const auto path = current_path().string();
×
42
                return convertUTF8ToUTF16(path);
×
43
        }
×
44
        catch (std::exception& e)
×
45
        {
46
                Log(LogLevel::Error) << "Cannot determine current path; " << e.what();
×
47
                return {};
×
48
        }
×
49
}
50

51

52
std::set<path> GetAllFilesInFolder(const path& folder_path)
22✔
53
{
54
        std::set<path> fileNames;
22✔
55
        if (!exists(folder_path))
22✔
56
                return fileNames;
1✔
57
        for (const auto& p: directory_iterator(folder_path))
195✔
58
        {
59
                if (!p.is_directory())
174✔
60
                {
61
                        fileNames.insert(p.path().filename());
113✔
62
                }
63
        }
21✔
64
        return fileNames;
21✔
NEW
65
}
×
66

67

68

69
std::set<std::string> GetAllFilesInFolder(const std::string& path)
10✔
70
{
71
#pragma warning(push)
72
#pragma warning(disable : 4996)
73
        std::set<std::string> fileNames;
10✔
74
        if (!exists(u8path(path)))
10✔
UNCOV
75
                return fileNames;
×
76
        for (const auto& p: directory_iterator(u8path(path)))
128✔
77
        {
78
                if (!p.is_directory())
118✔
79
                {
80
                        fileNames.insert(p.path().filename().string());
81✔
81
                }
82
        }
10✔
83
        return fileNames;
10✔
84
#pragma warning(pop)
NEW
85
}
×
86

87

NEW
88
void GetAllFilesInFolder(const std::string& path, std::set<std::string>& fileNames)
×
89
{
90
#pragma warning(push)
91
#pragma warning(disable : 4996)
NEW
92
        fileNames = GetAllFilesInFolder(path);
×
93
#pragma warning(pop)
UNCOV
94
}
×
95

96

97
std::set<path> GetAllSubfolders(const path& folder_path)
20✔
98
{
99
        std::set<path> subFolders;
20✔
100
        if (!exists(folder_path))
20✔
101
                return subFolders;
1✔
102
        for (const auto& p: directory_iterator(folder_path))
179✔
103
        {
104
                if (p.is_directory())
160✔
105
                {
106
                        subFolders.insert(p.path().filename());
56✔
107
                }
108
        }
19✔
109
        return subFolders;
19✔
NEW
110
}
×
111

112

UNCOV
113
std::set<std::string> GetAllSubfolders(const std::string& path)
×
114
{
115
#pragma warning(push)
116
#pragma warning(disable : 4996)
UNCOV
117
        std::set<std::string> subFolders;
×
NEW
118
        if (!exists(u8path(path)))
×
UNCOV
119
                return subFolders;
×
NEW
120
        for (const auto& p: directory_iterator(u8path(path)))
×
121
        {
UNCOV
122
                if (p.is_directory())
×
123
                {
UNCOV
124
                        subFolders.insert(p.path().filename().string());
×
125
                }
UNCOV
126
        }
×
UNCOV
127
        return subFolders;
×
128
#pragma warning(pop)
NEW
129
}
×
130

131

NEW
132
void GetAllSubfolders(const std::string& path, std::set<std::string>& subFolders)
×
133
{
134
#pragma warning(push)
135
#pragma warning(disable : 4996)
NEW
136
        subFolders = GetAllSubfolders(path);
×
137
#pragma warning(pop)
UNCOV
138
}
×
139

140

141
std::set<path> GetAllFilesInFolderRecursive(const path& folder_path)
15✔
142
{
143
        if (!exists(folder_path) || !is_directory(folder_path))
15✔
144
        {
145
                return {};
1✔
146
        }
147

148
        std::set<path> fileNames;
14✔
149
        for (const auto& p: recursive_directory_iterator(folder_path))
84✔
150
        {
151
                if (!p.is_directory())
70✔
152
                {
153
                        path current_path = p.path();
50✔
154
                        current_path.make_preferred();
50✔
155

156
                        // Get the part of current_path that's not in the requested path
157
                        auto current_itr = current_path.begin();
50✔
158
                        for (auto path_itr = folder_path.begin(); path_itr != folder_path.end(); ++current_itr, ++path_itr)
234✔
159
                        {
160
                                if (path_itr->empty())
196✔
161
                                {
162
                                        break;
12✔
163
                                }
164
                        }
165

166
                        path requested_path;
50✔
167
                        for (; current_itr != current_path.end(); ++current_itr)
120✔
168
                        {
169
                                requested_path /= *current_itr;
70✔
170
                        }
171
                        fileNames.insert(requested_path);
50✔
172
                }
50✔
173
        }
14✔
174
        return fileNames;
14✔
175
}
14✔
176

177

NEW
178
void GetAllFilesInFolderRecursive(const std::string& path, std::set<std::string>& fileNames)
×
179
{
180
#pragma warning(push)
181
#pragma warning(disable : 4996)
NEW
182
        fileNames = GetAllFilesInFolderRecursive(path);
×
183
#pragma warning(pop)
NEW
184
}
×
185

186

UNCOV
187
bool TryCopyFile(const std::string& sourcePath, const std::string& destPath)
×
188
{
189
#pragma warning(push)
190
#pragma warning(disable : 4996)
NEW
191
        if (copy_file(u8path(sourcePath), u8path(destPath), copy_options::overwrite_existing))
×
192
                return true;
×
193
        LOG(LogLevel::Warning) << "Could not copy file " << sourcePath << " to " << destPath << " - " << GetLastErrorString();
×
194
        return false;
×
195
#pragma warning(pop)
196
}
197

198

UNCOV
199
bool CopyFolder(const std::string& sourceFolder, const std::string& destFolder)
×
200
{
201
#pragma warning(push)
202
#pragma warning(disable : 4996)
203
        try
204
        {
NEW
205
                copy(u8path(sourceFolder), u8path(destFolder), copy_options::recursive);
×
206
                return true;
×
207
        }
208
        catch (std::exception& e)
×
209
        {
210
                Log(LogLevel::Error) << "Could not copy folder: " << e.what() << " " << GetLastErrorString();
×
211
                return false;
×
212
        }
×
213
#pragma warning(pop)
214
}
215

216

217
bool RenameFolder(const std::string& sourceFolder, const std::string& destFolder)
×
218
{
219
#pragma warning(push)
220
#pragma warning(disable : 4996)
221
        try
222
        {
NEW
223
                rename(u8path(sourceFolder), u8path(destFolder));
×
224
                return true;
×
225
        }
226
        catch (std::exception& e)
×
227
        {
228
                Log(LogLevel::Error) << "Could not rename folder: " << e.what() << " " << GetLastErrorString();
×
229
                return false;
×
230
        }
×
231
#pragma warning(pop)
232
}
233

234

235
bool DoesFileExist(const path& path)
85✔
236
{
237
        return exists(path) && !is_directory(path);
85✔
238
}
239

240

241
bool DoesFileExist(const std::string& path)
14✔
242
{
243
#pragma warning(push)
244
#pragma warning(disable : 4996)
245
        return DoesFileExist(u8path(path));
14✔
246
#pragma warning(pop)
247
}
248

249

250
bool DoesFolderExist(const path& path)
49✔
251
{
252
        return exists(path) && is_directory(path);
49✔
253
}
254

255

UNCOV
256
bool DoesFolderExist(const std::string& path)
×
257
{
258
#pragma warning(push)
259
#pragma warning(disable : 4996)
NEW
260
        return DoesFolderExist(u8path(path));
×
261
#pragma warning(pop)
262
}
263

264

UNCOV
265
bool DeleteFolder(const std::string& folder)
×
266
{
267
#pragma warning(push)
268
#pragma warning(disable : 4996)
NEW
269
        if (!exists(u8path(folder)))
×
UNCOV
270
                return true;
×
NEW
271
        if (remove_all(u8path(folder)) != static_cast<std::uintmax_t>(-1))
×
UNCOV
272
                return true;
×
273
        Log(LogLevel::Error) << "Could not delete folder: " << folder << " " << GetLastErrorString();
×
274
        return false;
×
275
#pragma warning(pop)
276
}
277

278

UNCOV
279
std::string normalizeUTF8Path(const std::string& utf_8_path)
×
280
{
281
        std::string asciiPath = convertUTF8ToASCII(utf_8_path);
×
282
        std::ranges::replace(asciiPath, '/', '_');
×
283
        std::ranges::replace(asciiPath, '\\', '_');
×
284
        std::ranges::replace(asciiPath, ':', '_');
×
285
        std::ranges::replace(asciiPath, '*', '_');
×
286
        std::ranges::replace(asciiPath, '?', '_');
×
287
        std::ranges::replace(asciiPath, '\"', '_');
×
288
        std::ranges::replace(asciiPath, '<', '_');
×
289
        std::ranges::replace(asciiPath, '>', '_');
×
290
        std::ranges::replace(asciiPath, '|', '_');
×
291
        asciiPath.erase(std::ranges::remove(asciiPath, '\t').begin(), asciiPath.end());
×
292
        asciiPath.erase(std::ranges::remove(asciiPath, '\n').begin(), asciiPath.end());
×
293
        asciiPath.erase(std::ranges::remove(asciiPath, '\r').begin(), asciiPath.end());
×
294

295
        return asciiPath;
×
296
}
×
297

298
} // namespace commonItems
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

© 2025 Coveralls, Inc