Skip to content

Commit a73ad38

Browse files
committed
Added WSJCppCore::extractFilename
1 parent 640a039 commit a73ad38

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed

src/wsjcpp_core.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,34 @@ std::string WSJCppCore::doNormalizePath(const std::string & sPath) {
9696

9797
// ---------------------------------------------------------------------
9898

99+
std::string WSJCppCore::extractFilename(const std::string &sPath) {
100+
// split path by /
101+
std::vector<std::string> vNames;
102+
std::string s = "";
103+
int nStrLen = sPath.length();
104+
for (int i = 0; i < sPath.length(); i++) {
105+
if (sPath[i] == '/') {
106+
vNames.push_back(s);
107+
s = "";
108+
if (i == nStrLen-1) {
109+
vNames.push_back("");
110+
}
111+
} else {
112+
s += sPath[i];
113+
}
114+
}
115+
if (s != "") {
116+
vNames.push_back(s);
117+
}
118+
std::string sRet;
119+
if (vNames.size() > 0) {
120+
sRet = vNames[vNames.size()-1];
121+
}
122+
return sRet;
123+
}
124+
125+
// ---------------------------------------------------------------------
126+
99127
std::string WSJCppCore::getCurrentDirectory() {
100128
char cwd[PATH_MAX];
101129
if (getcwd(cwd, sizeof(cwd)) == NULL) {
@@ -492,3 +520,4 @@ void WSJCppLog::add(WSJCppColorModifier &clr, const std::string &sType, const st
492520
logFile << sLogMessage << std::endl;
493521
logFile.close();
494522
}
523+

src/wsjcpp_core.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class WSJCppCore {
1818
const std::string &sLibraryNameForExports
1919
);
2020

21-
static std::string doNormalizePath(const std::string & sPath);
21+
static std::string doNormalizePath(const std::string &sPath);
22+
static std::string extractFilename(const std::string &sPath);
2223
static std::string getCurrentDirectory();
2324

2425
static long currentTime_milliseconds();
@@ -95,11 +96,11 @@ class WSJCppLog {
9596
static void ok(const std::string &sTag, const std::string &sMessage);
9697
static void setLogDirectory(const std::string &sDirectoryPath);
9798
static void setPrefixLogFile(const std::string &sPrefixLogFile);
98-
// static nlohmann::json getLastLogs();
9999
static void initGlobalVariables();
100100

101101
private:
102102
static void add(WSJCppColorModifier &clr, const std::string &sType, const std::string &sTag, const std::string &sMessage);
103103
};
104104

105105
#endif // WSJCPP_CORE_H
106+

unit-tests/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ list (APPEND WSJCPP_INCLUDE_DIRS "src")
2323
list (APPEND WSJCPP_SOURCES "./src/main.cpp")
2424
list (APPEND WSJCPP_SOURCES "./src/unit_tests.h")
2525
list (APPEND WSJCPP_SOURCES "./src/unit_tests.cpp")
26+
list (APPEND WSJCPP_SOURCES "./src/unit_test_core_extract_filename.h")
27+
list (APPEND WSJCPP_SOURCES "./src/unit_test_core_extract_filename.cpp")
2628
list (APPEND WSJCPP_SOURCES "./src/unit_test_core_normalize_path.h")
2729
list (APPEND WSJCPP_SOURCES "./src/unit_test_core_normalize_path.cpp")
2830
list (APPEND WSJCPP_SOURCES "./src/unit_test_core_uuid.h")
2931
list (APPEND WSJCPP_SOURCES "./src/unit_test_core_uuid.cpp")
3032

33+
3134
include_directories(${WSJCPP_INCLUDE_DIRS})
3235

3336
add_executable ("unit-tests" ${WSJCPP_SOURCES})
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "unit_test_core_extract_filename.h"
2+
#include <vector>
3+
#include <iostream>
4+
#include <wsjcpp_core.h>
5+
6+
REGISTRY_UNIT_TEST(UnitTestCoreExtractFilename)
7+
8+
UnitTestCoreExtractFilename::UnitTestCoreExtractFilename()
9+
: UnitTestBase("UnitTestCoreExtractFilename") {
10+
//
11+
}
12+
13+
// ---------------------------------------------------------------------
14+
15+
void UnitTestCoreExtractFilename::init() {
16+
// nothing
17+
}
18+
19+
// ---------------------------------------------------------------------
20+
21+
bool UnitTestCoreExtractFilename::run() {
22+
std::map<std::string, std::string> mapPaths;
23+
mapPaths.insert(std::pair<std::string, std::string>("",""));
24+
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/bin/some","some"));
25+
mapPaths.insert(std::pair<std::string, std::string>("./bin/some","some"));
26+
mapPaths.insert(std::pair<std::string, std::string>(".//../bin/some","some"));
27+
mapPaths.insert(std::pair<std::string, std::string>(".//../bin/some/../",""));
28+
mapPaths.insert(std::pair<std::string, std::string>(".//../bin/some/..",".."));
29+
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/./bin/some","some"));
30+
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/../bin/some","some"));
31+
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/..//bin/some","some"));
32+
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/..//bin/some/.","."));
33+
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/..//bin/./some/",""));
34+
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/../../bin/./some/",""));
35+
mapPaths.insert(std::pair<std::string, std::string>("/usr/local/../../../bin/./some/",""));
36+
mapPaths.insert(std::pair<std::string, std::string>(".//",""));
37+
std::map<std::string, std::string>::iterator it;
38+
39+
int nCounter = 0;
40+
for ( it = mapPaths.begin(); it != mapPaths.end(); it++ ) {
41+
std::string sSource = it->first;
42+
std::string sExpected = it->second;
43+
std::string sGot = WSJCppCore::extractFilename(sSource);
44+
if (sGot != sExpected) {
45+
nCounter++;
46+
WSJCppLog::err(TAG, "Wrong extract filename for: '" + sSource + "'.\n\t Got: '" + sGot + "', but expected: '" + sExpected + "'");
47+
}
48+
49+
}
50+
return nCounter == 0;
51+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef UNIT_TEST_CORE_EXCTRACT_FILENAME_H
2+
#define UNIT_TEST_CORE_EXCTRACT_FILENAME_H
3+
4+
#include <unit_tests.h>
5+
6+
class UnitTestCoreExtractFilename : public UnitTestBase {
7+
public:
8+
UnitTestCoreExtractFilename();
9+
virtual void init();
10+
virtual bool run();
11+
};
12+
13+
#endif // UNIT_TEST_CORE_EXCTRACT_FILENAME_H

0 commit comments

Comments
 (0)