@@ -215,13 +215,10 @@ int main(int argc, char* argv[])
215215 // prepare output files path
216216 std::string outputHeaderPath = outputFolder + " \\ " + headerFilename;
217217 std::string outputCppPath = outputFolder + " \\ " + headerFilename; bin2cpp::strReplace (outputCppPath, " .h" , " .cpp" );
218- std::string cppFilename = headerFilename; bin2cpp::strReplace (cppFilename, " .h" , " .cpp" );
219-
220- bin2cpp::ErrorCodes headerResult = bin2cpp::ErrorCodes::Success;
221- bin2cpp::ErrorCodes cppResult = bin2cpp::ErrorCodes::Success;
218+ std::string cppFilename = headerFilename; bin2cpp::strReplace (cppFilename, " .h" , " .cpp" );
222219
223220 // process files
224- headerResult = processFile (inputFile, generator, functionIdentifier, chunkSize, overrideExisting, outputHeaderPath);
221+ bin2cpp::ErrorCodes headerResult = processFile (inputFile, generator, functionIdentifier, chunkSize, overrideExisting, outputHeaderPath);
225222 switch (headerResult)
226223 {
227224 case bin2cpp::ErrorCodes::Success:
@@ -232,7 +229,7 @@ int main(int argc, char* argv[])
232229 return headerResult;
233230 };
234231
235- cppResult = processFile (inputFile, generator, functionIdentifier, chunkSize, overrideExisting, outputCppPath);
232+ bin2cpp::ErrorCodes cppResult = processFile (inputFile, generator, functionIdentifier, chunkSize, overrideExisting, outputCppPath);
236233 switch (cppResult)
237234 {
238235 case bin2cpp::ErrorCodes::Success:
@@ -247,77 +244,85 @@ int main(int argc, char* argv[])
247244 return bin2cpp::ErrorCodes::Success;
248245}
249246
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)
247+ enum FILE_UPDATE_MODE
251248{
252- uint64_t lastModifiedDate = getFileModifiedDate (iOutputFilePath);
253- std::string filename = getFilename (iOutputFilePath.c_str ());
254- std::string extension = getFileExtention (iOutputFilePath);
249+ WRITING,
250+ UPDATING,
251+ OVERWRITING,
252+ SKIPPING,
253+ };
255254
256- bin2cpp::ErrorCodes result = bin2cpp::ErrorCodes::Success;
255+ FILE_UPDATE_MODE getFileUpdateMode (const std::string & inputFile, const std::string & iOutputFilePath, bool overrideExisting)
256+ {
257+ if (!bin2cpp::fileExists (iOutputFilePath.c_str ()))
258+ return WRITING;
259+ // at this point, we know that the file exists
257260
258- if (bin2cpp::fileExists (iOutputFilePath.c_str ()))
261+ if (overrideExisting)
262+ return OVERWRITING;
263+
264+ // do not modify the output file if it is not out of date
265+ uint64_t lastModifiedDate = getFileModifiedDate (inputFile);
266+ uint64_t outputModifiedDate = getOutputFileModifiedDate (iOutputFilePath);
267+ if (outputModifiedDate == 0 )
268+ bin2cpp::log (bin2cpp::LOG_WARNING, " Unable to get last modified date of file \' %s\' " , iOutputFilePath.c_str ());
269+ if (lastModifiedDate == outputModifiedDate)
270+ return SKIPPING;
271+
272+ // file is out of date, update it
273+ return UPDATING;
274+ }
275+
276+ const char * getUpdateModeText (const FILE_UPDATE_MODE & iMode)
277+ {
278+ switch (iMode)
259279 {
260- uint64_t outputModifiedDate = getOutputFileModifiedDate (iOutputFilePath);
261- bool outputFileOutdated = (outputModifiedDate == 0 || lastModifiedDate > outputModifiedDate);
262- if (outputFileOutdated)
263- {
264- // should we force override flag ?
265- if (overrideExisting)
266- {
267- // no problem, user has already choosen to update the output files.
268- }
269- else
270- {
271- // force overriding output files.
272- std::string message;
273- message << " Output file \' " << filename << " \' is out of date. Forcing override flag" ;
274- bin2cpp::log (bin2cpp::LOG_INFO, message.c_str ());
275- overrideExisting = true ;
276- }
277- }
278- else if (lastModifiedDate == outputModifiedDate)
279- {
280- // output file already up to date.
281- result = bin2cpp::ErrorCodes::OutputFilesSkipped;
282- }
283- else if (!overrideExisting)
284- {
285- // fail if not overriding output file
286- bin2cpp::ErrorCodes error = bin2cpp::ErrorCodes::OutputFileAlreadyExist;
287- bin2cpp::log (bin2cpp::LOG_ERROR, " %s (%s)" , getErrorCodeDescription (error), iOutputFilePath.c_str ());
288- return error;
289- }
290- }
280+ case WRITING:
281+ return " Writing" ;
282+ case UPDATING:
283+ return " Updating" ;
284+ case OVERWRITING:
285+ return " Overwriting" ;
286+ case SKIPPING:
287+ return " Skipping" ;
288+ default :
289+ return " Unknown" ;
290+ };
291+ }
292+
293+ bin2cpp::ErrorCodes processFile (const std::string & inputFile, bin2cpp::IGenerator * generator, const std::string & functionIdentifier, const size_t & chunkSize, bool overrideExisting, const std::string & iOutputFilePath)
294+ {
295+ FILE_UPDATE_MODE mode = getFileUpdateMode (inputFile, iOutputFilePath, overrideExisting);
296+
297+ // writing message
298+ bin2cpp::log (bin2cpp::LOG_INFO, " %s file \" %s\" ..." , getUpdateModeText (mode), iOutputFilePath.c_str ());
299+
300+ if (mode == SKIPPING)
301+ return bin2cpp::ErrorCodes::OutputFilesSkipped;
291302
292303 // generate file
293- if (result == bin2cpp::ErrorCodes::Success)
304+ bin2cpp::ErrorCodes result = bin2cpp::ErrorCodes::Success;
305+ if (isCppHeaderFile (iOutputFilePath))
294306 {
295- bin2cpp::log (bin2cpp::LOG_INFO, " Writing file \" %s\" ..." , iOutputFilePath.c_str ());
296- if (extension == " .h" )
297- {
298- // generate header
299- result = generator->createHeaderEmbededFile (inputFile.c_str (), iOutputFilePath.c_str (), functionIdentifier.c_str ());
300- }
301- else
302- {
303- // generate cpp
304- result = generator->createCppEmbeddedFile (inputFile.c_str (), iOutputFilePath.c_str (), functionIdentifier.c_str (), chunkSize);
305- }
307+ // generate header
308+ result = generator->createHeaderEmbededFile (inputFile.c_str (), iOutputFilePath.c_str (), functionIdentifier.c_str ());
306309 }
307- if (result == bin2cpp::ErrorCodes::Success)
310+ else
308311 {
309- // OK
312+ // generate cpp
313+ result = generator->createCppEmbeddedFile (inputFile.c_str (), iOutputFilePath.c_str (), functionIdentifier.c_str (), chunkSize);
310314 }
311- else if (result == bin2cpp::ErrorCodes::OutputFilesSkipped)
315+ if (result == bin2cpp::ErrorCodes::Success ||
316+ result == bin2cpp::ErrorCodes::OutputFilesSkipped)
312317 {
313- bin2cpp::log (bin2cpp::LOG_WARNING, " %s" , getErrorCodeDescription (result));
318+ // OK
319+ return bin2cpp::ErrorCodes::Success;
314320 }
315321 else
316322 {
323+ // there was an error generating file
317324 bin2cpp::log (bin2cpp::LOG_ERROR, " %s" , getErrorCodeDescription (result));
318325 bin2cpp::log (bin2cpp::LOG_ERROR, " Embedding failed!" );
319326 return result;
320327 }
321-
322- return bin2cpp::ErrorCodes::Success;
323328}
0 commit comments