Skip to content

Commit

Permalink
Updated wsjcpp-core
Browse files Browse the repository at this point in the history
  • Loading branch information
sea-kg committed Apr 5, 2020
1 parent dadacec commit 5f1252c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 52 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# wsjcpp-print-tree

[![Build Status](https://api.travis-ci.org/wsjcpp/wsjcpp-print-tree.svg?branch=master)](https://travis-ci.org/wsjcpp/wsjcpp-print-tree) [![Github Stars](https://img.shields.io/github/stars/wsjcpp/wsjcpp-print-tree.svg?label=github%20%E2%98%85)](https://github.com/wsjcpp/wsjcpp-print-tree) [![Github Stars](https://img.shields.io/github/contributors/wsjcpp/wsjcpp-print-tree.svg)](https://github.com/wsjcpp/wsjcpp-print-tree) [![Github Forks](https://img.shields.io/github/forks/wsjcpp/wsjcpp-print-tree.svg?label=github%20forks)](https://github.com/wsjcpp/wsjcpp-print-tree/network/members)
[![Build Status](https://api.travis-ci.com/wsjcpp/wsjcpp-print-tree.svg?branch=master)](https://travis-ci.com/wsjcpp/wsjcpp-print-tree) [![Github Stars](https://img.shields.io/github/stars/wsjcpp/wsjcpp-print-tree.svg?label=github%20%E2%98%85)](https://github.com/wsjcpp/wsjcpp-print-tree) [![Github Stars](https://img.shields.io/github/contributors/wsjcpp/wsjcpp-print-tree.svg)](https://github.com/wsjcpp/wsjcpp-print-tree) [![Github Forks](https://img.shields.io/github/forks/wsjcpp/wsjcpp-print-tree.svg?label=github%20forks)](https://github.com/wsjcpp/wsjcpp-print-tree/network/members)

Helper class for print tree like a for filesystems

Expand All @@ -22,7 +22,7 @@ Second way integrate files:
## Example of usage

```
WSJCppPrintTree tree("Example Of Tree");
WsjcppPrintTree tree("Example Of Tree");
tree
.addChild("Hello1")
.switchToLatestChild()
Expand Down
14 changes: 12 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@

int main(int argc, const char* argv[]) {
std::string TAG = "MAIN";
WSJCppCore::init(argc, argv, true);
std::string appName = std::string(WSJCPP_NAME);
std::string appVersion = std::string(WSJCPP_VERSION);

std::string appLogPath = ".logs";
if (!WsjcppCore::dirExists(appLogPath)) {
WsjcppCore::makeDir(appLogPath);
}
WsjcppLog::setPrefixLogFile("wsjcpp-print-tree");
WsjcppLog::setLogDirectory(appLogPath);

// WsjcppCore::init(argc, argv, sAppName, sAppVersion, "Evgenii Sopov", "WsjcppPrintTree");

// may be better read from file struct for example json or any some
WSJCppPrintTree tree("Example Of Tree");
WsjcppPrintTree tree("Example Of Tree");
tree
.addChild("Hello1")
.switchToLatestChild()
Expand Down
52 changes: 26 additions & 26 deletions src/wsjcpp_print_tree.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
#include "wsjcpp_print_tree.h"
#include <wsjcpp_core.h>

WSJCppPrintNode::WSJCppPrintNode(WSJCppPrintNode *pParent, const std::string &sTitle) {
TAG = "WSJCppPrintNode";
WsjcppPrintNode::WsjcppPrintNode(WsjcppPrintNode *pParent, const std::string &sTitle) {
TAG = "WsjcppPrintNode";
m_pParent = pParent;
m_sTitle = sTitle;
}

// ----------------------------------------------------------------------

std::string WSJCppPrintNode::getTitle() {
std::string WsjcppPrintNode::getTitle() {
return m_sTitle;
}

// ----------------------------------------------------------------------

WSJCppPrintNode *WSJCppPrintNode::getParent() {
WsjcppPrintNode *WsjcppPrintNode::getParent() {
return m_pParent;
}

// ----------------------------------------------------------------------

std::vector<WSJCppPrintNode *> WSJCppPrintNode::getChildsList() {
std::vector<WsjcppPrintNode *> WsjcppPrintNode::getChildsList() {
return m_vChilds;
}

// ----------------------------------------------------------------------

WSJCppPrintNode *WSJCppPrintNode::getLastChild() {
WsjcppPrintNode *WsjcppPrintNode::getLastChild() {
if (m_vChilds.size() == 0) {
return nullptr;
}
Expand All @@ -36,85 +36,85 @@ WSJCppPrintNode *WSJCppPrintNode::getLastChild() {

// ----------------------------------------------------------------------

WSJCppPrintNode *WSJCppPrintNode::addChild(const std::string &sTitle) {
WSJCppPrintNode *pNode = new WSJCppPrintNode(this, sTitle);
WsjcppPrintNode *WsjcppPrintNode::addChild(const std::string &sTitle) {
WsjcppPrintNode *pNode = new WsjcppPrintNode(this, sTitle);
m_vChilds.push_back(pNode);
return pNode;
}

// ----------------------------------------------------------------------

bool WSJCppPrintNode::hasChilds() {
bool WsjcppPrintNode::hasChilds() {
return m_vChilds.size() > 0;
}

// ----------------------------------------------------------------------
// WSJCppPrintTree
// WsjcppPrintTree

WSJCppPrintTree::WSJCppPrintTree(const std::string &sTitle) {
TAG = "WSJCppPrintTree";
m_pRootNode = new WSJCppPrintNode(nullptr, sTitle);
WsjcppPrintTree::WsjcppPrintTree(const std::string &sTitle) {
TAG = "WsjcppPrintTree";
m_pRootNode = new WsjcppPrintNode(nullptr, sTitle);
m_pCurrentNode = m_pRootNode;
}

// ----------------------------------------------------------------------

WSJCppPrintNode *WSJCppPrintTree::getRootNode() {
WsjcppPrintNode *WsjcppPrintTree::getRootNode() {
return m_pRootNode;
}

// ----------------------------------------------------------------------

WSJCppPrintNode *WSJCppPrintTree::getCurrentNode() {
WsjcppPrintNode *WsjcppPrintTree::getCurrentNode() {
return m_pCurrentNode;
}

// ----------------------------------------------------------------------

WSJCppPrintTree &WSJCppPrintTree::switchToLatestChild() {
WSJCppPrintNode *pChild = m_pCurrentNode->getLastChild();
WsjcppPrintTree &WsjcppPrintTree::switchToLatestChild() {
WsjcppPrintNode *pChild = m_pCurrentNode->getLastChild();
if (pChild == nullptr) {
WSJCppLog::throw_err(TAG, "Could not found last child");
WsjcppLog::throw_err(TAG, "Could not found last child");
}
m_pCurrentNode = pChild;
return *this;
}

// ----------------------------------------------------------------------

WSJCppPrintTree &WSJCppPrintTree::addChild(const std::string &sTitle) {
WsjcppPrintTree &WsjcppPrintTree::addChild(const std::string &sTitle) {
m_pCurrentNode->addChild(sTitle);
return *this;
}

// ----------------------------------------------------------------------

WSJCppPrintTree &WSJCppPrintTree::switchToParent() {
WSJCppPrintNode *pParent = m_pCurrentNode->getParent();
WsjcppPrintTree &WsjcppPrintTree::switchToParent() {
WsjcppPrintNode *pParent = m_pCurrentNode->getParent();
if (pParent == nullptr) {
WSJCppLog::throw_err(TAG, "Parent is null");
WsjcppLog::throw_err(TAG, "Parent is null");
}
m_pCurrentNode = pParent;
return *this;
}

// ----------------------------------------------------------------------

std::string WSJCppPrintTree::printTree() {
std::string WsjcppPrintTree::printTree() {
return
m_pRootNode->getTitle() + "\n"
+ printRecoursive("", m_pRootNode);
}

// ----------------------------------------------------------------------

std::string WSJCppPrintTree::printRecoursive(const std::string &sPrefix, WSJCppPrintNode *pParentNode) {
std::string WsjcppPrintTree::printRecoursive(const std::string &sPrefix, WsjcppPrintNode *pParentNode) {
std::string sRet = "";
std::vector<WSJCppPrintNode *> v = pParentNode->getChildsList();
std::vector<WsjcppPrintNode *> v = pParentNode->getChildsList();
int nLen = v.size();
for (int i = 0; i < nLen; i++) {
bool bLatestChild = (i == nLen-1);
WSJCppPrintNode *pNode = v[i];
WsjcppPrintNode *pNode = v[i];
sRet += sPrefix;
sRet += bLatestChild ? "└─ " : "├─ ";
sRet += pNode->getTitle() + "\n";
Expand Down
36 changes: 18 additions & 18 deletions src/wsjcpp_print_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,40 @@
#include <string>
#include <vector>

class WSJCppPrintNode {
class WsjcppPrintNode {
public:
WSJCppPrintNode(WSJCppPrintNode *pParent, const std::string &sTitle);
WsjcppPrintNode(WsjcppPrintNode *pParent, const std::string &sTitle);
std::string getTitle();
WSJCppPrintNode *getParent();
std::vector<WSJCppPrintNode *> getChildsList();
WSJCppPrintNode *getLastChild();
WSJCppPrintNode *addChild(const std::string &sTitle);
WsjcppPrintNode *getParent();
std::vector<WsjcppPrintNode *> getChildsList();
WsjcppPrintNode *getLastChild();
WsjcppPrintNode *addChild(const std::string &sTitle);
bool hasChilds();
private:
std::string TAG;
WSJCppPrintNode *m_pParent;
WsjcppPrintNode *m_pParent;
std::string m_sTitle;
std::vector<WSJCppPrintNode *> m_vChilds;
std::vector<WsjcppPrintNode *> m_vChilds;
};

// ----------------------------------------------------------------------

class WSJCppPrintTree {
class WsjcppPrintTree {
public:
WSJCppPrintTree(const std::string &sTitle);
WSJCppPrintNode *getRootNode();
WSJCppPrintNode *getCurrentNode();
WSJCppPrintTree &switchToLatestChild();
WSJCppPrintTree &addChild(const std::string &sTitle);
WSJCppPrintTree &switchToParent();
WsjcppPrintTree(const std::string &sTitle);
WsjcppPrintNode *getRootNode();
WsjcppPrintNode *getCurrentNode();
WsjcppPrintTree &switchToLatestChild();
WsjcppPrintTree &addChild(const std::string &sTitle);
WsjcppPrintTree &switchToParent();
std::string printTree();

private:
std::string TAG;
WSJCppPrintNode *m_pRootNode;
WSJCppPrintNode *m_pCurrentNode;
WsjcppPrintNode *m_pRootNode;
WsjcppPrintNode *m_pCurrentNode;

std::string printRecoursive(const std::string &sPrefix, WSJCppPrintNode *pParentNode);
std::string printRecoursive(const std::string &sPrefix, WsjcppPrintNode *pParentNode);
};

#endif // WSJCPP_PRINT_TREE_H
6 changes: 3 additions & 3 deletions unit-tests.wsjcpp/src/unit_test_tree_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include <wsjcpp_core.h>
#include <wsjcpp_print_tree.h>

REGISTRY_UNIT_TEST(UnitTestTreeSimple)
REGISTRY_WSJCPP_UNIT_TEST(UnitTestTreeSimple)

UnitTestTreeSimple::UnitTestTreeSimple()
: WSJCppUnitTestBase("UnitTestTreeSimple") {
: WsjcppUnitTestBase("UnitTestTreeSimple") {
}

// ---------------------------------------------------------------------
Expand All @@ -20,7 +20,7 @@ void UnitTestTreeSimple::init() {
bool UnitTestTreeSimple::run() {
bool bTestSuccess = true;

WSJCppPrintTree tree("Example Of Tree");
WsjcppPrintTree tree("Example Of Tree");
tree
.addChild("Hello1")
.switchToLatestChild()
Expand Down
2 changes: 1 addition & 1 deletion unit-tests.wsjcpp/src/unit_test_tree_simple.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <wsjcpp_unit_tests.h>

// Description: TODO
class UnitTestTreeSimple : public WSJCppUnitTestBase {
class UnitTestTreeSimple : public WsjcppUnitTestBase {
public:
UnitTestTreeSimple();
virtual void init();
Expand Down

0 comments on commit 5f1252c

Please sign in to comment.