-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcommon.cpp
More file actions
executable file
·464 lines (393 loc) · 12.4 KB
/
common.cpp
File metadata and controls
executable file
·464 lines (393 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/**********************************************************************************
* MIT License
*
* Copyright (c) 2018 Antoine Beauchamp
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*********************************************************************************/
#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sstream>
#include "rapidassist/strings.h"
#include "rapidassist/filesystem.h"
#include "bin2cpp/version.h"
namespace bin2cpp
{
const char * getVersionString()
{
return BIN2CPP_VERSION;
}
const char* getErrorCodeDescription(const APP_ERROR_CODES& error_code)
{
switch ( error_code )
{
case APP_ERROR_SUCCESS:
return "Success";
break;
case APP_ERROR_MISSINGARGUMENTS:
return "Missing arguments";
break;
case APP_ERROR_INPUTFILENOTFOUND:
return "Unable to open input file";
break;
case APP_ERROR_UNABLETOCREATEOUTPUTFILES:
return "Unable to create output files";
break;
case APP_ERROR_TOOMANYARGUMENTS:
return "Too many arguments";
break;
case APP_ERROR_INPUTDIRNOTFOUND:
return "Input directory not found";
break;
case AAP_ERROR_NOTSUPPORTED:
return "Operation not supported";
break;
case APP_ERROR_OPERATIONHASFAILED:
return "Operation has failed";
break;
case APP_ERROR_INVALIDVALUE:
return "Invalid value";
break;
default:
return "Unknown error";
};
}
const char* getUpdateModeText(const FILE_UPDATE_MODE& mode)
{
switch ( mode )
{
case WRITING:
return "Writing";
case UPDATING:
return "Updating";
case OVERWRITING:
return "Overwriting";
case SKIPPING:
return "Skipping";
default:
return "Unknown";
};
}
uint64_t getOutputFileModifiedDate(const std::string & path)
{
uint64_t mod_time = 0;
FILE * f = fopen(path.c_str(), "r");
if (!f)
return mod_time;
//create buffer for each chunks from input buffer
static const size_t BUFFER_SIZE = 10240;
char buffer[BUFFER_SIZE];
while(/*!feof(f)*/ fgets(buffer, BUFFER_SIZE, f) != NULL )
{
//read a text line of the file
std::string text = buffer;
static const char * lastModifiedTag = "last modified";
size_t lastModifiedIndex = text.find(lastModifiedTag);
if (lastModifiedIndex != std::string::npos)
{
std::string date = text.substr(lastModifiedIndex);
ra::strings::Replace(date, lastModifiedTag, "");
ra::strings::Replace(date, " ", "");
ra::strings::Replace(date, ".", "");
//parse date into mod_time
bool parseOK = ra::strings::Parse(date, mod_time);
if (parseOK)
fclose(f); //force existing while loop
}
}
fclose(f);
return mod_time;
}
bool isCppHeaderFile(const std::string & path)
{
std::string extension = ra::strings::Uppercase(ra::filesystem::GetFileExtention(path));
if (extension == "H" || extension == "HPP")
return true;
return false;
}
bool isCppSourceFile(const std::string & path)
{
std::string extension = ra::strings::Uppercase(ra::filesystem::GetFileExtention(path));
if (extension == "CPP" || extension == "CXX")
return true;
return false;
}
bool isCHeaderFile(const std::string & path)
{
std::string extension = ra::strings::Uppercase(ra::filesystem::GetFileExtention(path));
if (extension == "H")
return true;
return false;
}
bool isCSourceFile(const std::string & path)
{
std::string extension = ra::strings::Uppercase(ra::filesystem::GetFileExtention(path));
if (extension == "C")
return true;
return false;
}
std::string getIncludeGuardMacroName(const std::string & path)
{
static const std::string EMPTY_STRING;
if (path.empty())
return EMPTY_STRING;
std::string filename = ra::filesystem::GetFilename(path.c_str());
//remove consecutive spaces
std::string pattern = " ";
while(filename.find(pattern) != std::string::npos)
{
ra::strings::Replace(filename, pattern, pattern.substr(0, 1));
}
//remove consecutive colon (for handling namespaces)
pattern = "::";
while(filename.find(pattern) != std::string::npos)
{
ra::strings::Replace(filename, pattern, pattern.substr(0, 1));
}
//remove consecutive dash
pattern = "--";
while(filename.find(pattern) != std::string::npos)
{
ra::strings::Replace(filename, pattern, pattern.substr(0, 1));
}
//replace
int numSpaces = ra::strings::Replace(filename, " ", "_");
int numColon = ra::strings::Replace(filename, ":", "_");
int numDots = ra::strings::Replace(filename, ".", "_");
int numDash = ra::strings::Replace(filename, "-", "_");
//uppercase
filename = ra::strings::Uppercase(filename);
return filename;
}
std::string filter(std::string str, const std::string & valid_characters)
{
std::string output;
//reserve as many characters as in input string
output.reserve(str.size());
//for each characters in input string
for(size_t i=0; i < str.size(); i++)
{
//is the current character is found in valid characters?
size_t pos = valid_characters.find(str[i], 0);
if (pos != std::string::npos)
output.append(1, str[i]);
}
return output;
}
std::string getFunctionIdentifierFromPath(const std::string & path)
{
std::string id;
//build default id
std::string name = ra::filesystem::GetFilenameWithoutExtension(path.c_str());
std::string ext = ra::filesystem::GetFileExtention(path.c_str());
name = ra::strings::CapitalizeFirstCharacter(name);
ext = ra::strings::CapitalizeFirstCharacter(ext );
id = name + ext;
//filter out characters which are not alphanumeric characters or '_'.
static const std::string validCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
id = filter(id, validCharacters);
return id;
}
std::string getUniqueFunctionIdentifierFromPath(const std::string & path, Dictionary & dict)
{
std::string id = getFunctionIdentifierFromPath(path);
//find an unused identifier
bool exists = dict.find(id) != dict.end();
if (exists) {
std::string base_id = id + "_";
//increase a counter until an identifier does not already exists
size_t counter = 0;
while(exists) {
//duplicate id
//increase counter and generate a new id
counter++;
id = base_id + ra::strings::ToString(counter);
//check again
exists = dict.find(id) != dict.end();
}
}
//this identifier is not already used.
//register this identifier in the dictionary.
dict.insert(id);
return id;
}
std::string getUniqueFilePath(const std::string & base_path, Dictionary & dict)
{
std::string dir;
std::string file_name;
std::string file_ext;
pathSplit(base_path, dir, file_name, file_ext);
std::string next_path = base_path;
//find an unused identifier
bool exists = dict.find(base_path) != dict.end();
if (exists) {
//increase a counter until an identifier does not already exists
size_t counter = 0;
while(exists) {
//duplicate id
//increase counter and generate a new path
counter++;
std::string next_file_name = file_name + "_" + ra::strings::ToString(counter);
next_path = pathJoin(dir, next_file_name, file_ext);
//check again
exists = dict.find(next_path) != dict.end();
}
}
//this identifier is not already used.
//register this identifier in the dictionary.
dict.insert(next_path);
return next_path;
}
#ifdef _WIN32
inline bool isDriveLetter(char c)
{
if ( (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') )
{
return true;
}
return false;
}
#endif
void pathSplit(const std::string & path, std::string & directory, std::string & file_name, std::string & file_extension)
{
std::string tmp = path;
directory = ra::filesystem::GetParentPath(tmp);
if (!directory.empty())
tmp.erase(0, directory.size() + 1); // +1 to erase the last \ character
#ifdef _WIN32
//test special case for root directories
//convert C: to C:\
if (directory.size() == 2 && directory[1] == ':')
{
if (isDriveLetter(directory[0]))
{
directory += "\\";
}
}
#else
//test for root directory
if (directory.empty() && !path.empty() && path[0] == '/')
directory = "/";
#endif
file_name = ra::filesystem::GetFilenameWithoutExtension(tmp.c_str());
file_extension = ra::filesystem::GetFileExtention(tmp);
}
std::string pathJoin(const std::string & directory, const std::string & file_name, const std::string & file_extension)
{
std::string tmp;
if (!directory.empty())
{
tmp += directory;
tmp += ra::filesystem::GetPathSeparatorStr();
#ifdef _WIN32
//special case for root directories
if (directory.size() == 3 && directory[1] == ':' && directory[2] == '\\' && isDriveLetter(directory[0]))
{
tmp.erase(2, 1);
}
#else
//special case for root directory
if (directory == "/")
tmp.erase(0, 1);
#endif
}
if (!file_name.empty())
{
tmp += file_name;
}
if (!file_extension.empty())
{
tmp += ".";
tmp += file_extension;
}
else
{
//no file extension
if (file_name.find('.') != std::string::npos)
{
// this file has a dot in file name
// we must add a dot at the end of the file name to make the distinction.
tmp += ".";
}
}
return tmp;
}
void strSplit(const std::string& value, char separator, std::vector<std::string>& values)
{
values.clear();
size_t start = 0;
size_t end = std::string::npos;
size_t length = 0;
// find first separator
end = value.find(separator, start);
while ( end != std::string::npos )
{
length = end - start;
std::string item = value.substr(start, length);
values.push_back(item);
// find next separator
start = end + 1;
end = value.find(separator, start);
}
// Capture last token
values.push_back(value.substr(start));
}
std::string strJoin(const std::vector<std::string>& values, char separator)
{
std::string output;
for ( size_t i = 0; i < values.size(); i++ )
{
const std::string& element = values[i];
output += element;
bool is_last = (i == (values.size() - 1));
if ( !is_last )
output.append(1, separator);
}
return output;
}
CodeGenerationEnum parseCode(const std::string& value)
{
std::string value_upper = ra::strings::Uppercase(value);
if ( value_upper == "C" )
return CodeGenerationEnum::CODE_GENERATION_C;
if ( value_upper == "CPP" || value_upper == "C++" )
return CodeGenerationEnum::CODE_GENERATION_CPP;
return CodeGenerationEnum::CODE_GENERATION_UNKNOW;
}
const std::string& getDefaultCodeSourceFileExtension(CodeGenerationEnum code)
{
static const std::string EMPTY = "";
static const std::string CPP = "cpp";
static const std::string C = "c";
switch ( code )
{
case CODE_GENERATION_UNKNOW:
return EMPTY;
case CODE_GENERATION_CPP:
return CPP;
case CODE_GENERATION_C:
return C;
default:
return EMPTY;
};
}
}; //bin2cpp