Skip to content

Commit f034f11

Browse files
committed
- Created toString() and getFileModifiedDate() APIs.
- Modified all generators to print input file's modified date into generated file header.
1 parent 56e7175 commit f034f11

File tree

7 files changed

+76
-4
lines changed

7 files changed

+76
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@
2222
/msvc/TestCLI.testQuiet.actual.txt
2323
/msvc/TestCLI.testGenerators.actual.txt
2424
/msvc/TestCLI.testOverride.actual.txt
25+
/msvc/TestCommon.testFileModifiedDate.actual.txt

src/ArrayGenerator.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ namespace bin2cpp
5050
return bin2cpp::ErrorCodes::UnableToCreateOutputFiles;
5151
}
5252

53-
//determine segment size
53+
//determine file properties
5454
long fileSize = getFileSize(input);
55+
std::string filename = getFilename(iInputFilename);
56+
uint64_t fileDateModified = getFileModifiedDate(iInputFilename);
5557

5658
//Build class name
5759
std::string className;
@@ -65,6 +67,7 @@ namespace bin2cpp
6567
fprintf(cpp, "/**\n");
6668
fprintf(cpp, " * This file was generated by bin2cpp v%s.\n", getVersionString() );
6769
fprintf(cpp, " * Copyright (C) 2013-%d end2endzone.com. All rights reserved.\n", bin2cpp::getCopyrightYear());
70+
fprintf(cpp, " * Source code for file '%s', last modified %s.\n", filename.c_str(), toString(fileDateModified).c_str());
6871
fprintf(cpp, " * Do not modify this file.\n");
6972
fprintf(cpp, " */\n");
7073
fprintf(cpp, "#include \"%s\"\n", headerFilename.c_str() );

src/SegmentGenerator.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ namespace bin2cpp
5050
return bin2cpp::ErrorCodes::UnableToCreateOutputFiles;
5151
}
5252

53-
//determine segment size
53+
//determine file properties
5454
long fileSize = getFileSize(input);
55+
std::string filename = getFilename(iInputFilename);
56+
uint64_t fileDateModified = getFileModifiedDate(iInputFilename);
5557
//long lastSegmentSize = fileSize%iChunkSize;
5658
//size_t numSegments = fileSize/iChunkSize + (lastSegmentSize == 0 ? 0 : 1);
5759

@@ -67,6 +69,7 @@ namespace bin2cpp
6769
fprintf(cpp, "/**\n");
6870
fprintf(cpp, " * This file was generated by bin2cpp v%s.\n", getVersionString() );
6971
fprintf(cpp, " * Copyright (C) 2013-%d end2endzone.com. All rights reserved.\n", bin2cpp::getCopyrightYear());
72+
fprintf(cpp, " * Source code for file '%s', last modified %s.\n", filename.c_str(), toString(fileDateModified).c_str());
7073
fprintf(cpp, " * Do not modify this file.\n");
7174
fprintf(cpp, " */\n");
7275
fprintf(cpp, "#include \"%s\"\n", headerFilename.c_str() );

src/StringGenerator.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ namespace bin2cpp
5050
return bin2cpp::ErrorCodes::UnableToCreateOutputFiles;
5151
}
5252

53-
//determine segment size
53+
//determine file properties
5454
long fileSize = getFileSize(input);
55+
std::string filename = getFilename(iInputFilename);
56+
uint64_t fileDateModified = getFileModifiedDate(iInputFilename);
5557

5658
//Build class name
5759
std::string className;
@@ -65,6 +67,7 @@ namespace bin2cpp
6567
fprintf(cpp, "/**\n");
6668
fprintf(cpp, " * This file was generated by bin2cpp v%s.\n", getVersionString() );
6769
fprintf(cpp, " * Copyright (C) 2013-%d end2endzone.com. All rights reserved.\n", bin2cpp::getCopyrightYear());
70+
fprintf(cpp, " * Source code for file '%s', last modified %s.\n", filename.c_str(), toString(fileDateModified).c_str());
6871
fprintf(cpp, " * Do not modify this file.\n");
6972
fprintf(cpp, " */\n");
7073
fprintf(cpp, "#include \"%s\"\n", headerFilename.c_str() );

src/common.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@
66
#include <stdio.h>
77
#include <stdlib.h>
88
#include <string>
9-
#include <stdlib.h>
109
#include <direct.h>
1110
#include <sstream>
1211

12+
#include <sys/types.h>
13+
#include <sys/stat.h>
14+
#ifndef WIN32
15+
#include <unistd.h>
16+
#endif
17+
#ifdef WIN32
18+
#define stat _stat
19+
#endif
20+
1321
namespace bin2cpp
1422
{
1523
//Note: http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
@@ -432,4 +440,20 @@ namespace bin2cpp
432440
return friendlySize;
433441
}
434442

443+
uint64_t getFileModifiedDate(const std::string & iPath)
444+
{
445+
struct stat result;
446+
uint64_t mod_time = 0;
447+
if(stat(iPath.c_str(), &result)==0)
448+
{
449+
mod_time = result.st_mtime;
450+
}
451+
return mod_time;
452+
}
453+
454+
std::string toString(const uint64_t & value)
455+
{
456+
return toStringT(value);
457+
}
458+
435459
}; //bin2cpp

src/common.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <stdint.h>
44
#include <string>
5+
#include <stdio.h>
6+
#include <stdint.h>
57

68
namespace bin2cpp
79
{
@@ -152,4 +154,18 @@ namespace bin2cpp
152154
///<return>Returns a given size in a user friendly format and units.<return>
153155
std::string getUserFriendlySize(uint64_t iBytesSize, FileSizeEnum iUnit);
154156

157+
///<summary>
158+
///Returns the modified date of the given file.
159+
///</summary>
160+
///<param name="iPath">The valid path to a file.</param>
161+
///<return>Returns the modified date of the given file.<return>
162+
uint64_t getFileModifiedDate(const std::string & iPath);
163+
164+
///<summary>
165+
///Converts the given value to string.
166+
///</summary>
167+
///<param name="value">The numeric value.</param>
168+
///<return>Converts the given value to string.<return>
169+
std::string toString(const uint64_t & value);
170+
155171
}; //bin2cpp

test/TestCommon.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ namespace TestCommonUtils
2626
using namespace TestCommonUtils;
2727
using namespace bin2cpp;
2828

29+
namespace TestCLIUtils
30+
{
31+
extern bool createDummyFile(const char * iPath);
32+
};
33+
2934
void TestCommon::SetUp()
3035
{
3136
}
@@ -130,3 +135,20 @@ TEST_F(TestCommon, testFileExtention)
130135
}
131136

132137
}
138+
139+
TEST_F(TestCommon, testFileModifiedDate)
140+
{
141+
//assert with file not found
142+
{
143+
uint64_t date = getFileModifiedDate("foobar.txt");
144+
ASSERT_EQ(0, date);
145+
}
146+
147+
//assert with a new file
148+
{
149+
std::string filePath = getActualFilePath();
150+
TestCLIUtils::createDummyFile(filePath.c_str());
151+
uint64_t date = getFileModifiedDate(filePath.c_str());
152+
ASSERT_GT(date, 0);
153+
}
154+
}

0 commit comments

Comments
 (0)