Skip to content

Commit 375fbc0

Browse files
committed
Implemented WsjcppResourcesManager
1 parent 8884d81 commit 375fbc0

11 files changed

+318
-4
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ list (APPEND WSJCPP_INCLUDE_DIRS "src")
1717

1818
list (APPEND WSJCPP_SOURCES "src/wsjcpp_core.h")
1919
list (APPEND WSJCPP_SOURCES "src/wsjcpp_core.cpp")
20+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_resources_manager.h")
21+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_resources_manager.cpp")
22+
2023
list (APPEND WSJCPP_SOURCES "src/main.cpp")
2124

2225
include_directories(${WSJCPP_INCLUDE_DIRS})

src.wsjcpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Automaticly generated by [email protected]
22
cmake_minimum_required(VERSION 3.0)
33

4-
add_definitions(-DWSJCPP_VERSION="v0.1.2")
4+
add_definitions(-DWSJCPP_VERSION="v0.1.3")
55
add_definitions(-DWSJCPP_NAME="wsjcpp-core")
66

77
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")

src/wsjcpp_resources_manager.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#include "wsjcpp_resources_manager.h"
2+
3+
// ---------------------------------------------------------------------
4+
5+
WsjcppResourceFile::WsjcppResourceFile() {
6+
WsjcppResourcesManager::add(this);
7+
}
8+
9+
// ---------------------------------------------------------------------
10+
11+
std::vector<WsjcppResourceFile*> *g_pWsjcppResourceFiles = nullptr;
12+
13+
// ---------------------------------------------------------------------
14+
15+
void WsjcppResourcesManager::initGlobalVariables() {
16+
if (g_pWsjcppResourceFiles == nullptr) {
17+
g_pWsjcppResourceFiles = new std::vector<WsjcppResourceFile*>();
18+
}
19+
}
20+
21+
// ---------------------------------------------------------------------
22+
23+
void WsjcppResourcesManager::add(WsjcppResourceFile* pStorage) {
24+
WsjcppResourcesManager::initGlobalVariables();
25+
g_pWsjcppResourceFiles->push_back(pStorage);
26+
}
27+
28+
// ---------------------------------------------------------------------
29+
30+
bool WsjcppResourcesManager::has(const std::string &sFilename) {
31+
WsjcppResourcesManager::initGlobalVariables();
32+
for (int i = 0; i < WsjcppResourcesManager::list().size(); i++) {
33+
if (WsjcppResourcesManager::list()[i]->getFilename() == sFilename) {
34+
return true;
35+
}
36+
}
37+
return false;
38+
}
39+
40+
// ---------------------------------------------------------------------
41+
42+
WsjcppResourceFile* WsjcppResourcesManager::get(const std::string &sFilename) {
43+
WsjcppResourcesManager::initGlobalVariables();
44+
for (int i = 0; i < WsjcppResourcesManager::list().size(); i++) {
45+
if (WsjcppResourcesManager::list()[i]->getFilename() == sFilename) {
46+
return WsjcppResourcesManager::list()[i];
47+
}
48+
}
49+
return nullptr;
50+
}
51+
52+
// ---------------------------------------------------------------------
53+
54+
const std::vector<WsjcppResourceFile*> &WsjcppResourcesManager::list() {
55+
return *g_pWsjcppResourceFiles;
56+
}
57+
58+
// ---------------------------------------------------------------------
59+
60+
/*
61+
bool WsjcppResourcesManager::make(const std::string &sWorkspace) {
62+
if (!WsjcppResourcesManager::createFolders(sWorkspace)) {
63+
return false;
64+
}
65+
return WsjcppResourcesManager::extractFiles(sWorkspace);
66+
}
67+
68+
// ---------------------------------------------------------------------
69+
70+
bool WsjcppResourcesManager::createFolders(const std::string &sWorkspace) {
71+
// prepare folders
72+
std::vector<std::string> vCreateDirs;
73+
vCreateDirs.push_back(sWorkspace + "/logs");
74+
vCreateDirs.push_back(sWorkspace + "/teams");
75+
vCreateDirs.push_back(sWorkspace + "/checkers");
76+
vCreateDirs.push_back(sWorkspace + "/html");
77+
vCreateDirs.push_back(sWorkspace + "/html/css");
78+
vCreateDirs.push_back(sWorkspace + "/html/js");
79+
vCreateDirs.push_back(sWorkspace + "/html/images");
80+
vCreateDirs.push_back(sWorkspace + "/html/images/teams");
81+
vCreateDirs.push_back(sWorkspace + "/html/images/states");
82+
83+
for(int i = 0; i < vCreateDirs.size(); i++) {
84+
std::string sPath = vCreateDirs[i];
85+
// check dir existing
86+
if (!FS::dirExists(sPath)) {
87+
// try make dir
88+
if (!FS::makeDir(sPath)) {
89+
std::cout << "Could not create folder " << sPath << std::endl;
90+
return false;
91+
} else {
92+
std::cout << "Created folder " << sPath << std::endl;
93+
}
94+
}
95+
}
96+
return true;
97+
}
98+
99+
// ---------------------------------------------------------------------
100+
101+
bool WsjcppResourcesManager::extractFiles(const std::string &sWorkspace) {
102+
// TODO mkdir -p for files
103+
const std::vector<WsjcppResourceFile*> list = WsjcppResourcesManager::list();
104+
for(int i = 0; i < list.size(); i++) {
105+
std::string sFilename = sWorkspace + "/" + list[i]->filename();
106+
if (!FS::fileExists(sFilename)) {
107+
if (!FS::writeFile(sFilename, list[i]->buffer(), list[i]->bufferSize())) {
108+
std::cout << "Could not write file " << sFilename << std::endl;
109+
return false;
110+
} else {
111+
std::cout << "Created file " << sFilename << std::endl;
112+
}
113+
}
114+
}
115+
return true;
116+
}
117+
*/

