Skip to content

Commit 8f1dccc

Browse files
committed
Fixed #21: moved resource manager to wsjcpp core
1 parent 5a80b45 commit 8f1dccc

File tree

10 files changed

+156
-167
lines changed

10 files changed

+156
-167
lines changed

CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ 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")
2220

2321
list (APPEND WSJCPP_SOURCES "src/main.cpp")
2422

src/wsjcpp_core.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,4 +1213,122 @@ void WsjcppLog::add(WsjcppColorModifier &clr, const std::string &sType, const st
12131213
}
12141214
}
12151215

1216+
// ---------------------------------------------------------------------
1217+
// WsjcppResourceFile
1218+
1219+
WsjcppResourceFile::WsjcppResourceFile() {
1220+
WsjcppResourcesManager::add(this);
1221+
}
1222+
1223+
// ---------------------------------------------------------------------
1224+
1225+
1226+
// ---------------------------------------------------------------------
1227+
// WsjcppResourcesManager
1228+
1229+
std::vector<WsjcppResourceFile*> *g_pWsjcppResourceFiles = nullptr;
1230+
1231+
void WsjcppResourcesManager::initGlobalVariables() {
1232+
if (g_pWsjcppResourceFiles == nullptr) {
1233+
g_pWsjcppResourceFiles = new std::vector<WsjcppResourceFile*>();
1234+
}
1235+
}
1236+
1237+
// ---------------------------------------------------------------------
1238+
1239+
void WsjcppResourcesManager::add(WsjcppResourceFile* pStorage) {
1240+
WsjcppResourcesManager::initGlobalVariables();
1241+
g_pWsjcppResourceFiles->push_back(pStorage);
1242+
}
1243+
1244+
// ---------------------------------------------------------------------
1245+
1246+
bool WsjcppResourcesManager::has(const std::string &sFilename) {
1247+
WsjcppResourcesManager::initGlobalVariables();
1248+
for (int i = 0; i < WsjcppResourcesManager::list().size(); i++) {
1249+
if (WsjcppResourcesManager::list()[i]->getFilename() == sFilename) {
1250+
return true;
1251+
}
1252+
}
1253+
return false;
1254+
}
1255+
1256+
// ---------------------------------------------------------------------
1257+
1258+
WsjcppResourceFile* WsjcppResourcesManager::get(const std::string &sFilename) {
1259+
WsjcppResourcesManager::initGlobalVariables();
1260+
for (int i = 0; i < WsjcppResourcesManager::list().size(); i++) {
1261+
if (WsjcppResourcesManager::list()[i]->getFilename() == sFilename) {
1262+
return WsjcppResourcesManager::list()[i];
1263+
}
1264+
}
1265+
return nullptr;
1266+
}
1267+
1268+
// ---------------------------------------------------------------------
1269+
1270+
const std::vector<WsjcppResourceFile*> &WsjcppResourcesManager::list() {
1271+
return *g_pWsjcppResourceFiles;
1272+
}
1273+
1274+
// ---------------------------------------------------------------------
1275+
1276+
/*
1277+
bool WsjcppResourcesManager::make(const std::string &sWorkspace) {
1278+
if (!WsjcppResourcesManager::createFolders(sWorkspace)) {
1279+
return false;
1280+
}
1281+
return WsjcppResourcesManager::extractFiles(sWorkspace);
1282+
}
1283+
1284+
// ---------------------------------------------------------------------
1285+
1286+
bool WsjcppResourcesManager::createFolders(const std::string &sWorkspace) {
1287+
// prepare folders
1288+
std::vector<std::string> vCreateDirs;
1289+
vCreateDirs.push_back(sWorkspace + "/logs");
1290+
vCreateDirs.push_back(sWorkspace + "/teams");
1291+
vCreateDirs.push_back(sWorkspace + "/checkers");
1292+
vCreateDirs.push_back(sWorkspace + "/html");
1293+
vCreateDirs.push_back(sWorkspace + "/html/css");
1294+
vCreateDirs.push_back(sWorkspace + "/html/js");
1295+
vCreateDirs.push_back(sWorkspace + "/html/images");
1296+
vCreateDirs.push_back(sWorkspace + "/html/images/teams");
1297+
vCreateDirs.push_back(sWorkspace + "/html/images/states");
1298+
1299+
for(int i = 0; i < vCreateDirs.size(); i++) {
1300+
std::string sPath = vCreateDirs[i];
1301+
// check dir existing
1302+
if (!FS::dirExists(sPath)) {
1303+
// try make dir
1304+
if (!FS::makeDir(sPath)) {
1305+
std::cout << "Could not create folder " << sPath << std::endl;
1306+
return false;
1307+
} else {
1308+
std::cout << "Created folder " << sPath << std::endl;
1309+
}
1310+
}
1311+
}
1312+
return true;
1313+
}
1314+
1315+
// ---------------------------------------------------------------------
1316+
1317+
bool WsjcppResourcesManager::extractFiles(const std::string &sWorkspace) {
1318+
// TODO mkdir -p for files
1319+
const std::vector<WsjcppResourceFile*> list = WsjcppResourcesManager::list();
1320+
for(int i = 0; i < list.size(); i++) {
1321+
std::string sFilename = sWorkspace + "/" + list[i]->filename();
1322+
if (!FS::fileExists(sFilename)) {
1323+
if (!FS::writeFile(sFilename, list[i]->buffer(), list[i]->bufferSize())) {
1324+
std::cout << "Could not write file " << sFilename << std::endl;
1325+
return false;
1326+
} else {
1327+
std::cout << "Created file " << sFilename << std::endl;
1328+
}
1329+
}
1330+
}
1331+
return true;
1332+
}
1333+
*/
12161334

