Skip to content

Commit 207d652

Browse files
committed
Updated with latest cross-platform libraries
1 parent 399f83c commit 207d652

File tree

9 files changed

+347
-6
lines changed

9 files changed

+347
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@
2929
/msvc/TestCLI.testUpdatingFile.actual.txt
3030
/msvc/TestCLI.testEncoding.actual.txt
3131
/build
32+
/msvc/TestFilesystemFunc.testGetFileSize
33+
/msvc/TestFilesystemFunc.testFileExists

msvc/bin2cpp_unittest.vcxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@
108108
<ClCompile Include="..\src\bin2cpp_unittest\main.cpp" />
109109
<ClCompile Include="..\src\bin2cpp_unittest\TestCLI.cpp" />
110110
<ClCompile Include="..\src\bin2cpp_unittest\TestCommon.cpp" />
111+
<ClCompile Include="..\src\bin2cpp_unittest\TestEnvironmentFunc.cpp" />
111112
<ClCompile Include="..\src\bin2cpp_unittest\TestExtraction.cpp" />
113+
<ClCompile Include="..\src\bin2cpp_unittest\TestFilesystemFunc.cpp" />
114+
<ClCompile Include="..\src\bin2cpp_unittest\TestStringFunc.cpp" />
112115
<ClCompile Include="generated_files\testBaseClass\_testBaseClass.cpp" />
113116
<ClCompile Include="generated_files\testEncodingHex\_testEncodingHex.cpp" />
114117
<ClCompile Include="generated_files\testEncodingOct\_testEncodingOct.cpp" />
@@ -154,7 +157,10 @@
154157
<ClInclude Include="..\src\bin2cpp_unittest\gtesthelper.h" />
155158
<ClInclude Include="..\src\bin2cpp_unittest\TestCLI.h" />
156159
<ClInclude Include="..\src\bin2cpp_unittest\TestCommon.h" />
160+
<ClInclude Include="..\src\bin2cpp_unittest\TestEnvironmentFunc.h" />
157161
<ClInclude Include="..\src\bin2cpp_unittest\TestExtraction.h" />
162+
<ClInclude Include="..\src\bin2cpp_unittest\TestFilesystemFunc.h" />
163+
<ClInclude Include="..\src\bin2cpp_unittest\TestStringFunc.h" />
158164
<ClInclude Include="generated_files\testBaseClass\_testBaseClass.h" />
159165
<ClInclude Include="generated_files\testEncodingHex\_testEncodingHex.h" />
160166
<ClInclude Include="generated_files\testEncodingOct\_testEncodingOct.h" />

msvc/bin2cpp_unittest.vcxproj.filters

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@
117117
<ClCompile Include="..\src\bin2cpp_unittest\main.cpp">
118118
<Filter>Source Files</Filter>
119119
</ClCompile>
120+
<ClCompile Include="..\src\bin2cpp_unittest\TestEnvironmentFunc.cpp">
121+
<Filter>Tests</Filter>
122+
</ClCompile>
123+
<ClCompile Include="..\src\bin2cpp_unittest\TestFilesystemFunc.cpp">
124+
<Filter>Tests</Filter>
125+
</ClCompile>
126+
<ClCompile Include="..\src\bin2cpp_unittest\TestStringFunc.cpp">
127+
<Filter>Tests</Filter>
128+
</ClCompile>
120129
</ItemGroup>
121130
<ItemGroup>
122131
<None Include="bin2cppTest.x86.debug.xml">
@@ -218,6 +227,15 @@
218227
<ClInclude Include="..\src\bin2cpp_unittest\gtesthelper.h">
219228
<Filter>Header Files</Filter>
220229
</ClInclude>
230+
<ClInclude Include="..\src\bin2cpp_unittest\TestEnvironmentFunc.h">
231+
<Filter>Tests</Filter>
232+
</ClInclude>
233+
<ClInclude Include="..\src\bin2cpp_unittest\TestFilesystemFunc.h">
234+
<Filter>Tests</Filter>
235+
</ClInclude>
236+
<ClInclude Include="..\src\bin2cpp_unittest\TestStringFunc.h">
237+
<Filter>Tests</Filter>
238+
</ClInclude>
221239
</ItemGroup>
222240
<ItemGroup>
223241
<CustomBuild Include="generate_test_files.bat" />

src/bin2cpp_unittest/TestFilesystemFunc.cpp

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "TestFilesystemFunc.h"
22
#include "filesystemfunc.h"
3+
#include "nativefunc.h"
34
#include "gtesthelper.h"
45