src/wsjcpp_resources_manager.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef WSJCPP_RESOURCES_MANAGER_H
2+
#define WSJCPP_RESOURCES_MANAGER_H
3+
4+
#include <string>
5+
#include <stdio.h>
6+
#include <iostream>
7+
#include <vector>
8+
9+
class WsjcppResourceFile {
10+
public:
11+
WsjcppResourceFile();
12+
virtual const std::string &getFilename() = 0;
13+
virtual const int getBufferSize() = 0;
14+
virtual const char *getBuffer() = 0;
15+
};
16+
17+
extern std::vector<WsjcppResourceFile*> *g_pWsjcppResourceFiles;
18+
19+
class WsjcppResourcesManager {
20+
public:
21+
static void initGlobalVariables();
22+
static void add(WsjcppResourceFile*);
23+
static const std::vector<WsjcppResourceFile*> &list();
24+
static bool has(const std::string &sFilename);
25+
static WsjcppResourceFile* get(const std::string &sFilename);
26+
static bool make(const std::string &sWorkspace);
27+
// static bool createFolders(const std::string &sWorkspace);
28+
// static bool extractFiles(const std::string &sWorkspace);
29+
};
30+
31+
// ---------------------------------------------------------------------
32+
// Registry WsjcppResourceFile
33+
#define REGISTRY_WSJCPP_RESOURCE_FILE( classname ) \
34+
static classname * pRegistryWsjcppResourceFile ## classname = new classname(); \
35+
36+
#endif // WSJCPP_RESOURCES_MANAGER_H

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
cmake_minimum_required(VERSION 3.0)
33

44
project(unit-tests C CXX)
5-
add_definitions(-DWSJCPP_VERSION="ut-v0.1.2")
5+
add_definitions(-DWSJCPP_VERSION="ut-v0.1.3")
66
add_definitions(-DWSJCPP_NAME="unit-tests-wsjcpp-core")
77

