-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·687 lines (599 loc) · 21.7 KB
/
main.cpp
File metadata and controls
executable file
·687 lines (599 loc) · 21.7 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/**********************************************************************************
* 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.
*********************************************************************************/
// main.cpp : Defines the entry point for the console application.
//
#include "SegmentGenerator.h"
#include "StringGenerator.h"
#include "ArrayGenerator.h"
#include "Win32ResourceGenerator.h"
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#include "rapidassist/cli.h"
#include "rapidassist/logging.h"
#include "rapidassist/strings.h"
#include "rapidassist/filesystem.h"
#include "rapidassist/process.h"
#include "common.h"
using namespace bin2cpp;
enum APP_ERROR_CODES
{
APP_ERROR_SUCCESS = 0,
APP_ERROR_MISSINGARGUMENTS,
APP_ERROR_INPUTFILENOTFOUND,
APP_ERROR_UNABLETOCREATEOUTPUTFILES,
APP_ERROR_TOOMANYARGUMENTS,
APP_ERROR_INPUTDIRNOTFOUND
};
enum FILE_UPDATE_MODE
{
WRITING,
UPDATING,
OVERWRITING,
SKIPPING,
};
//default values
static const size_t DEFAULT_CHUNK_SIZE = 200;
static const char * DEFAULT_NAMESPACE = "bin2cpp";
static const char * DEFAULT_BASECLASSNAME = "File";
static const IGenerator::CppEncoderEnum DEFAULT_ENCODING = IGenerator::CPP_ENCODER_OCT;
const char * getErrorCodeDescription(const APP_ERROR_CODES & iErrorCode)
{
switch(iErrorCode)
{
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;
default:
return "Unknown error";
};
}
const char * getUpdateModeText(const FILE_UPDATE_MODE & iMode)
{
switch(iMode)
{
case WRITING:
return "Writing";
case UPDATING:
return "Updating";
case OVERWRITING:
return "Overwriting";
case SKIPPING:
return "Skipping";
default:
return "Unknown";
};
}
std::string getIdentifier(const std::string & value)
{
std::string id = value;
int numReplaced = -1;
//no space character
numReplaced = ra::strings::Replace(id, " ", "");
//no space character
numReplaced = ra::strings::Replace(id, " ", "");
return id;
}
std::string getFilenameWithoutExtension(const char * iPath)
{
if (iPath == NULL)
return "";
std::string filename = ra::filesystem::GetFilename(iPath);
std::string extension = ra::filesystem::GetFileExtention(iPath);
//extract filename without extension
std::string filenameWE = filename.substr(0, filename.size() - extension.size());
//remove last dot of the filename if required
filenameWE = ra::strings::TrimRight(filenameWE, '.');
return filenameWE;
}
struct ARGUMENTS
{
bool help;
bool noheader;
bool quiet;
bool version;
bool hasFile;
bool hasDir;
bool hasManagerFile;
std::string inputFile;
std::string inputDir;
std::string outputFolder;
std::string headerFilename;
std::string functionIdentifier;
size_t chunkSize;
bool overrideExisting;
std::string codeNamespace;
std::string baseClass;
std::string managerHeaderFilename;
bool registerfile;
IGenerator::CppEncoderEnum encoding;
std::string generatorName;
};
//pre-declarations
bool generateFile(const std::string & inputFile, const std::string & iOutputFilePath, bin2cpp::IGenerator * generator, bool overrideExisting);
bool generateManagerFile(const std::string & iOutputFilePath, bin2cpp::IGenerator * generator, bool overrideExisting);
APP_ERROR_CODES processSingleFile(const ARGUMENTS & args, bin2cpp::IGenerator * generator);
APP_ERROR_CODES processManagerFiles(const ARGUMENTS & args, bin2cpp::IGenerator * generator);
void printHeader()
{
printf("bin2cpp v%s - Convert binary files into C++ source code.\n", getVersionString() );
printf("Copyright (C) 2013-%d end2endzone.com. All rights reserved.\n", bin2cpp::getCopyrightYear());
printf("bin2cpp is open source software, see http://github.com/end2endzone/bin2cpp \n");
}
void printUsage()
{
//usage string in docopt format. See http://docopt.org/
static const char usage[] =
"Usage:\n"
" bin2cpp --file=<path> --output=<path> --headerfile=<name> --identifier=<name> [--generator=<name>] [--encoding=<name>] [--chunksize=<value>] [--namespace=<value>] [--baseclass=<value>] [--managerfile=<name>] [--registerfile] [--override] [--noheader] [--quiet]\n"
" bin2cpp --help\n"
" bin2cpp --version\n"
"\n"
"Options:\n"
" --help Display this help message.\n"
" --version Display this application version.\n"
" --file=<path> Path of the input file used for embedding as a C++ source code.\n"
" --dir=<path> Path of the input directory used for embedding all files of the directory as a C++ source code.\n"
#ifdef _WIN32
" --output=<path> Output folder where to create generated code. ie: .\\generated_files\n"
#else
" --output=<path> Output folder where to create generated code. ie: ./generated_files\n"
#endif
" --headerfile=<name> File name of the generated C++ Header file. ie: SplashScreen.h\n"
" --generator=<name> Name of the generator to use. Possible values are 'segment', 'string', 'array' and 'win32'. [default: segment].\n"
" --encoding=<name> Name of the binary to string literal encoding to use. Possible values are 'oct' and 'hex'. [default: oct].\n"
" --identifier=<name> Identifier of the function name that is used to get an instance of the file. ie: SplashScreen\n"
" --chunksize=<value> Size in bytes of each string segments (bytes per row). [default: 200].\n"
" --baseclass=<value> The name of the interface for embedded files. [default: File].\n"
" --namespace=<value> The namespace of the generated source code [default: bin2cpp].\n"
" --managerfile=<name> File name of the generated C++ Header file for the FileManager class. ie: FileManager.h\n"
" --registerfile Register the generated file to the FileManager class. [default: false].\n"
" --override Tells bin2cpp to overwrite the destination files.\n"
" --noheader Do not print program header to standard output.\n"
" --quiet Do not log any message to standard output.\n"
"\n";
printf("%s", usage);
}
int main(int argc, char* argv[])
{
ARGUMENTS args;
args.help = false;
args.noheader = false;
args.quiet = false;
args.version = false;
args.hasFile = false;
args.hasDir = false;
args.hasManagerFile = false;
args.chunkSize = 0;
args.overrideExisting;
args.registerfile = false;
std::string dummy;
//help
args.help = ra::cli::ParseArgument("help", dummy, argc, argv);
if (args.help)
{
printHeader();
printUsage();
return APP_ERROR_SUCCESS;
}
//noheader
args.noheader = ra::cli::ParseArgument("noheader", dummy, argc, argv);
//quiet
args.quiet = ra::cli::ParseArgument("quiet", dummy, argc, argv);
//force noheader if quiet
if (args.quiet)
args.noheader = true;
ra::logging::SetQuietMode(args.quiet);
//version
args.version = ra::cli::ParseArgument("version", dummy, argc, argv);
if (args.version)
{
if (!args.noheader)
printHeader();
return APP_ERROR_SUCCESS;
}
if (!args.noheader && !args.quiet)
printHeader();
//mandatory arguments
args.hasFile = ra::cli::ParseArgument("file", args.inputFile, argc, argv);
args.hasDir = ra::cli::ParseArgument("dir", args.inputDir, argc, argv);
args.hasManagerFile = ra::cli::ParseArgument("managerfile", args.managerHeaderFilename, argc, argv);
if (!args.hasFile && !args.hasDir && !args.hasManagerFile)
{
//file, dir or managerfile must be specified
APP_ERROR_CODES error = APP_ERROR_MISSINGARGUMENTS;
ra::logging::Log(ra::logging::LOG_ERROR, "%s (file, dir, managerfile)", getErrorCodeDescription(error));
printUsage();
return error;
}
else if (args.hasFile && args.hasDir)
{
//file OR dir must be specified, not both
APP_ERROR_CODES error = APP_ERROR_TOOMANYARGUMENTS;
ra::logging::Log(ra::logging::LOG_ERROR, "%s (file, dir)", getErrorCodeDescription(error));
printUsage();
return error;
}
if (!ra::cli::ParseArgument("output", args.outputFolder, argc, argv))
{
APP_ERROR_CODES error = APP_ERROR_MISSINGARGUMENTS;
ra::logging::Log(ra::logging::LOG_ERROR, "%s (output)", getErrorCodeDescription(error));
printUsage();
return error;
}
if (args.hasDir)
{
if (ra::cli::ParseArgument("headerfile", args.headerFilename, argc, argv))
{
//headerfile not supported with dir argument
APP_ERROR_CODES error = APP_ERROR_TOOMANYARGUMENTS;
ra::logging::Log(ra::logging::LOG_ERROR, "%s (headerfile)", getErrorCodeDescription(error));
printUsage();
return error;
}
}
else if (args.hasFile)
{
if (!ra::cli::ParseArgument("headerfile", args.headerFilename, argc, argv))
{
APP_ERROR_CODES error = APP_ERROR_MISSINGARGUMENTS;
ra::logging::Log(ra::logging::LOG_ERROR, "%s (headerfile)", getErrorCodeDescription(error));
printUsage();
return error;
}
}
if (args.hasDir)
{
if (ra::cli::ParseArgument("identifier", args.functionIdentifier, argc, argv))
{
//identifier not supported with dir argument
APP_ERROR_CODES error = APP_ERROR_TOOMANYARGUMENTS;
ra::logging::Log(ra::logging::LOG_ERROR, "%s (identifier)", getErrorCodeDescription(error));
printUsage();
return error;
}
}
else if (args.hasFile)
{
if (!ra::cli::ParseArgument("identifier", args.functionIdentifier, argc, argv))
{
APP_ERROR_CODES error = APP_ERROR_MISSINGARGUMENTS;
ra::logging::Log(ra::logging::LOG_ERROR, "%s (identifier)", getErrorCodeDescription(error));
printUsage();
return error;
}
}
//optional arguments
size_t tmpChunkSize = 0;
args.chunkSize = DEFAULT_CHUNK_SIZE;
if (ra::cli::ParseArgument("chunksize", tmpChunkSize, argc, argv))
{
args.chunkSize = tmpChunkSize;
}
args.overrideExisting = ra::cli::ParseArgument("override", dummy, argc, argv);
if (!ra::cli::ParseArgument("namespace", args.codeNamespace, argc, argv))
{
args.codeNamespace = DEFAULT_NAMESPACE;
}
if (!ra::cli::ParseArgument("baseclass", args.baseClass, argc, argv))
{
args.baseClass = DEFAULT_BASECLASSNAME;
}
args.registerfile = ra::cli::ParseArgument("registerfile", dummy, argc, argv);
//force registerfile if managerfile is specified
if (args.hasManagerFile)
{
args.registerfile = true;
}
std::string encodingStr;
if (ra::cli::ParseArgument("encoding", encodingStr, argc, argv))
{
if (ra::strings::Uppercase(encodingStr) == "OCT")
args.encoding = IGenerator::CPP_ENCODER_OCT;
else if (ra::strings::Uppercase(encodingStr) == "HEX")
args.encoding = IGenerator::CPP_ENCODER_HEX;
else
{
APP_ERROR_CODES error = APP_ERROR_MISSINGARGUMENTS;
ra::logging::Log(ra::logging::LOG_ERROR, "%s (encoding)", getErrorCodeDescription(error));
printUsage();
return error;
}
}
else
{
args.encoding = DEFAULT_ENCODING;
}
//select generator
bin2cpp::SegmentGenerator segmentGenerator;
bin2cpp::StringGenerator stringGenerator;
bin2cpp::ArrayGenerator arrayGenerator;
bin2cpp::Win32ResourceGenerator win32Generator;
bin2cpp::IGenerator * generator = NULL;
if (ra::cli::ParseArgument("generator", args.generatorName, argc, argv))
{
if (args.generatorName == "segment")
{
generator = &segmentGenerator;
}
else if (args.generatorName == "string")
{
generator = &stringGenerator;
}
else if (args.generatorName == "array")
{
generator = &arrayGenerator;
}
else if (args.generatorName == "win32")
{
generator = &win32Generator;
}
//validate generator selection
if (generator == NULL)
{
APP_ERROR_CODES error = APP_ERROR_MISSINGARGUMENTS;
ra::logging::Log(ra::logging::LOG_ERROR, "%s, unknown values for 'generator' argument!", getErrorCodeDescription(error));
printUsage();
return error;
}
}
//apply default generator
if (generator == NULL)
{
generator = &segmentGenerator;
}
//process file or directory
if (args.hasFile)
{
APP_ERROR_CODES error = processSingleFile(args, generator);
if (error != APP_ERROR_SUCCESS)
{
ra::logging::Log(ra::logging::LOG_ERROR, "%s", getErrorCodeDescription(error));
return error;
}
}
else if (args.hasDir)
{
//check if input dir exists
if (!ra::filesystem::DirectoryExists(args.inputDir.c_str()))
{
APP_ERROR_CODES error = APP_ERROR_INPUTDIRNOTFOUND;
ra::logging::Log(ra::logging::LOG_ERROR, "%s (%s)", getErrorCodeDescription(error), args.inputDir.c_str());
return error;
}
//search all files in the directory
ra::strings::StringVector files;
bool found = ra::filesystem::FindFiles(files, args.inputDir.c_str());
if (!found)
{
APP_ERROR_CODES error = APP_ERROR_INPUTDIRNOTFOUND;
ra::logging::Log(ra::logging::LOG_ERROR, "%s (%s)", getErrorCodeDescription(error), args.inputDir.c_str());
return error;
}
//remove directories from list
ra::strings::StringVector tmp;
for(size_t i=0; i<files.size(); i++)
{
const std::string & file = files[i];
if (ra::filesystem::FileExists(file.c_str()))
tmp.push_back(file);
}
files = tmp;
//need to process files ?
if (files.empty())
return APP_ERROR_SUCCESS;
//for each files
for(size_t i=0; i<files.size(); i++)
{
const std::string & file = files[i];
//build a 'headerfile' and 'identifier' argument for this file...
ARGUMENTS argsCopy = args;
//replace 'dir' input by current file input
argsCopy.hasDir = false;
argsCopy.inputDir = "";
argsCopy.hasFile = true;
argsCopy.inputFile = file;
//use the file name without extension as 'headerfile'.
argsCopy.headerFilename = getIdentifier(getFilenameWithoutExtension(file.c_str()));
argsCopy.headerFilename.append(".h");
//use the file name without extension as 'identifier'.
argsCopy.functionIdentifier = getIdentifier(getFilenameWithoutExtension(file.c_str()));
argsCopy.functionIdentifier = ra::strings::CapitalizeFirstCharacter(argsCopy.functionIdentifier);
//process this file...
APP_ERROR_CODES error = processSingleFile(argsCopy, generator);
if (error != APP_ERROR_SUCCESS)
{
ra::logging::Log(ra::logging::LOG_ERROR, "%s", getErrorCodeDescription(error));
return error;
}
//next file
}
//all files processed
}
//should we also generate the FileManager class?
if (args.hasManagerFile)
{
APP_ERROR_CODES error = processManagerFiles(args, generator);
if (error != APP_ERROR_SUCCESS)
{
ra::logging::Log(ra::logging::LOG_ERROR, "%s", getErrorCodeDescription(error));
return error;
}
}
return APP_ERROR_SUCCESS;
}
APP_ERROR_CODES processSingleFile(const ARGUMENTS & args, bin2cpp::IGenerator * generator)
{
// printing info
std::string info;
info << "Embedding \"" << args.inputFile << "\"";
if (args.chunkSize != DEFAULT_CHUNK_SIZE)
{
info << " using chunks of ";
info << args.chunkSize;
info << " bytes";
}
if (args.overrideExisting)
info << " overriding existing files";
info << "...";
ra::logging::Log(ra::logging::LOG_INFO, info.c_str());
//prepare output files path
std::string outputHeaderPath = args.outputFolder + ra::filesystem::GetPathSeparatorStr() + args.headerFilename;
std::string outputCppPath = args.outputFolder + ra::filesystem::GetPathSeparatorStr() + args.headerFilename;
std::string cppFilename = args.headerFilename;
ra::strings::Replace(outputCppPath, ".h", ".cpp");
ra::strings::Replace(cppFilename, ".h", ".cpp");
//check if input file exists
if (!ra::filesystem::FileExists(args.inputFile.c_str()))
return APP_ERROR_INPUTFILENOTFOUND;
//configure the generator
generator->setInputFile(args.inputFile.c_str());
generator->setFunctionIdentifier(args.functionIdentifier.c_str());
generator->setChunkSize(args.chunkSize);
generator->setNamespace(args.codeNamespace.c_str());
generator->setBaseClass(args.baseClass.c_str());
generator->setCppEncoder(args.encoding);
generator->setManagerHeaderFile(args.managerHeaderFilename.c_str());
generator->setRegisterFileEnabled(args.registerfile);
//process files
bool headerResult = generateFile(args.inputFile, outputHeaderPath, generator, args.overrideExisting);
if (!headerResult)
return APP_ERROR_UNABLETOCREATEOUTPUTFILES;
bool cppResult = generateFile(args.inputFile, outputCppPath, generator, args.overrideExisting);
if (!cppResult)
return APP_ERROR_UNABLETOCREATEOUTPUTFILES;
//success
return APP_ERROR_SUCCESS;
}
FILE_UPDATE_MODE getFileUpdateMode(const std::string & inputFile, const std::string & iOutputFilePath, bool overrideExisting)
{
if (!ra::filesystem::FileExists(iOutputFilePath.c_str()))
return WRITING;
//at this point, we know that the file exists
if (overrideExisting)
return OVERWRITING;
//do not modify the output file if it is not out of date
uint64_t lastModifiedDate = ra::filesystem::GetFileModifiedDate(inputFile);
uint64_t outputModifiedDate = bin2cpp::getOutputFileModifiedDate(iOutputFilePath);
if (outputModifiedDate == 0)
ra::logging::Log(ra::logging::LOG_WARNING, "Unable to get last modified date of file \'%s\'", iOutputFilePath.c_str());
if (lastModifiedDate == outputModifiedDate)
return SKIPPING;
//file is out of date, update it
return UPDATING;
}
bool generateFile(const std::string & inputFile, const std::string & iOutputFilePath, bin2cpp::IGenerator * generator, bool overrideExisting)
{
FILE_UPDATE_MODE mode = getFileUpdateMode(inputFile, iOutputFilePath, overrideExisting);
//writing message
ra::logging::Log(ra::logging::LOG_INFO, "%s file \"%s\"...", getUpdateModeText(mode), iOutputFilePath.c_str());
if (mode == SKIPPING)
return true; //skipping is success
//generate file
bool result = false;
if (isCppHeaderFile(iOutputFilePath))
{
//generate header
result = generator->createCppHeaderFile(iOutputFilePath.c_str());
}
else
{
//generate cpp
result = generator->createCppSourceFile(iOutputFilePath.c_str());
}
if (!result)
{
//there was an error generating file
ra::logging::Log(ra::logging::LOG_ERROR, "%s", getErrorCodeDescription(APP_ERROR_UNABLETOCREATEOUTPUTFILES));
ra::logging::Log(ra::logging::LOG_ERROR, "Embedding failed!");
}
return result;
}
bool generateManagerFile(const std::string & iOutputFilePath, bin2cpp::IGenerator * generator, bool overrideExisting)
{
std::string processPath = ra::process::GetCurrentProcessPath();
FILE_UPDATE_MODE mode = getFileUpdateMode(processPath, iOutputFilePath, overrideExisting);
//writing message
ra::logging::Log(ra::logging::LOG_INFO, "%s file \"%s\"...", getUpdateModeText(mode), iOutputFilePath.c_str());
if (mode == SKIPPING)
return true; //skipping is success
//generate file
bool result = false;
if (isCppHeaderFile(iOutputFilePath))
{
//generate header
result = generator->createManagerHeaderFile(iOutputFilePath.c_str());
}
else
{
//generate cpp
result = generator->createManagerSourceFile(iOutputFilePath.c_str());
}
if (!result)
{
//there was an error generating file
ra::logging::Log(ra::logging::LOG_ERROR, "%s failed!", getUpdateModeText(mode));
}
return result;
}
APP_ERROR_CODES processManagerFiles(const ARGUMENTS & args, bin2cpp::IGenerator * generator)
{
// printing info
std::string info;
info << "Generating \"" << args.managerHeaderFilename << "\"";
if (args.overrideExisting)
info << " overriding existing files";
info << "...";
ra::logging::Log(ra::logging::LOG_INFO, info.c_str());
//prepare output files path
std::string outputHeaderPath = args.outputFolder + ra::filesystem::GetPathSeparatorStr() + args.managerHeaderFilename;
std::string outputCppPath = args.outputFolder + ra::filesystem::GetPathSeparatorStr() + args.managerHeaderFilename;
std::string cppFilename = args.headerFilename;
ra::strings::Replace(outputCppPath, ".h", ".cpp");
ra::strings::Replace(cppFilename, ".h", ".cpp");
//process files
bool headerResult = generateManagerFile(outputHeaderPath, generator, args.overrideExisting);
if (!headerResult)
return APP_ERROR_UNABLETOCREATEOUTPUTFILES;
bool cppResult = generateManagerFile(outputCppPath, generator, args.overrideExisting);
if (!cppResult)
return APP_ERROR_UNABLETOCREATEOUTPUTFILES;
//success
return APP_ERROR_SUCCESS;
}