src/wsjcpp_core.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,42 @@ class WsjcppLog {
194194
static void add(WsjcppColorModifier &clr, const std::string &sType, const std::string &sTag, const std::string &sMessage);
195195
};
196196

197+
// ---------------------------------------------------------------------
198+
// WsjcppResourceFile
199+
200+
class WsjcppResourceFile {
201+
public:
202+
WsjcppResourceFile();
203+
virtual const std::string &getFilename() const = 0;
204+
virtual const std::string &getPackAs() const = 0;
205+
virtual int getBufferSize() const = 0;
206+
virtual const char *getBuffer() const = 0;
207+
};
208+
209+
210+
// ---------------------------------------------------------------------
211+
// WsjcppResourcesManager
212+
213+
extern std::vector<WsjcppResourceFile*> *g_pWsjcppResourceFiles;
214+
215+
class WsjcppResourcesManager {
216+
public:
217+
static void initGlobalVariables();
218+
static void add(WsjcppResourceFile*);
219+
static const std::vector<WsjcppResourceFile*> &list();
220+
static bool has(const std::string &sFilename);
221+
static WsjcppResourceFile* get(const std::string &sFilename);
222+
static bool make(const std::string &sWorkspace);
223+
// static bool createFolders(const std::string &sWorkspace);
224+
// static bool extractFiles(const std::string &sWorkspace);
225+
};
226+
227+
// ---------------------------------------------------------------------
228+
// Registry WsjcppResourceFile
229+
#define REGISTRY_WSJCPP_RESOURCE_FILE( classname ) \
230+
static classname * pRegistryWsjcppResourceFile ## classname = new classname(); \
231+
232+
197233
#endif // WSJCPP_CORE_H
198234

199235

src/wsjcpp_resources_manager.cpp

Lines changed: 0 additions & 117 deletions
This file was deleted.

src/wsjcpp_resources_manager.h

Lines changed: 0 additions & 37 deletions
This file was deleted.

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ 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")
3129

3230
# unit-tests
3331
list (APPEND WSJCPP_INCLUDE_DIRS "src")

unit-tests.wsjcpp/build_simple.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
if [ ! -d tmp ]; then
4-
mkdir -p tmp
4+
mkdir -p tmp
55
fi
66

77
cd tmp

unit-tests.wsjcpp/src/resources.wsjcpp/html_images_points_svg_path_835d61.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <wsjcpp_resources_manager.h>
1+
#include <wsjcpp_core.h>
22

33
class RES_html_images_points_svg : public WsjcppResourceFile {
44
public:

unit-tests.wsjcpp/src/unit_test_test_resources.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "unit_test_test_resources.h"
22
#include <vector>
33
#include <wsjcpp_core.h>
4-
#include <wsjcpp_resources_manager.h>
54

65
REGISTRY_WSJCPP_UNIT_TEST(UnitTestTestResources)
76

wsjcpp.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ distribution:
3939
- source-file: "scripts.wsjcpp/generate.Class"
4040
target-file: "generate.Class"
4141
type: "safe-scripting-generate"
42-
- source-file: "src/wsjcpp_resources_manager.h"
43-
target-file: "wsjcpp_resources_manager.h"
44-
type: "source-code"
45-
- source-file: "src/wsjcpp_resources_manager.cpp"
46-
target-file: "wsjcpp_resources_manager.cpp"
47-
type: "source-code"
4842

4943
unit-tests:
5044
cases:

0 commit comments

Comments
 (0)