56
using namespace filesystem;
@@ -275,5 +276,234 @@ namespace filesystem { namespace test
275276
}
276277
}
277278
//--------------------------------------------------------------------------------------------------
279+
TEST_F(TestFilesystemFunc, testSplitPath)
280+
{
281+
//test baseline
282+
{
283+
static const std::string EXPECTED_PARENT = "/home/myFolder";
284+
static const std::string EXPECTED_FILENAME = "myFile.txt";
285+
std::string parent, filename;
286+
filesystem::splitPath("/home/myFolder/myFile.txt", parent, filename);
287+
ASSERT_EQ(EXPECTED_PARENT, parent);
288+
ASSERT_EQ(EXPECTED_FILENAME, filename);
289+
}
290+
291+
//test empty
292+
{
293+
static const std::string EXPECTED_PARENT = "";
294+
static const std::string EXPECTED_FILENAME = "";
295+
std::string parent, filename;
296+
filesystem::splitPath("", parent, filename);
297+
ASSERT_EQ(EXPECTED_PARENT, parent);
298+
ASSERT_EQ(EXPECTED_FILENAME, filename);
299+
}
300+
301+
//test single filename
302+
{
303+
static const std::string EXPECTED_PARENT = "";
304+
static const std::string EXPECTED_FILENAME = "myfile.txt";
305+
std::string parent, filename;
306+
filesystem::splitPath("myfile.txt", parent, filename);
307+
ASSERT_EQ(EXPECTED_PARENT, parent);
308+
ASSERT_EQ(EXPECTED_FILENAME, filename);
309+
}
310+
311+
//test spaces in folder
312+
{
313+
static const std::string EXPECTED_PARENT = "/home/my Folder";
314+
static const std::string EXPECTED_FILENAME = "myFile.txt";
315+
std::string parent, filename;
316+
filesystem::splitPath("/home/my Folder/myFile.txt", parent, filename);
317+
ASSERT_EQ(EXPECTED_PARENT, parent);
318+
ASSERT_EQ(EXPECTED_FILENAME, filename);
319+
}
320+
321+
//test filename without extension / folder name
322+
{
323+
static const std::string EXPECTED_PARENT = "/home/myFolder";
324+
static const std::string EXPECTED_FILENAME = "myFile";
325+
std::string parent, filename;
326+
filesystem::splitPath("/home/myFolder/myFile", parent, filename);
327+
ASSERT_EQ(EXPECTED_PARENT, parent);
328+
ASSERT_EQ(EXPECTED_FILENAME, filename);
329+
}
330+
}
331+
//--------------------------------------------------------------------------------------------------
332+
TEST_F(TestFilesystemFunc, testSplitPathArray)
333+
{
334+
//test baseline
335+
{
336+
std::vector<std::string> elements;
337+
filesystem::splitPath("/home/myFolder/myFile.txt", elements);
338+
ASSERT_EQ(3, elements.size());
339+
ASSERT_EQ("home", elements[0]);
340+
ASSERT_EQ("myFolder", elements[1]);
341+
ASSERT_EQ("myFile.txt", elements[2]);
342+
}
343+
344+
//test empty
345+
{
346+
std::vector<std::string> elements;
347+
filesystem::splitPath("", elements);
348+
ASSERT_EQ(0, elements.size());
349+
}
350+
351+
//test single filename
352+
{
353+
std::vector<std::string> elements;
354+
filesystem::splitPath("myfile.txt", elements);
355+
ASSERT_EQ(1, elements.size());
356+
ASSERT_EQ("myfile.txt", elements[0]);
357+
}
358+
359+
//test spaces in folder
360+
{
361+
std::vector<std::string> elements;
362+
filesystem::splitPath("/home/my Folder/myFile.txt", elements);
363+
ASSERT_EQ(3, elements.size());
364+
ASSERT_EQ("home", elements[0]);
365+
ASSERT_EQ("my Folder", elements[1]);
366+
ASSERT_EQ("myFile.txt", elements[2]);
367+
}
368+
369+
//test filename without extension / folder name
370+
{
371+
std::vector<std::string> elements;
372+
filesystem::splitPath("/home/myFolder/myFile", elements);
373+
ASSERT_EQ(3, elements.size());
374+
ASSERT_EQ("home", elements[0]);
375+
ASSERT_EQ("myFolder", elements[1]);
376+
ASSERT_EQ("myFile", elements[2]);
377+
}
378+
}
379+
//--------------------------------------------------------------------------------------------------
380+
TEST_F(TestFilesystemFunc, testGetPathSeparator)
381+
{
382+
#ifdef WIN32
383+
ASSERT_EQ('\\', filesystem::getPathSeparator());
384+
#elif UNIX
385+
ASSERT_EQ('/', filesystem::getPathSeparator());
386+
#endif
387+
}
388+
//--------------------------------------------------------------------------------------------------
389+
TEST_F(TestFilesystemFunc, testGetCurrentFolder)
390+
{
391+
std::string curdir = getCurrentFolder();
392+
ASSERT_NE("", curdir);
393+
394+
ASSERT_TRUE(filesystem::folderExists(curdir.c_str()));
395+
}
396+
//--------------------------------------------------------------------------------------------------
397+
TEST_F(TestFilesystemFunc, testGetFileExtention)
398+
{
399+
//test baseline
400+
{
401+
static const std::string EXPECTED = "txt";
402+
std::string ext = filesystem::getFileExtention("myFile.txt");
403+
ASSERT_EQ(EXPECTED, ext);
404+
}
405+
406+
//test empty
407+
{
408+
static const std::string EXPECTED = "";
409+
std::string ext = filesystem::getFileExtention("");
410+
ASSERT_EQ(EXPECTED, ext);
411+
}
412+
413+
//test spaces in extension
414+
{
415+
static const std::string EXPECTED = "foo bar";
416+
std::string ext = filesystem::getFileExtention("/home/my Folder/myFile.foo bar");
417+
ASSERT_EQ(EXPECTED, ext);
418+
}
419+
420+
//test filename without extension / folder name
421+
{
422+
static const std::string EXPECTED = "";
423+
std::string ext = filesystem::getFileExtention("/home/myFolder/myFile");
424+
ASSERT_EQ(EXPECTED, ext);
425+
}
426+
427+
//test folder with an extension and file without extension
428+
{
429+
static const std::string EXPECTED = "";
430+
std::string ext = filesystem::getFileExtention("/home/my.Folder/myFile");
431+
ASSERT_EQ(EXPECTED, ext);
432+
}
433+
}
434+
//--------------------------------------------------------------------------------------------------
435+
TEST_F(TestFilesystemFunc, testGetUserFriendlySize)
436+
{
437+
static const uint64_t MULTIPLICATOR_KB = 1024;
438+
static const uint64_t MULTIPLICATOR_MB = 1024*MULTIPLICATOR_KB;
439+
static const uint64_t MULTIPLICATOR_GB = 1024*MULTIPLICATOR_MB;
440+
static const uint64_t MULTIPLICATOR_TB = 1024*MULTIPLICATOR_GB;
441+
442+
{
443+
static const std::string EXPECTED = "0 bytes";
444+
std::string size = filesystem::getUserFriendlySize(0ull);
445+
ASSERT_EQ(EXPECTED, size);
446+
}
447+
{
448+
static const std::string EXPECTED = "1023 bytes";
449+
std::string size = filesystem::getUserFriendlySize(1023ull);
450+
ASSERT_EQ(EXPECTED, size);
451+
}
452+
{
453+
static const std::string EXPECTED = "1.00 KB";
454+
std::string size = filesystem::getUserFriendlySize(1ull*MULTIPLICATOR_KB);
455+
ASSERT_EQ(EXPECTED, size);
456+
}
457+
{
458+
static const std::string EXPECTED = "0.97 MB";
459+
std::string size = filesystem::getUserFriendlySize(1000ull*MULTIPLICATOR_KB);
460+
ASSERT_EQ(EXPECTED, size);
461+
}
462+
{
463+
static const std::string EXPECTED = "1.00 MB";
464+
std::string size = filesystem::getUserFriendlySize(1ull*MULTIPLICATOR_MB);
465+
ASSERT_EQ(EXPECTED, size);
466+
}
467+
{
468+
static const std::string EXPECTED = "0.97 GB";
469+
std::string size = filesystem::getUserFriendlySize(1000ull*MULTIPLICATOR_MB);
470+
ASSERT_EQ(EXPECTED, size);
471+
}
472+
{
473+
static const std::string EXPECTED = "1.00 GB";
474+
std::string size = filesystem::getUserFriendlySize(1ull*MULTIPLICATOR_GB);
475+
ASSERT_EQ(EXPECTED, size);
476+
}
477+
{
478+
static const std::string EXPECTED = "0.97 TB";
479+
std::string size = filesystem::getUserFriendlySize(1000ull*MULTIPLICATOR_GB);
480+
ASSERT_EQ(EXPECTED, size);
481+
}
482+
{
483+
static const std::string EXPECTED = "1.00 TB";
484+
std::string size = filesystem::getUserFriendlySize(1ull*MULTIPLICATOR_TB);
485+
ASSERT_EQ(EXPECTED, size);
486+
}
487+
}
488+
//--------------------------------------------------------------------------------------------------
489+
TEST_F(TestFilesystemFunc, testGetFileModifiedDate)
490+
{
491+
//assert that unit of return value is seconds
492+
{
493+
static const uint64_t EXPECTED = 3;
494+
const std::string filename1 = gTestHelper::getInstance().getTestQualifiedName() + ".1.txt";
495+
const std::string filename2 = gTestHelper::getInstance().getTestQualifiedName() + ".2.txt";
496+
ASSERT_TRUE( createDummyFile(filename1.c_str()) );
497+
nativefunc::millisleep(1000*EXPECTED + 50); //at least 3 seconds between the files
498+
ASSERT_TRUE( createDummyFile(filename2.c_str()) );
499+
500+
uint64_t time1 = filesystem::getFileModifiedDate(filename1);
501+
uint64_t time2 = filesystem::getFileModifiedDate(filename2);
502+
uint64_t diff = time2 - time1;
503+
ASSERT_GT(diff, EXPECTED-1); //allow 1 seconds of difference
504+
ASSERT_LT(diff, EXPECTED+1); //allow 1 seconds of difference
505+
}
506+
}
507+
//--------------------------------------------------------------------------------------------------
278508
} // End namespace test
279509
} // End namespace filesystem