88
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
@@ -19,13 +19,15 @@ set (WSJCPP_SOURCES "")
1919
find_package(Threads REQUIRED)
2020
list (APPEND WSJCPP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
2121

22-
# wsjcpp-core:v0.1.2
22+
# wsjcpp-core:v0.1.3
2323
list (APPEND WSJCPP_INCLUDE_DIRS "../src")
2424
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_core.cpp")
2525
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_core.h")
2626
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_unit_tests.cpp")
2727
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_unit_tests.h")
2828
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_unit_tests_main.cpp")
29+
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_resources_manager.h")
30+
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_resources_manager.cpp")
2931

3032
# unit-tests
3133
list (APPEND WSJCPP_INCLUDE_DIRS "src")
@@ -61,6 +63,8 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_join.h")
6163
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_join.cpp")
6264
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_human_size_bytes.h")
6365
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_human_size_bytes.cpp")
66+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_test_resources.h")
67+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_test_resources.cpp")
6468

6569
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
6670

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
## You can here write some custom includes
22

3+
list (APPEND WSJCPP_INCLUDE_DIRS "./src/resources.wsjcpp")
4+
list (APPEND WSJCPP_SOURCES "./src/resources.wsjcpp/html_images_points_svg_path_835d61.cpp")
5+
list (APPEND WSJCPP_SOURCES "./src/resources.wsjcpp/html_images_points_svg_path_835d61.h")
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <html_images_points_svg_path_835d61.h>
2+
3+
REGISTRY_WSJCPP_RESOURCE_FILE(RES_html_images_points_svg)
4+
5+
const std::string &RES_html_images_points_svg::getFilename() {
6+
static const std::string s = "html/images/points.svg";
7+
return s;
8+
}
9+
10+
const int RES_html_images_points_svg::getBufferSize() {
11+
return 2979;
12+
}
13+
14+
const char *RES_html_images_points_svg::getBuffer() {
15+
static const std::string sRet = // size: 2979
16+
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
17+
"<!-- Created with Inkscape (http://www.inkscape.org/) -->\n"
18+
"\n"
19+
"<svg\n"
20+
" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n"
21+
" xmlns:cc=\"http://creativecommons.org/ns#\"\n"
22+
" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n"
23+
" xmlns:svg=\"http://www.w3.org/2000/svg\"\n"
24+
" xmlns=\"http://www.w3.org/2000/svg\"\n"
25+
" xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\"\n"
26+
" xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\"\n"
27+
" width=\"100\"\n"
28+
" height=\"100\"\n"
29+
" viewBox=\"0 0 26.458333 26.458334\"\n"
30+
" version=\"1.1\"\n"
31+
" id=\"svg8\"\n"
32+
" inkscape:version=\"0.92.3 (2405546, 2018-03-11)\"\n"
33+
" sodipodi:docname=\"points.svg\">\n"
34+
" <defs\n"
35+
" id=\"defs2\" />\n"
36+
" <sodipodi:namedview\n"
37+
" id=\"base\"\n"
38+
" pagecolor=\"#ffffff\"\n"
39+
" bordercolor=\"#666666\"\n"
40+
" borderopacity=\"1.0\"\n"
41+
" inkscape:pageopacity=\"0.0\"\n"
42+
" inkscape:pageshadow=\"2\"\n"
43+
" inkscape:zoom=\"5.6\"\n"
44+
" inkscape:cx=\"59.537015\"\n"
45+
" inkscape:cy=\"50.060612\"\n"
46+
" inkscape:document-units=\"mm\"\n"
47+
" inkscape:current-layer=\"layer1\"\n"
48+
" showgrid=\"false\"\n"
49+
" units=\"px\"\n"
50+
" inkscape:snap-global=\"false\"\n"
51+
" inkscape:window-width=\"1853\"\n"
52+
" inkscape:window-height=\"1025\"\n"
53+
" inkscape:window-x=\"67\"\n"
54+
" inkscape:window-y=\"27\"\n"
55+
" inkscape:window-maximized=\"1\" />\n"
56+
" <metadata\n"
57+
" id=\"metadata5\">\n"
58+
" <rdf:RDF>\n"
59+
" <cc:Work\n"
60+
" rdf:about=\"\">\n"
61+
" <dc:format>image/svg+xml</dc:format>\n"
62+
" <dc:type\n"
63+
" rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\" />\n"
64+
" <dc:title></dc:title>\n"
65+
" </cc:Work>\n"
66+
" </rdf:RDF>\n"
67+
" </metadata>\n"
68+
" <g\n"
69+
" inkscape:label=\"Layer 1\"\n"
70+
" inkscape:groupmode=\"layer\"\n"
71+
" id=\"layer1\"\n"
72+
" transform=\"translate(0,-270.54165)\">\n"
73+
" <path\n"
74+
" style=\"fill:#ffe600;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.33430848px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1\"\n"
75+
" d=\"m 4.522599,289.63405 2.3282191,-7.04435 -6.02949182,-4.17886 7.16375252,-0.1791 2.4476152,-6.86525 2.208825,7.04435 7.342847,0.0598 -5.850398,4.17886 2.387919,7.22345 -6.029495,-4.47734 z\"\n"
76+
" id=\"path826-3\"\n"
77+
" inkscape:connector-curvature=\"0\" />\n"
78+
" <path\n"
79+
" style=\"fill:#ff6700;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1\"\n"
80+
" d=\"m 13.708745,289.40582 1.842634,-5.57514 -4.77195,-3.3073 5.669643,-0.14174 1.937128,-5.4334 1.74814,5.57514 5.811384,0.0473 -4.630208,3.30729 1.889881,5.71689 -4.77195,-3.54352 z\"\n"
81+
" id=\"path826\"\n"
82+
" inkscape:connector-curvature=\"0\" />\n"
83+
" <path\n"
84+
" style=\"fill:#4be600;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1\"\n"
85+
" d=\"m 7.3942979,296.36898 1.8426337,-5.57514 -4.7719497,-3.3073 5.6696431,-0.14174 1.937128,-5.4334 1.74814,5.57514 5.811384,0.0473 -4.630208,3.30729 1.889881,5.71689 -4.77195,-3.54352 z\"\n"
86+
" id=\"path826-6\"\n"
87+
" inkscape:connector-curvature=\"0\" />\n"
88+
" </g>\n"
89+
"</svg>\n"
90+
;
91+
return sRet.c_str();
92+
} //::buffer()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <wsjcpp_resources_manager.h>
2+
3+
class RES_html_images_points_svg : public WsjcppResourceFile {
4+
public:
5+
virtual const std::string &getFilename();
6+
virtual const int getBufferSize();
7+
virtual const char *getBuffer();
8+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "unit_test_test_resources.h"
2+
#include <vector>
3+
#include <wsjcpp_core.h>
4+
#include <wsjcpp_resources_manager.h>
5+
6+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestTestResources)
7+
8+
UnitTestTestResources::UnitTestTestResources()
9+
: WsjcppUnitTestBase("UnitTestTestResources") {
10+
}
11+
12+
// ---------------------------------------------------------------------
13+
14+
void UnitTestTestResources::init() {
15+
// nothing
16+
}
17+
18+
// ---------------------------------------------------------------------
19+
20+
bool UnitTestTestResources::run() {
21+
bool bTestSuccess = true;
22+
// std::vector<ResourceFile*> &list();
23+
WsjcppResourceFile* pFile = WsjcppResourcesManager::get("html/images/points.svg");
24+
compareN(bTestSuccess, "getBufferSize", pFile->getBufferSize(), 2979);
25+
compareS(bTestSuccess, "getFilename", pFile->getFilename(), "html/images/points.svg");
26+
return bTestSuccess;
27+
}
28+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef UNIT_TEST_TEST_RESOURCES_H
2+
#define UNIT_TEST_TEST_RESOURCES_H
3+
4+
#include <wsjcpp_unit_tests.h>
5+
6+
// Description: TODO
7+
class UnitTestTestResources : public WsjcppUnitTestBase {
8+
public:
9+
UnitTestTestResources();
10+
virtual void init();
11+
virtual bool run();
12+
};
13+
14+
#endif // UNIT_TEST_TEST_RESOURCES_H
15+

0 commit comments

Comments
 (0)