Skip to content

Commit b69826c

Browse files
committed
Implemented WsjcppCore::makeDirsPath
1 parent 10e9bfe commit b69826c

File tree

7 files changed

+123
-30
lines changed

7 files changed

+123
-30
lines changed

CHANGELOG.md

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

1617
### Changed
1718

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,16 @@ if (WsjcppCore::makeDir(sDirname)) {
181181
}
182182
```
183183

184+
### makeDirsPath
185+
186+
Create a new directories full path
187+
```
188+
std::string sDirsPath = "./data/dir1/dir1/dir3";
189+
if (WsjcppCore::makeDirsPath(sDirname)) {
190+
std::cout << " Created '" << sDirsPath << "'" << std::endl;
191+
}
192+
```
193+
184194
### writeFile
185195

186196
```

src/wsjcpp_core.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,25 @@ bool WsjcppCore::makeDir(const std::string &sDirname) {
549549
return true;
550550
}
551551

552-
// ---------------------------------------------------------------------
552+
bool WsjcppCore::makeDirsPath(const std::string &sDirname) {
553+
std::string sDirpath = WsjcppCore::doNormalizePath(sDirname);
554+
std::vector<std::string> vDirs = WsjcppCore::split(sDirpath, "/");
555+
std::string sDirpath2 = "";
556+
for (int i = 0; i < vDirs.size(); i++) {
557+
if (vDirs[i] == "") {
558+
continue;
559+
}
560+
sDirpath2 += vDirs[i] + "/";
561+
if (WsjcppCore::dirExists(sDirpath2)) {
562+
continue;
563+
} else {
564+
if (!WsjcppCore::makeDir(sDirpath2)) {
565+
return false;
566+
}
567+
}
568+
}
569+
return true;
570+
}
553571