src/common/CMakeLists.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
add_library(common STATIC argumentparser.cpp argumentparser.h common.cpp common.h cppencoder.cpp cppencoder.h environmentfunc.cpp environmentfunc.h filesystemfunc.cpp filesystemfunc.h logger.cpp logger.h stringfunc.cpp stringfunc.h)
1+
add_library(common STATIC
2+
argumentparser.cpp
3+
argumentparser.h
4+
common.cpp
5+
common.h
6+
cppencoder.cpp
7+
cppencoder.h
8+
environmentfunc.cpp
9+
environmentfunc.h
10+
filesystemfunc.cpp
11+
filesystemfunc.h
12+
logger.cpp
13+
logger.h
14+
nativefunc.cpp
15+
nativefunc.h
16+
stringfunc.cpp
17+
stringfunc.h
18+
)
219

320
if (WIN32)
421
add_definitions(-D_CRT_SECURE_NO_WARNINGS)

src/common/filesystemfunc.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,13 @@ namespace filesystem
247247
for(unsigned int i = 0; i<s.size(); i++)
248248
{
249249
const char & c = s[i];
250-
if (c == getPathSeparator() && accumulator.size() > 0)
250+
if ((c == '/' || c == '\\'))
251251
{
252-
oElements.push_back(accumulator);
253-
accumulator = "";
252+
if (accumulator.size() > 0)
253+
{
254+
oElements.push_back(accumulator);
255+
accumulator = "";
256+
}
254257
}
255258
else
256259
accumulator += c;

src/common/nativefunc.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "nativefunc.h"
2+
3+
#ifdef WIN32
4+
#ifndef WIN32_LEAN_AND_MEAN
5+
#define WIN32_LEAN_AND_MEAN 1
6+
#endif
7+
#include <windows.h> // for Sleep()
8+
#undef min
9+
#undef max
10+
#elif _POSIX_C_SOURCE >= 199309L
11+
#include <time.h> // for nanosleep()
12+
#else
13+
#include <unistd.h> // for usleep()
14+
#endif
15+
16+
17+
namespace nativefunc
18+
{
19+
20+
int millisleep(uint32_t milliseconds)
21+
{
22+
//code from https://stackoverflow.com/a/14818830 and https://stackoverflow.com/a/28827188
23+
#if defined(WIN32)
24+
SetLastError(0);
25+
Sleep(milliseconds);
26+
return GetLastError() ?-1 :0;
27+
#elif _POSIX_C_SOURCE >= 199309L
28+
/* prefer to use nanosleep() */
29+
const struct timespec ts = {
30+
(__time_t)milliseconds / 1000, /* seconds */
31+
((__syscall_slong_t)milliseconds % 1000) * 1000 * 1000 /* nano seconds */
32+
};
33+
return nanosleep(&ts, NULL);
34+
#elif _BSD_SOURCE || \
35+
(_XOPEN_SOURCE >= 500 || \
36+
_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) && \
37+
!(_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700)
38+
39+
/* else fallback to obsolte usleep() */
40+
return usleep(1000 * milliseconds);
41+
#else
42+
# error ("No millisecond sleep available for this platform!")
43+
return -1;
44+
#endif
45+
}
46+
47+
}; //nativefunc

0 commit comments

Comments
 (0)