Skip to content

Commit dfa535c

Browse files
committed
Properly implemented issue #20:
* Now using the input file's modified date to know if the output files is outdated. * Implemented the same logic for header and source files: * If file does not exists, it is written. * If the override flag is specified, the file is overwrite. * If the file exists, the application looks if the file is up to date and update the file accordingly. * If not, then the file is untouched (skipped). * Note that application does not return an error anymore when the file already exist. * The application returns a success code for all modes: write, update, overwrite & skip. The application returns an error code is the application is unable to process the selected mode.
1 parent 96024d0 commit dfa535c

File tree

5 files changed

+263
-76
lines changed

5 files changed

+263
-76
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@
2323
/msvc/TestCLI.testGenerators.actual.txt
2424
/msvc/TestCLI.testOverride.actual.txt
2525
/msvc/TestCommon.testFileModifiedDate.actual.txt
26+
/msvc/TestCLI.testNewFiles.actual.txt
27+
/msvc/TestCLI.testSkipping.actual.txt
28+
/msvc/TestCLI.testUpdating.actual.txt
29+
/msvc/TestCLI.testUpdatingFile.actual.txt

src/common.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
#define stat _stat
1919
#endif
2020

21+
#ifdef WIN32
22+
#ifndef WIN32_LEAN_AND_MEAN
23+
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
24+
#endif
25+
#include <Windows.h>
26+
#endif
27+
2128
namespace bin2cpp
2229
{
2330
template <class T>
@@ -552,4 +559,25 @@ namespace bin2cpp
552559
return parseValueT(str, oValue);
553560
}
554561

562+
bool isCppHeaderFile(const std::string & iPath)
563+
{
564+
std::string extension = getFileExtention(iPath);
565+
if (extension == "h" || extension == "hpp")
566+
return true;
567+
return false;
568+
}
569+
570+
bool isCppSourceFile(const std::string & iPath)
571+
{
572+
std::string extension = getFileExtention(iPath);
573+
if (extension == "cpp" || extension == "cxx")
574+
return true;
575+
return false;
576+
}
577+
578+
void sleep(const size_t & iTimeMs)
579+
{
580+
Sleep(iTimeMs);
581+
}
582+
555583
}; //bin2cpp

src/common.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,15 @@ namespace bin2cpp
155155

156156
///<summary>
157157
///Returns the modified date of the given file.
158+
///Note that the function returns the number of seconds elapsed since epoch since Jan 1st 1970.
158159
///</summary>
159160
///<param name="iPath">The valid path to a file.</param>
160161
///<return>Returns the modified date of the given file.<return>
161162
uint64_t getFileModifiedDate(const std::string & iPath);
162163

163164
///<summary>
164165
///Returns the modified date from an embedded file's c++ header/source file.
166+
///Note that the function returns the number of seconds elapsed since epoch since Jan 1st 1970.
165167
///</summary>
166168
///<param name="iPath">The valid path to a c++ header/source file.</param>
167169
///<return>Returns the modified date from an embedded file's c++ header/source file.<return>
@@ -210,4 +212,24 @@ namespace bin2cpp
210212
///<return>Returns true when the parsing is successful.<return>
211213
bool parseValue(const std::string& str, uint64_t & oValue);
212214

215+
///<summary>
216+
///Determine if a given path is a c++ header file.
217+
///</summary>
218+
///<param name="iPath">An valid file path.</param>
219+
///<return>Returns true if iPath is a c++ header file. Returns false otherwise.<return>
220+
bool isCppHeaderFile(const std::string & iPath);
221+
222+
///<summary>
223+
///Determine if a given path is a c++ source file.
224+
///</summary>
225+
///<param name="iPath">An valid file path.</param>
226+
///<return>Returns true if iPath is a c++ source file. Returns false otherwise.<return>
227+
bool isCppSourceFile(const std::string & iPath);
228+
229+
///<summary>
230+
///Sleep the current thread by iTimeMs milliseconds.
231+
///</summary>
232+
///<param name="iTimeMs">The number of milliseconds to sleep.</param>
233+
void sleep(const size_t & iTimeMs);
234+
213235
}; //bin2cpp

src/main.cpp

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)