554572
bool WsjcppCore::writeFile(const std::string &sFilename, const std::string &sContent) {
555573

src/wsjcpp_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class WsjcppCore {
9090
static std::vector<std::string> listOfFiles(const std::string &sDirname);
9191
static std::vector<std::string> getListOfFiles(const std::string &sDirname);
9292
static bool makeDir(const std::string &sDirname);
93+
static bool makeDirsPath(const std::string &sDirname);
9394
static bool writeFile(const std::string &sFilename, const std::string &sContent);
9495
static bool readTextFile(const std::string &sFilename, std::string &sOutputContent);
9596
static bool readFileToBuffer(const std::string &sFilename, char *pBuffer[], int &nBufferSize);

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Automaticly generated by wsjcpp@v0.1.7
1+
# Automaticly generated by wsjcpp@v0.2.2
22
cmake_minimum_required(VERSION 3.0)
33

44
project(unit-tests C CXX)
@@ -29,46 +29,27 @@ list (APPEND WSJCPP_SOURCES "../src/wsjcpp_unit_tests_main.cpp")
2929

3030
# unit-tests
3131
list (APPEND WSJCPP_INCLUDE_DIRS "src")
32-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_core_normalize_path.h")
3332
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_core_normalize_path.cpp")
34-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_core_extract_filename.h")
3533
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_core_extract_filename.cpp")
36-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_to_upper.h")
3734
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_to_upper.cpp")
38-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_create_uuid.h")
3935
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_create_uuid.cpp")
40-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_env.h")
4136
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_env.cpp")
42-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_to_lower.h")
4337
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_to_lower.cpp")
44-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_replace_all.h")
4538
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_replace_all.cpp")
46-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_decode_uri_component.h")
4739
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_decode_uri_component.cpp")
48-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_encode_uri_component.h")
4940
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_encode_uri_component.cpp")
50-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_uint2_hex_string.h")
5141
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_uint2_hex_string.cpp")
52-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_split.h")
5342
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_split.cpp")
54-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_create_empty_file.h")
5543
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_create_empty_file.cpp")
56-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_read_file_to_buffer.h")
5744
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_read_file_to_buffer.cpp")
58-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_join.h")
5945
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_join.cpp")
60-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_human_size_bytes.h")
6146
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_human_size_bytes.cpp")
62-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_test_resources.h")
6347
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_test_resources.cpp")
64-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_list_of_dirs.h")
6548
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_list_of_dirs.cpp")
66-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_file_permissions.h")
6749
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_file_permissions.cpp")
68-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_string_padding.h")
6950
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_string_padding.cpp")
70-
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_date_time_format.h")
7151
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_date_time_format.cpp")
52+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_make_dirs_path.cpp")
7253

7354
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
7455

@@ -79,11 +60,3 @@ add_executable ("unit-tests" ${WSJCPP_SOURCES})
7960

8061
target_link_libraries("unit-tests" -lpthread ${WSJCPP_LIBRARIES} )
8162

82-
install(
83-
TARGETS
84-
"unit-tests"
85-
RUNTIME DESTINATION
86-
/usr/bin
87-
)
88-
89-
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
5+
// ---------------------------------------------------------------------
6+
// UnitTestMakeDirsPath
7+
8+
class UnitTestMakeDirsPath : public WsjcppUnitTestBase {
9+
public:
10+
UnitTestMakeDirsPath();
11+
virtual bool doBeforeTest() override;
12+
virtual void executeTest() override;
13+
virtual bool doAfterTest() override;
14+
};
15+
16+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestMakeDirsPath)
17+
18+
UnitTestMakeDirsPath::UnitTestMakeDirsPath()
19+
: WsjcppUnitTestBase("UnitTestMakeDirsPath") {
20+
}
21+
22+
// ---------------------------------------------------------------------
23+
24+
bool UnitTestMakeDirsPath::doBeforeTest() {
25+
// do something before test
26+
return true;
27+
}
28+
29+
// ---------------------------------------------------------------------
30+
31+
void UnitTestMakeDirsPath::executeTest() {
32+
33+
if (WsjcppCore::dirExists("./data/makedirpaths")) {
34+
WsjcppCore::recoursiveRemoveDir("./data/makedirpaths");
35+
}
36+
37+
struct LTest {
38+
LTest(
39+
const std::string &sDir,
40+
const std::vector<std::string> &vSubPaths
41+
) {
42+
this->sDir = sDir;
43+
this->vSubPaths = vSubPaths;
44+
};
45+
std::string sDir;
46+
std::vector<std::string> vSubPaths;
47+
};
48+
std::vector<LTest> tests;
49+
tests.push_back(LTest("./data/makedirpaths/dir1/dir2", {
50+
"./data",
51+
"./data/makedirpaths",
52+
"./data/makedirpaths/dir1",
53+
"./data/makedirpaths/dir1/dir2"
54+
}));
55+
tests.push_back(LTest("./data/makedirpaths/dir1/dir3", {
56+
"./data",
57+
"./data/makedirpaths",
58+
"./data/makedirpaths/dir1",
59+
"./data/makedirpaths/dir1/dir3"
60+
}));
61+
tests.push_back(LTest("./data/makedirpaths/dir1/dir4/", {
62+
"./data",
63+
"./data/makedirpaths",
64+
"./data/makedirpaths/dir1",
65+
"./data/makedirpaths/dir1/dir4"
66+
}));
67+
68+
for (int i = 0; i < tests.size(); i++) {
69+
std::string sPath = tests[i].sDir;
70+
bool bResult = WsjcppCore::makeDirsPath(sPath);
71+
std::string sPathMark = "path" + std::to_string(i+1);
72+
compare("Create " + sPathMark, bResult, true);
73+
std::vector<std::string> vPaths = tests[i].vSubPaths;
74+
for (int x = 0; x < vPaths.size(); x++) {
75+
compare("Check " + sPathMark + " " + vPaths[x], WsjcppCore::dirExists(vPaths[x]), true);
76+
}
77+
}
78+
WsjcppCore::recoursiveRemoveDir("./data/makedirpaths");
79+
}
80+
81+
// ---------------------------------------------------------------------
82+
83+
bool UnitTestMakeDirsPath::doAfterTest() {
84+
// do somethig after test
85+
return true;
86+
}
87+
88+

wsjcpp.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,5 @@ unit-tests:
8282
description: ""
8383
- name: "DateTimeFormat"
8484
description: ""
85+
- name: "MakeDirsPath"
86+
description: ""

0 commit comments

Comments
 (0)