diff --git a/src/File.cpp b/src/File.cpp new file mode 100644 index 0000000..6bd7b8d --- /dev/null +++ b/src/File.cpp @@ -0,0 +1,39 @@ +/* + * File.cpp + * + * Created on: 17.10.2015 + * Author: jdi + */ + +#include +#include "File.h" + +std::string File::read(std::string path) { + if (!fs::exists(home)) { + fs::create_directory(home); + } + + fs::ifstream in((home / path), std::ios::in | std::ios::binary); + if (in) { + std::string contents; + in.seekg(0, std::ios::end); + contents.resize(in.tellg()); + in.seekg(0, std::ios::beg); + in.read(&contents[0], contents.size()); + in.close(); + return (contents); + } + return ""; +} + +int File::write(std::string path, std::string content) { + if (!fs::exists(home)) { + fs::create_directory(home); + } + fs::path p = home / path; + fs::ofstream file(p); + file << content; + file.close(); + return 0; +} + diff --git a/src/File.h b/src/File.h new file mode 100644 index 0000000..955a2bd --- /dev/null +++ b/src/File.h @@ -0,0 +1,28 @@ +/* + * File.h + * + * Created on: 17.10.2015 + * Author: jdi + */ + +#ifndef FILE_H_ +#define FILE_H_ + +#include +#include +namespace fs = boost::filesystem; + +#include + +class File { +public: + File() { + home = fs::path(getenv("HOME")) / ".smrtlink"; + } + std::string read(std::string); + int write(std::string, std::string); +private: + fs::path home; +}; + +#endif /* FILE_H_ */ diff --git a/src/jsonNode.cpp b/src/jsonNode.cpp new file mode 100644 index 0000000..a617402 --- /dev/null +++ b/src/jsonNode.cpp @@ -0,0 +1,51 @@ +/* + * jsonNode.cpp + * + * Created on: 17.10.2015 + * Author: jdi + */ + +#include "jsonNode.h" + +jsonNode::jsonNode(const std::string &x, doc &root) { + super(rapidjson::kStringType); + char buffer[30]; + int len = sprintf(buffer, "%s", x.c_str()); + this->SetString(buffer, static_cast(len), root.GetAllocator()); + memset(buffer, 0, sizeof(buffer)); +} + +jsonNode::jsonNode(const ipAddr &x, doc &root) { + super(rapidjson::kStringType); + char buffer[16]; + int len = sprintf(buffer, "%d.%d.%d.%d", x[0], x[1], + x[2], x[3]); + this->SetString(buffer, static_cast(len), root.GetAllocator()); + memset(buffer, 0, sizeof(buffer)); +} + +jsonNode::jsonNode(const macAddr &x, doc &root) { + super(rapidjson::kStringType); + char buffer[18]; + int len = sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x", x[0], x[1], + x[2], x[3], x[4], x[5]); + this->SetString(buffer, static_cast(len), root.GetAllocator()); + memset(buffer, 0, sizeof(buffer)); +} + +jsonNode::jsonNode(const vlan &x, doc &root) { + super(rapidjson::kObjectType); + AddMember("id", x.vlan_id, root.GetAllocator()); + AddMember("name", jsonNode(x.name, root), root.GetAllocator()); + AddMember("tagged-members", jsonNode(x.tagged_member, root), + root.GetAllocator()); + AddMember("untagged-members", jsonNode(x.untagged_member, root), + root.GetAllocator()); +} + +jsonNode::jsonNode(const port &x, doc &root) { + super(rapidjson::kObjectType); + AddMember("id", x.id, root.GetAllocator()); + AddMember("status", x.status, root.GetAllocator()); + AddMember("pvid", x.pvid, root.GetAllocator()); +} diff --git a/src/jsonNode.h b/src/jsonNode.h new file mode 100644 index 0000000..9fccbf5 --- /dev/null +++ b/src/jsonNode.h @@ -0,0 +1,38 @@ +/* + * jsonNode.h + * + * Created on: 17.10.2015 + * Author: jdi + */ + +#ifndef JSONNODE_H_ +#define JSONNODE_H_ + +#include "../include/rapidjson/document.h" +#include "../include/rapidjson/prettywriter.h" +#include "Switch.h" + +class jsonNode: public rapidjson::Value { + typedef rapidjson::Value super; + typedef rapidjson::Document doc; +public: + jsonNode(const std::string&, doc&); + jsonNode(const macAddr&, doc&); + jsonNode(const ipAddr&, doc&); + jsonNode(const vlan&, doc&); + jsonNode(const port&, doc&); + + template + jsonNode(const T &templ_arg, doc &root){ + std::cerr<<"Not serializable Type: "< + jsonNode(const std::vector &x, doc &root) { + super(rapidjson::kArrayType); + for (T y : x) + PushBack(jsonNode(y, root), root.GetAllocator()); + } +}; + +#endif /* JSONNODE_H_ */