@@ -209,25 +209,90 @@ int main(int argc, char* argv[])
209209 info << " ..." ;
210210 bin2cpp::log (bin2cpp::LOG_INFO, info.c_str ());
211211
212- // generate header
212+ // prepare output files path
213213 std::string outputHeaderPath = outputFolder + " \\ " + headerFilename;
214- bin2cpp::log (bin2cpp::LOG_INFO, " Writing file \" %s\" ..." , outputHeaderPath.c_str ());
214+ std::string outputCppPath = outputFolder + " \\ " + headerFilename; bin2cpp::strReplace (outputCppPath, " .h" , " .cpp" );
215+ std::string cppFilename = headerFilename; bin2cpp::strReplace (cppFilename, " .h" , " .cpp" );
216+ uint64_t lastModifiedDate = getFileModifiedDate (inputFile);
217+
215218 bin2cpp::ErrorCodes headerResult = bin2cpp::ErrorCodes::Success;
219+ bin2cpp::ErrorCodes cppResult = bin2cpp::ErrorCodes::Success;
216220
217221 // check if header file already exists
218222 if (bin2cpp::fileExists (outputHeaderPath.c_str ()))
219223 {
220- if (!overrideExisting)
224+ uint64_t outputModifiedDate = getOutputFileModifiedDate (outputHeaderPath);
225+ bool outputFileOutdated = (outputModifiedDate == 0 || lastModifiedDate > outputModifiedDate);
226+ if (outputFileOutdated)
227+ {
228+ // should we force override flag ?
229+ if (overrideExisting)
230+ {
231+ // no problem, user has already choosen to update the output files.
232+ }
233+ else
234+ {
235+ // force overriding output files.
236+ std::string message;
237+ message << " Output file \' " << headerFilename << " \' is out of date. Forcing override flag" ;
238+ bin2cpp::log (bin2cpp::LOG_INFO, message.c_str ());
239+ overrideExisting = true ;
240+ }
241+ }
242+ else if (lastModifiedDate == outputModifiedDate)
243+ {
244+ // output file already up to date.
245+ headerResult = bin2cpp::ErrorCodes::OutputFilesSkipped;
246+ }
247+ else if (!overrideExisting)
248+ {
249+ // fail if not overriding output file
250+ bin2cpp::ErrorCodes error = bin2cpp::ErrorCodes::OutputFileAlreadyExist;
251+ bin2cpp::log (bin2cpp::LOG_ERROR, " %s (%s)" , getErrorCodeDescription (error), outputHeaderPath.c_str ());
252+ return error;
253+ }
254+ }
255+
256+ // check if cpp file already exists
257+ if (bin2cpp::fileExists (outputCppPath.c_str ()))
258+ {
259+ uint64_t outputModifiedDate = getOutputFileModifiedDate (outputCppPath);
260+ bool outputFileOutdated = (outputModifiedDate == 0 || lastModifiedDate > outputModifiedDate);
261+ if (outputFileOutdated)
221262 {
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)
278+ {
279+ // output file already up to date.
280+ cppResult = bin2cpp::ErrorCodes::OutputFilesSkipped;
281+ }
282+ else if (!overrideExisting)
283+ {
284+ // fail if not overriding output file
222285 bin2cpp::ErrorCodes error = bin2cpp::ErrorCodes::OutputFileAlreadyExist;
223286 bin2cpp::log (bin2cpp::LOG_ERROR, " %s (%s)" , getErrorCodeDescription (error), outputHeaderPath.c_str ());
224287 return error;
225288 }
226289 }
227290
291+ // generate header
228292 if (headerResult == bin2cpp::ErrorCodes::Success)
229293 {
230294 // generate file or override existing
295+ bin2cpp::log (bin2cpp::LOG_INFO, " Writing file \" %s\" ..." , outputHeaderPath.c_str ());
231296 headerResult = generator->createHeaderEmbededFile (inputFile.c_str (), outputHeaderPath.c_str (), functionIdentifier.c_str ());
232297 }
233298 if (headerResult == bin2cpp::ErrorCodes::Success)
@@ -246,27 +311,10 @@ int main(int argc, char* argv[])
246311 }
247312
248313 // generate cpp
249- std::string outputCppPath = outputFolder + " \\ " + headerFilename;
250- bin2cpp::strReplace (outputCppPath, " .h" , " .cpp" );
251- bin2cpp::log (bin2cpp::LOG_INFO, " Writing file \" %s\" ..." , outputCppPath.c_str ());
252- bin2cpp::ErrorCodes cppResult = bin2cpp::ErrorCodes::Success;
253-
254- // check if cpp file already exists
255- std::string cppFilename = headerFilename;
256- bin2cpp::strReplace (cppFilename, " .h" , " .cpp" );
257- if (bin2cpp::fileExists (outputCppPath.c_str ()))
258- {
259- if (!overrideExisting)
260- {
261- bin2cpp::ErrorCodes error = bin2cpp::ErrorCodes::OutputFileAlreadyExist;
262- bin2cpp::log (bin2cpp::LOG_ERROR, " %s (%s)" , getErrorCodeDescription (error), outputCppPath.c_str ());
263- return error;
264- }
265- }
266-
267314 if (cppResult == bin2cpp::ErrorCodes::Success)
268315 {
269316 // generate file or override existing
317+ bin2cpp::log (bin2cpp::LOG_INFO, " Writing file \" %s\" ..." , outputCppPath.c_str ());
270318 cppResult = generator->createCppEmbeddedFile (inputFile.c_str (), outputCppPath.c_str (), functionIdentifier.c_str (), chunkSize);
271319 }
272320 if (cppResult == bin2cpp::ErrorCodes::Success)
0 commit comments