Skip to content

Commit e0d9988

Browse files
committed
Updated
1 parent f6b7102 commit e0d9988

File tree

10 files changed

+191
-88
lines changed

10 files changed

+191
-88
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ wsjcpp-yaml
22
tmp/*
33
.wsjcpp-yaml-logs/*
44
.vscode
5+
.wsjcpp-cache/*
6+
.wsjcpp-logs/*
7+
.wsjcpp/*
58

69
# Prerequisites
710
*.d

src.wsjcpp/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set (WSJCPP_LIBRARIES "")
1010
set (WSJCPP_INCLUDE_DIRS "")
1111
set (WSJCPP_SOURCES "")
1212

13-
list (APPEND WSJCPP_INCLUDE_DIRS "src.wsjcpp/wsjcpp-core/")
14-
list (APPEND WSJCPP_SOURCES "src.wsjcpp/wsjcpp-core/wsjcpp_core.h")
15-
list (APPEND WSJCPP_SOURCES "src.wsjcpp/wsjcpp-core/wsjcpp_core.cpp")
13+
# wsjcpp/wsjcpp-core
14+
list (APPEND WSJCPP_INCLUDE_DIRS "src.wsjcpp/wsjcpp_wsjcpp_core/")
15+
list (APPEND WSJCPP_SOURCES "src.wsjcpp/wsjcpp_wsjcpp_core/wsjcpp_core.h")
16+
list (APPEND WSJCPP_SOURCES "src.wsjcpp/wsjcpp_wsjcpp_core/wsjcpp_core.cpp")

src.wsjcpp/wsjcpp-core/wsjcpp.hold.yml

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
wsjcpp_version: v0.0.1
2+
cmake_cxx_standard: 11
3+
cmake_minimum_required: 3.0
4+
5+
name: wsjcpp/wsjcpp-core
6+
version: v0.0.2
7+
description: Basic Utils for wsjcpp
8+
issues: https://github.com/wsjcpp/wsjcpp-core/issues
9+
repositories:
10+
- type: main
11+
url: "https://github.com/wsjcpp/wsjcpp-core"
12+
keywords:
13+
- c++
14+
- wsjcpp
15+
16+
authors:
17+
- name: Evgenii Sopov
18+
19+
20+
required-libraries:
21+
- pthread
22+
23+
distribution:
24+
- source-file: src/wsjcpp_core.cpp
25+
target-file: wsjcpp_core.cpp
26+
sha1: "bede8216bc5aaf105e86a9572c3d9066a71026b7"
27+
type: "source-code"
28+
- source-file: src/wsjcpp_core.h
29+
target-file: wsjcpp_core.h
30+
sha1: "b3d842d01fe13c5463fc3af61c5408ac437c65aa"
31+
type: "source-code"
32+
33+
unit-tests:
34+
folder: unit-tests # default
35+
files:
36+
- unit-tests/src/main.cpp
37+
- unit-tests/src/unit_tests.h
38+
- unit-tests/src/unit_tests.cpp
39+

src.wsjcpp/wsjcpp-core/wsjcpp_core.cpp renamed to src.wsjcpp/wsjcpp_wsjcpp_core/wsjcpp_core.cpp

Lines changed: 56 additions & 24 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) {
@@ -275,20 +303,20 @@ bool WSJCppCore::makeDir(const std::string &sDirname) {
275303
std::cout << "FAILED create folder " << sDirname << std::endl;
276304
return false;
277305
}
278-
std::cout << "nStatus: " << nStatus << std::endl;
306+
// std::cout << "nStatus: " << nStatus << std::endl;
279307
return true;
280308
}
281309

282310
// ---------------------------------------------------------------------
283311

284312
bool WSJCppCore::writeFile(const std::string &sFilename, const std::string &sContent) {
285313

286-
std::ofstream f(sFilename, std::ifstream::in);
314+
// std::ofstream f(sFilename, std::ifstream::in);
315+
std::ofstream f(sFilename, std::ios::out);
287316
if (!f) {
288-
std::cout << "FAILED could not create file to wtite " << sFilename << std::endl;
317+
WSJCppLog::err("WSJCppCore", "Could not create file to write '" + sFilename + "'");
289318
return false;
290319
}
291-
292320
f << sContent << std::endl;
293321
f.close();
294322
return true;
@@ -380,21 +408,23 @@ std::string WSJCppCore::createUuid() {
380408
// WSJCppLog
381409

382410
// Last log messages
383-
std::deque<std::string> *g_LAST_LOG_MESSAGES = NULL;
384-
std::mutex *g_LOG_MUTEX = NULL;
385-
std::string WSJCppLog::g_LOG_DIR = "./";
386-
std::string WSJCppLog::g_LOG_FILE = "";
387-
std::string WSJCppLog::g_PREFIX_LOG_FILE = "";
388-
long WSJCppLog::g_LOG_START_TIME = 0;
411+
std::deque<std::string> * WSJCppLog::g_WSJCPP_LOG_LAST_MESSAGES = nullptr;
412+
std::mutex * WSJCppLog::g_WSJCPP_LOG_MUTEX = nullptr;
413+
std::string WSJCppLog::g_WSJCPP_LOG_DIR = "./";
414+
std::string WSJCppLog::g_WSJCPP_LOG_FILE = "";
415+
std::string WSJCppLog::g_WSJCPP_LOG_PREFIX_FILE = "";
416+
long WSJCppLog::g_WSJCPP_LOG_START_TIME = 0;
389417

390418
// ---------------------------------------------------------------------
391419

392420
void WSJCppLog::doLogRotateUpdateFilename(bool bForce) {
393421
long t = WSJCppCore::currentTime_seconds();
394422
long nEverySeconds = 51000; // rotate log if started now or if time left more then 1 day
395-
if (g_LOG_START_TIME == 0 || t - g_LOG_START_TIME > nEverySeconds || bForce) {
396-
g_LOG_START_TIME = t;
397-
g_LOG_FILE = g_LOG_DIR + "/" + WSJCppLog::g_PREFIX_LOG_FILE + "_" + WSJCppCore::formatTimeForFilename(g_LOG_START_TIME) + ".log";
423+
if (g_WSJCPP_LOG_START_TIME == 0 || t - g_WSJCPP_LOG_START_TIME > nEverySeconds || bForce) {
424+
g_WSJCPP_LOG_START_TIME = t;
425+
g_WSJCPP_LOG_FILE = g_WSJCPP_LOG_DIR + "/"
426+
+ WSJCppLog::g_WSJCPP_LOG_PREFIX_FILE + "_"
427+
+ WSJCppCore::formatTimeForFilename(g_WSJCPP_LOG_START_TIME) + ".log";
398428
}
399429
}
400430

@@ -437,28 +467,28 @@ void WSJCppLog::ok(const std::string &sTag, const std::string &sMessage) {
437467
// ---------------------------------------------------------------------
438468

439469
void WSJCppLog::setLogDirectory(const std::string &sDirectoryPath) {
440-
WSJCppLog::g_LOG_DIR = sDirectoryPath;
470+
WSJCppLog::g_WSJCPP_LOG_DIR = sDirectoryPath;
441471
WSJCppLog::doLogRotateUpdateFilename(true);
442472
}
443473

444474
// ---------------------------------------------------------------------
445475

446476
void WSJCppLog::setPrefixLogFile(const std::string &sPrefixLogFile) {
447-
WSJCppLog::g_PREFIX_LOG_FILE = sPrefixLogFile;
477+
WSJCppLog::g_WSJCPP_LOG_PREFIX_FILE = sPrefixLogFile;
448478
WSJCppLog::doLogRotateUpdateFilename(true);
449479
}
450480

451481
// ---------------------------------------------------------------------
452482

453483
void WSJCppLog::initGlobalVariables() {
454484
// create deque if not created
455-
if (g_LAST_LOG_MESSAGES == NULL) {
456-
g_LAST_LOG_MESSAGES = new std::deque<std::string>();
485+
if (WSJCppLog::g_WSJCPP_LOG_LAST_MESSAGES == nullptr) {
486+
WSJCppLog::g_WSJCPP_LOG_LAST_MESSAGES = new std::deque<std::string>();
457487
// std::cout << WSJCppCore::currentTime_logformat() + ", " + WSJCppCore::threadId() + " Init last messages deque\r\n";
458488
}
459489
// create mutex if not created
460-
if (g_LOG_MUTEX == NULL) {
461-
g_LOG_MUTEX = new std::mutex();
490+
if (WSJCppLog::g_WSJCPP_LOG_MUTEX == nullptr) {
491+
WSJCppLog::g_WSJCPP_LOG_MUTEX = new std::mutex();
462492
// std::cout << WSJCppCore::currentTime_logformat() + ", " + WSJCppCore::threadId() + " Init mutex for log\r\n";
463493
}
464494
}
@@ -469,19 +499,19 @@ void WSJCppLog::add(WSJCppColorModifier &clr, const std::string &sType, const st
469499
WSJCppLog::initGlobalVariables();
470500
WSJCppLog::doLogRotateUpdateFilename();
471501

472-
std::lock_guard<std::mutex> lock(*g_LOG_MUTEX);
502+
std::lock_guard<std::mutex> lock(*WSJCppLog::g_WSJCPP_LOG_MUTEX);
473503
WSJCppColorModifier def(WSJCppColorCode::FG_DEFAULT);
474504

475505
std::string sLogMessage = WSJCppCore::currentTime_logformat() + ", " + WSJCppCore::threadId()
476506
+ " [" + sType + "] " + sTag + ": " + sMessage;
477507
std::cout << clr << sLogMessage << def << std::endl;
478508

479-
g_LAST_LOG_MESSAGES->push_front(sLogMessage);
480-
while (g_LAST_LOG_MESSAGES->size() > 50) {
481-
g_LAST_LOG_MESSAGES->pop_back();
509+
g_WSJCPP_LOG_LAST_MESSAGES->push_front(sLogMessage);
510+
while (g_WSJCPP_LOG_LAST_MESSAGES->size() > 50) {
511+
g_WSJCPP_LOG_LAST_MESSAGES->pop_back();
482512
}
483513
// TODO try create global variable
484-
std::ofstream logFile(WSJCppLog::g_LOG_FILE, std::ios::app);
514+
std::ofstream logFile(WSJCppLog::g_WSJCPP_LOG_FILE, std::ios::app);
485515
if (!logFile) {
486516
std::cout << "Error Opening File" << std::endl;
487517
return;
@@ -490,3 +520,5 @@ void WSJCppLog::add(WSJCppColorModifier &clr, const std::string &sType, const st
490520
logFile << sLogMessage << std::endl;
491521
logFile.close();
492522
}
523+
524+

src.wsjcpp/wsjcpp-core/wsjcpp_core.h renamed to src.wsjcpp/wsjcpp_wsjcpp_core/wsjcpp_core.h

Lines changed: 11 additions & 7 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();
@@ -80,10 +81,12 @@ class WSJCppColorModifier {
8081

8182
class WSJCppLog {
8283
public:
83-
static std::string g_LOG_DIR;
84-
static std::string g_PREFIX_LOG_FILE;
85-
static std::string g_LOG_FILE;
86-
static long g_LOG_START_TIME;
84+
static std::string g_WSJCPP_LOG_DIR;
85+
static std::string g_WSJCPP_LOG_PREFIX_FILE;
86+
static std::string g_WSJCPP_LOG_FILE;
87+
static long g_WSJCPP_LOG_START_TIME;
88+
static std::mutex * g_WSJCPP_LOG_MUTEX;
89+
static std::deque<std::string> * g_WSJCPP_LOG_LAST_MESSAGES;
8790
static void doLogRotateUpdateFilename(bool bForce = false);
8891

8992
static void info(const std::string &sTag, const std::string &sMessage);
@@ -93,11 +96,12 @@ class WSJCppLog {
9396
static void ok(const std::string &sTag, const std::string &sMessage);
9497
static void setLogDirectory(const std::string &sDirectoryPath);
9598
static void setPrefixLogFile(const std::string &sPrefixLogFile);
96-
// static nlohmann::json getLastLogs();
9799
static void initGlobalVariables();
98100

99101
private:
100102
static void add(WSJCppColorModifier &clr, const std::string &sType, const std::string &sTag, const std::string &sMessage);
101103
};
102104

103-
#endif // WSJCPP_CORE_H
105+
#endif // WSJCPP_CORE_H
106+
107+

src/wsjcpp_yaml.cpp

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,30 @@ std::vector<std::string> WSJCppYAMLItem::getKeys() {
271271

272272
// ---------------------------------------------------------------------
273273

274+
bool WSJCppYAMLItem::setElementValue(const std::string &sName, bool bHasNameQuotes, const std::string &sValue, bool bHasValueQuotes) {
275+
if (m_nItemType == WSJCPP_YAML_ITEM_UNDEFINED) {
276+
m_nItemType = WSJCPP_YAML_ITEM_MAP; // change item type to map on first element
277+
}
278+
279+
if (m_nItemType != WSJCPP_YAML_ITEM_MAP) {
280+
WSJCppLog::throw_err(TAG, "setElement, Element must be 'map' for " + this->getPlaceInFile().getForLogFormat());
281+
}
282+
283+
if (this->hasElement(sName)) {
284+
WSJCppYAMLItem *pItem = this->getElement(sName);
285+
pItem->setValue(sValue, bHasValueQuotes);
286+
} else {
287+
WSJCppYAMLPlaceInFile pl;
288+
WSJCppYAMLItem *pNewItem = new WSJCppYAMLItem(this, pl, WSJCppYAMLItemType::WSJCPP_YAML_ITEM_VALUE);
289+
pNewItem->setName(sName, bHasNameQuotes);
290+
pNewItem->setValue(sValue, bHasValueQuotes);
291+
this->setElement(sName, pNewItem);
292+
}
293+
return true;
294+
}
295+
296+
// ---------------------------------------------------------------------
297+
274298
bool WSJCppYAMLItem::isArray() {
275299
return m_nItemType == WSJCPP_YAML_ITEM_ARRAY;
276300
}
@@ -279,7 +303,7 @@ bool WSJCppYAMLItem::isArray() {
279303

280304
int WSJCppYAMLItem::getLength() {
281305
if (m_nItemType != WSJCPP_YAML_ITEM_ARRAY) {
282-
WSJCppLog::throw_err(TAG, "getLength, Element must be array");
306+
WSJCppLog::throw_err(TAG, "getLength, Element must be array for " + this->getForLogFormat());
283307
}
284308
int nCount = 0;
285309
for (int i = 0; i < m_vObjects.size(); i++) {
@@ -329,6 +353,36 @@ bool WSJCppYAMLItem::appendElement(WSJCppYAMLItem *pItem) {
329353

330354
// ---------------------------------------------------------------------
331355

356+
bool WSJCppYAMLItem::removeElement(int i) {
357+
if (m_nItemType != WSJCPP_YAML_ITEM_ARRAY) {
358+
WSJCppLog::throw_err(TAG, "appendElement, Element must be array for " + this->getForLogFormat());
359+
}
360+
int nCounter = -1;
361+
WSJCppYAMLItem *pItem = nullptr;
362+
for (int n = 0; n < m_vObjects.size(); n++) {
363+
if (!m_vObjects[n]->isEmpty()) {
364+
nCounter++;
365+
if (nCounter == i) {
366+
pItem = m_vObjects[n];
367+
break;
368+
}
369+
}
370+
}
371+
if (pItem == nullptr) {
372+
WSJCppLog::throw_err(TAG, "getElement(" + std::to_string(i) + "), Out of range in array for '" + this->getPlaceInFile().getLine() + "'");
373+
}
374+
std::vector<WSJCppYAMLItem *>::iterator it;
375+
for (it = m_vObjects.begin(); it != m_vObjects.end(); ++it) {
376+
if (*it == pItem) {
377+
m_vObjects.erase(it);
378+
return true;
379+
}
380+
}
381+
return false;
382+
}
383+
384+
// ---------------------------------------------------------------------
385+
332386
bool WSJCppYAMLItem::isValue() {
333387
return m_nItemType == WSJCPP_YAML_ITEM_VALUE;
334388
}
@@ -928,3 +982,4 @@ void WSJCppYAML::process_sameIntent_emptyName_emptyValue_noArrayItem(WSJCppYAMLP
928982
}
929983

930984
// ---------------------------------------------------------------------
985+

0 commit comments

Comments
 (0)