Skip to content

Commit 96024d0

Browse files
committed
Moved repeating code to a unique function processFile(...) for implementing issue #20.
1 parent 3512caf commit 96024d0

File tree

1 file changed

+59
-73
lines changed

1 file changed

+59
-73
lines changed

src/main.cpp

Lines changed: 59 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
using namespace bin2cpp;
2727

28+
//pre-declarations
29+
bin2cpp::ErrorCodes processFile(const std::string & inputFile, bin2cpp::IGenerator * generator, const std::string & functionIdentifier, const size_t & chunkSize, bool overrideExisting, const std::string & iOutputFilePath);
30+
2831
void printHeader()
2932
{
3033
printf("bin2cpp v%s - Convert binary files into C++ source code.\n", bin2cpp::getVersionString() );
@@ -213,15 +216,48 @@ int main(int argc, char* argv[])
213216
std::string outputHeaderPath = outputFolder + "\\" + headerFilename;
214217
std::string outputCppPath = outputFolder + "\\" + headerFilename; bin2cpp::strReplace(outputCppPath, ".h", ".cpp");
215218
std::string cppFilename = headerFilename; bin2cpp::strReplace(cppFilename, ".h", ".cpp");
216-
uint64_t lastModifiedDate = getFileModifiedDate(inputFile);
217219

218220
bin2cpp::ErrorCodes headerResult = bin2cpp::ErrorCodes::Success;
219221
bin2cpp::ErrorCodes cppResult = bin2cpp::ErrorCodes::Success;
220222

221-
//check if header file already exists
222-
if (bin2cpp::fileExists(outputHeaderPath.c_str()))
223+
//process files
224+
headerResult = processFile(inputFile, generator, functionIdentifier, chunkSize, overrideExisting, outputHeaderPath);
225+
switch(headerResult)
226+
{
227+
case bin2cpp::ErrorCodes::Success:
228+
case bin2cpp::ErrorCodes::OutputFilesSkipped:
229+
//ok
230+
break;
231+
default:
232+
return headerResult;
233+
};
234+
235+
cppResult = processFile(inputFile, generator, functionIdentifier, chunkSize, overrideExisting, outputCppPath);
236+
switch(cppResult)
237+
{
238+
case bin2cpp::ErrorCodes::Success:
239+
case bin2cpp::ErrorCodes::OutputFilesSkipped:
240+
//ok
241+
break;
242+
default:
243+
return cppResult;
244+
};
245+
246+
//success
247+
return bin2cpp::ErrorCodes::Success;
248+
}
249+
250+
bin2cpp::ErrorCodes processFile(const std::string & inputFile, bin2cpp::IGenerator * generator, const std::string & functionIdentifier, const size_t & chunkSize, bool overrideExisting, const std::string & iOutputFilePath)
251+
{
252+
uint64_t lastModifiedDate = getFileModifiedDate(iOutputFilePath);
253+
std::string filename = getFilename(iOutputFilePath.c_str());
254+
std::string extension = getFileExtention(iOutputFilePath);
255+
256+
bin2cpp::ErrorCodes result = bin2cpp::ErrorCodes::Success;
257+
258+
if (bin2cpp::fileExists(iOutputFilePath.c_str()))
223259
{
224-
uint64_t outputModifiedDate = getOutputFileModifiedDate(outputHeaderPath);
260+
uint64_t outputModifiedDate = getOutputFileModifiedDate(iOutputFilePath);
225261
bool outputFileOutdated = (outputModifiedDate == 0 || lastModifiedDate > outputModifiedDate);
226262
if (outputFileOutdated)
227263
{
@@ -234,104 +270,54 @@ int main(int argc, char* argv[])
234270
{
235271
//force overriding output files.
236272
std::string message;
237-
message << "Output file \'" << headerFilename << "\' is out of date. Forcing override flag";
273+
message << "Output file \'" << filename << "\' is out of date. Forcing override flag";
238274
bin2cpp::log(bin2cpp::LOG_INFO, message.c_str());
239275
overrideExisting = true;
240276
}
241277
}
242278
else if (lastModifiedDate == outputModifiedDate)
243279
{
244280
//output file already up to date.
245-
headerResult = bin2cpp::ErrorCodes::OutputFilesSkipped;
281+
result = bin2cpp::ErrorCodes::OutputFilesSkipped;
246282
}
247283
else if (!overrideExisting)
248284
{
249285
//fail if not overriding output file
250286
bin2cpp::ErrorCodes error = bin2cpp::ErrorCodes::OutputFileAlreadyExist;
251-
bin2cpp::log(bin2cpp::LOG_ERROR, "%s (%s)", getErrorCodeDescription(error), outputHeaderPath.c_str());
287+
bin2cpp::log(bin2cpp::LOG_ERROR, "%s (%s)", getErrorCodeDescription(error), iOutputFilePath.c_str());
252288
return error;
253289
}
254290
}
255291

256-
//check if cpp file already exists
257-
if (bin2cpp::fileExists(outputCppPath.c_str()))
292+
//generate file
293+
if (result == bin2cpp::ErrorCodes::Success)
258294
{
259-
uint64_t outputModifiedDate = getOutputFileModifiedDate(outputCppPath);
260-
bool outputFileOutdated = (outputModifiedDate == 0 || lastModifiedDate > outputModifiedDate);
261-
if (outputFileOutdated)
262-
{
263-
//should we force override flag ?
264-
if (overrideExisting)
265-
{
266-
//no problem, user has already choosen to update the output files.
267-
}
268-
else
269-
{
270-
//force overriding output files.
271-
std::string message;
272-
message << "Output file \'" << cppFilename << "\' is out of date. Forcing override flag";
273-
bin2cpp::log(bin2cpp::LOG_INFO, message.c_str());
274-
overrideExisting = true;
275-
}
276-
}
277-
else if (lastModifiedDate == outputModifiedDate)
295+
bin2cpp::log(bin2cpp::LOG_INFO, "Writing file \"%s\"...", iOutputFilePath.c_str());
296+
if (extension == ".h")
278297
{
279-
//output file already up to date.
280-
cppResult = bin2cpp::ErrorCodes::OutputFilesSkipped;
298+
//generate header
299+
result = generator->createHeaderEmbededFile(inputFile.c_str(), iOutputFilePath.c_str(), functionIdentifier.c_str());
281300
}
282-
else if (!overrideExisting)
301+
else
283302
{
284-
//fail if not overriding output file
285-
bin2cpp::ErrorCodes error = bin2cpp::ErrorCodes::OutputFileAlreadyExist;
286-
bin2cpp::log(bin2cpp::LOG_ERROR, "%s (%s)", getErrorCodeDescription(error), outputHeaderPath.c_str());
287-
return error;
303+
//generate cpp
304+
result = generator->createCppEmbeddedFile(inputFile.c_str(), iOutputFilePath.c_str(), functionIdentifier.c_str(), chunkSize);
288305
}
289306
}
290-
291-
//generate header
292-
if (headerResult == bin2cpp::ErrorCodes::Success)
293-
{
294-
//generate file or override existing
295-
bin2cpp::log(bin2cpp::LOG_INFO, "Writing file \"%s\"...", outputHeaderPath.c_str());
296-
headerResult = generator->createHeaderEmbededFile(inputFile.c_str(), outputHeaderPath.c_str(), functionIdentifier.c_str());
297-
}
298-
if (headerResult == bin2cpp::ErrorCodes::Success)
307+
if (result == bin2cpp::ErrorCodes::Success)
299308
{
300309
//OK
301310
}
302-
else if (headerResult == bin2cpp::ErrorCodes::OutputFilesSkipped)
311+
else if (result == bin2cpp::ErrorCodes::OutputFilesSkipped)
303312
{
304-
bin2cpp::log(bin2cpp::LOG_WARNING, "%s", getErrorCodeDescription(headerResult));
313+
bin2cpp::log(bin2cpp::LOG_WARNING, "%s", getErrorCodeDescription(result));
305314
}
306315
else
307316
{
308-
bin2cpp::log(bin2cpp::LOG_ERROR, "%s", getErrorCodeDescription(headerResult));
317+
bin2cpp::log(bin2cpp::LOG_ERROR, "%s", getErrorCodeDescription(result));
309318
bin2cpp::log(bin2cpp::LOG_ERROR, "Embedding failed!");
310-
return headerResult;
319+
return result;
311320
}
312-
313-
//generate cpp
314-
if (cppResult == bin2cpp::ErrorCodes::Success)
315-
{
316-
//generate file or override existing
317-
bin2cpp::log(bin2cpp::LOG_INFO, "Writing file \"%s\"...", outputCppPath.c_str());
318-
cppResult = generator->createCppEmbeddedFile(inputFile.c_str(), outputCppPath.c_str(), functionIdentifier.c_str(), chunkSize);
319-
}
320-
if (cppResult == bin2cpp::ErrorCodes::Success)
321-
{
322-
//OK
323-
}
324-
else if (cppResult == bin2cpp::ErrorCodes::OutputFilesSkipped)
325-
{
326-
bin2cpp::log(bin2cpp::LOG_WARNING, "%s", getErrorCodeDescription(cppResult));
327-
}
328-
else
329-
{
330-
bin2cpp::log(bin2cpp::LOG_ERROR, "%s", getErrorCodeDescription(cppResult));
331-
bin2cpp::log(bin2cpp::LOG_ERROR, "Embedding failed!");
332-
return cppResult;
333-
}
334-
335-
//success
336-
return 0;
321+
322+
return bin2cpp::ErrorCodes::Success;
337323
}

0 commit comments

Comments
 (0)