Skip to content

Commit 729900e

Browse files
committed
Added WsjcppCore::extractDirpath
1 parent b69826c commit 729900e

File tree

8 files changed

+148
-10
lines changed

8 files changed

+148
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1313
- Added test case for replace all
1414
- Added build_simple.bat for windows
1515
- Added WsjcppCore::makeDirsPath
16+
- Added WsjcppCore::extractDirpath
1617

1718
### Changed
1819

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,21 @@ std::string sPath = WsjcppCore::doNormalizePath(".//../bin/some/../");
8484
Extract base filename from fullpath.
8585

8686
```
87-
std::string sFilename = WsjcppCore::doNormalizePath(".//../bin/some/../file.txt");
87+
std::string sFilename = WsjcppCore::extractFilename(".//../bin/some/../file.txt");
8888
```
8989

90+
Variable `sFilename` will has value `file.txt`
91+
92+
### extractDirpath
93+
94+
Extract base dir path from fullpath.
95+
96+
```
97+
std::string sDirpath = WsjcppCore::extractDirpath(".//../bin/some/../file.txt");
98+
```
99+
100+
Variable `sDirpath` will has value `.//../bin/some/..`
101+
90102
### getCurrentDirectory
91103

92104
```

src/wsjcpp_core.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,11 @@ std::string WsjcppCore::extractFilename(const std::string &sPath) {
362362
return sRet;
363363
}
364364

365-
// ---------------------------------------------------------------------
365+
std::string WsjcppCore::extractDirpath(const std::string &sFullPath) {
366+
std::vector<std::string> vDirs = WsjcppCore::split(sFullPath, "/");
367+
vDirs.pop_back();
368+
return WsjcppCore::join(vDirs, "/");
369+
}
366370

367371
std::string WsjcppCore::getCurrentDirectory() {
368372
char cwd[PATH_MAX];
@@ -372,8 +376,6 @@ std::string WsjcppCore::getCurrentDirectory() {
372376
return std::string(cwd) + "/";
373377
}
374378

375-
// ---------------------------------------------------------------------
376-
377379
long WsjcppCore::getCurrentTimeInMilliseconds() {
378380
long nTimeStart = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
379381
return nTimeStart;
@@ -423,8 +425,6 @@ std::string WsjcppCore::getThreadId() {
423425
return std::string(stream.str());
424426
}
425427

426-
// ---------------------------------------------------------------------
427-
428428
std::string WsjcppCore::formatTimeForWeb(long nTimeInSec) {
429429
std::time_t tm_ = long(nTimeInSec);
430430
// struct tm tstruct = *localtime(&tm_);
@@ -439,10 +439,6 @@ std::string WsjcppCore::formatTimeForWeb(long nTimeInSec) {
439439
return std::string(buf);
440440
}
441441

442-
443-
444-
// ---------------------------------------------------------------------
445-
446442
std::string WsjcppCore::formatTimeForFilename(long nTimeInSec) {
447443
std::time_t tm_ = long(nTimeInSec);
448444
// struct tm tstruct = *localtime(&tm_);

src/wsjcpp_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class WsjcppCore {
7272

7373
static std::string doNormalizePath(const std::string &sPath);
7474
static std::string extractFilename(const std::string &sPath);
75+
static std::string extractDirpath(const std::string &sFullPath);
7576
static std::string getCurrentDirectory();
7677

7778
static long getCurrentTimeInMilliseconds();

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_file_permissions
5050
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_string_padding.cpp")
5151
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_date_time_format.cpp")
5252
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_make_dirs_path.cpp")
53+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_extract_dirpath.cpp")
54+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_extract_filepath.cpp")
5355

5456
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
5557

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
5+
// ---------------------------------------------------------------------
6+
// UnitTestExtractDirpath
7+
8+
class UnitTestExtractDirpath : public WsjcppUnitTestBase {
9+
public:
10+
UnitTestExtractDirpath();
11+
virtual bool doBeforeTest() override;
12+
virtual void executeTest() override;
13+
virtual bool doAfterTest() override;
14+
};
15+
16+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestExtractDirpath)
17+
18+
UnitTestExtractDirpath::UnitTestExtractDirpath()
19+
: WsjcppUnitTestBase("UnitTestExtractDirpath") {
20+
}
21+
22+
// ---------------------------------------------------------------------
23+
24+
bool UnitTestExtractDirpath::doBeforeTest() {
25+
// do something before test
26+
return true;
27+
}
28+
29+
// ---------------------------------------------------------------------
30+
31+
void UnitTestExtractDirpath::executeTest() {
32+
struct LTest {
33+
LTest(
34+
const std::string &sFullpath,
35+
const std::string &sDirpath
36+
) {
37+
this->sFullpath = sFullpath;
38+
this->sDirpath = sDirpath;
39+
};
40+
std::string sFullpath;
41+
std::string sDirpath;
42+
};
43+
std::vector<LTest> tests;
44+
tests.push_back(LTest(".//../bin/some/../file.txt", ".//../bin/some/.."));
45+
46+
for (int i = 0; i < tests.size(); i++) {
47+
LTest t = tests[i];
48+
std::string sTestId = "test" + std::to_string(i);
49+
50+
compare(sTestId, WsjcppCore::extractDirpath(t.sFullpath), t.sDirpath);
51+
}
52+
}
53+
54+
// ---------------------------------------------------------------------
55+
56+
bool UnitTestExtractDirpath::doAfterTest() {
57+
// do somethig after test
58+
return true;
59+
}
60+
61+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
5+
// ---------------------------------------------------------------------
6+
// UnitTestExtractFilepath
7+
8+
class UnitTestExtractFilepath : public WsjcppUnitTestBase {
9+
public:
10+
UnitTestExtractFilepath();
11+
virtual bool doBeforeTest() override;
12+
virtual void executeTest() override;
13+
virtual bool doAfterTest() override;
14+
};
15+
16+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestExtractFilepath)
17+
18+
UnitTestExtractFilepath::UnitTestExtractFilepath()
19+
: WsjcppUnitTestBase("UnitTestExtractFilepath") {
20+
}
21+
22+
// ---------------------------------------------------------------------
23+
24+
bool UnitTestExtractFilepath::doBeforeTest() {
25+
// do something before test
26+
return true;
27+
}
28+
29+
// ---------------------------------------------------------------------
30+
31+
void UnitTestExtractFilepath::executeTest() {
32+
struct LTest {
33+
LTest(
34+
const std::string &sFullpath,
35+
const std::string &sFilename
36+
) {
37+
this->sFullpath = sFullpath;
38+
this->sFilename = sFilename;
39+
};
40+
std::string sFullpath;
41+
std::string sFilename;
42+
};
43+
std::vector<LTest> tests;
44+
tests.push_back(LTest(".//../bin/some/../file.txt", "file.txt"));
45+
46+
for (int i = 0; i < tests.size(); i++) {
47+
LTest t = tests[i];
48+
std::string sTestId = "test" + std::to_string(i);
49+
50+
compare(sTestId, WsjcppCore::extractFilename(t.sFullpath), t.sFilename);
51+
}
52+
}
53+
54+
// ---------------------------------------------------------------------
55+
56+
bool UnitTestExtractFilepath::doAfterTest() {
57+
// do somethig after test
58+
return true;
59+
}
60+
61+

wsjcpp.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,7 @@ unit-tests:
8484
description: ""
8585
- name: "MakeDirsPath"
8686
description: ""
87+
- name: "ExtractDirpath"
88+
description: ""
89+
- name: "ExtractFilepath"
90+
description: ""

0 commit comments

Comments
 (0)