From 56b4b3a84b88edfd143063a847f12831ca3cc8f5 Mon Sep 17 00:00:00 2001 From: /jdi/ Date: Thu, 15 Oct 2015 20:32:05 +0200 Subject: [PATCH] json lib added --- src/Types.h | 124 ++++++++++++++++++++++++++++++++++++++++++++ src/bytes.cpp | 28 ++++++++++ src/bytes.h | 86 ++++++++++++++++++++++++++++++ src/datasets.cpp | 8 +++ src/datasets.h | 31 +++++++++++ src/lookupTable.cpp | 31 +++++++++++ src/lookupTable.h | 31 +++++++++++ 7 files changed, 339 insertions(+) create mode 100644 src/Types.h create mode 100644 src/bytes.cpp create mode 100644 src/bytes.h create mode 100644 src/datasets.cpp create mode 100644 src/datasets.h create mode 100644 src/lookupTable.cpp create mode 100644 src/lookupTable.h diff --git a/src/Types.h b/src/Types.h new file mode 100644 index 0000000..1a45092 --- /dev/null +++ b/src/Types.h @@ -0,0 +1,124 @@ +/* + * Types.h + * + * Created on: 11.09.2015 + * Author: jdi + */ + +#ifndef TYPES_H_ +#define TYPES_H_ + +#include +#include +#include +#include +#include +#include + +#include "bytes.h" +#include "datasets.h" + +class macAddr: public std::array { +public: + friend std::ostream& operator<<(std::ostream& out, const macAddr& arr) { + out << std::hex << std::setw(2) << std::setfill('0') + << (unsigned) arr[0]; + for (unsigned i = 1; i < 6; i++) { + out << ":" << std::setw(2) << std::setfill('0') + << (unsigned) arr[i]; + } + return out; + } + + macAddr() { + *this= {0,0,0,0,0,0}; + } + + macAddr(std::initializer_list s) { + int i = 0; + for (byte b : s) { + if(i<6) (*this)[i++]=b; + else break; + } + } + + macAddr(bytes bts) { + int i = 0; + for (byte b : bts) { + if(i<6) (*this)[i++]=b; + else break; + } + } +}; + +/* +class mac_addr : public std::array { +public: + typedef std::array super; + + using super::super; + + mac_addr{00, 00, 00, 000}; +}; +*/ + + +class ipAddr: public std::array { +public: + + ipAddr() { + *this= {0,0,0,0,0,0}; + } + + ipAddr(std::initializer_list s) { + int i = 0; + for (byte b : s) { + if(i<4) (*this)[i++]=b; + else break; + } + } + + ipAddr(bytes bts) { + int i = 0; + for (byte b : bts) { + if(i<4) (*this)[i++]=b; + else break; + } + } + + friend std::ostream& operator<<(std::ostream& out, ipAddr& arr) { + out << std::dec << (unsigned) arr[0]; + for (unsigned i = 1; i < 4; i++) { + out << "." << (unsigned) arr[i]; + } + return out; + } +}; + +template +std::vector operator+(const std::vector &A, const std::vector &B) { + std::vector AB; + AB.reserve(A.size() + B.size()); // preallocate memory + AB.insert(AB.end(), A.begin(), A.end()); // add A; + AB.insert(AB.end(), B.begin(), B.end()); // add B; + return AB; +} + +template +std::vector &operator+=(std::vector &A, const std::vector &B) { + A.reserve(A.size() + B.size()); + A.insert(A.end(), B.begin(), B.end()); + return A; +} + +struct Options { + unsigned flags = 0x00; + std::string user; + std::string password; + std::string interface; + std::string file; + int debug_level=0; + long timeout; +}; + +#endif /* TYPES_H_ */ diff --git a/src/bytes.cpp b/src/bytes.cpp new file mode 100644 index 0000000..bdc8c9d --- /dev/null +++ b/src/bytes.cpp @@ -0,0 +1,28 @@ +/* + * Bytes.cpp + * + * Created on: 02.10.2015 + * Author: jdi + */ + +#include "bytes.h" + +bytes::bytes(std::string d) { + vector(); + std::string delimiter = ":"; + std::string token; + size_t pos = 0; + int hex; + byte b; + resize(0); + while ((pos = d.find(delimiter)) != std::string::npos) { + token = d.substr(0, pos); + sscanf(token.c_str(), "%x", &hex); + d.erase(0, pos + delimiter.length()); + b = hex & 0xFF; + push_back(b); + } + sscanf(d.c_str(), "%x", &hex); + b = hex & 0xFF; + push_back(b); +} diff --git a/src/bytes.h b/src/bytes.h new file mode 100644 index 0000000..dcc0ec9 --- /dev/null +++ b/src/bytes.h @@ -0,0 +1,86 @@ +/* + * Bytes.h + * + * Created on: 02.10.2015 + * Author: jdi + */ + +#ifndef BYTES_H_ +#define BYTES_H_ + +#include +#include +#include +#include +#include +#include +#include + +typedef unsigned char byte; + +class bytes: public std::vector { + typedef std::vector vector; +public: + using vector::operator[]; + bytes() { + } + bytes(int n) : + vector(n) { + } + + bytes(std::string); + + bytes(std::initializer_list s) + { + for (uint8_t b : s) { + this->push_back(b); + } + } + + bytes(const vector &B) { + this->reserve(B.size()); + this->insert(this->begin(), B.begin(), B.end()); + } + + bytes readHex(std::string s){ + return bytes(s); + } + + bytes operator=(const vector &B) { + this->reserve(B.size()); + this->insert(this->begin(), B.begin(), B.end()); + return *this; + } + + bytes &operator+=(const bytes &B) { + this->reserve(this->size() + B.size()); + this->insert(this->end(), B.begin(), B.end()); + return *this; + } + + bytes operator+(const bytes &B) { + bytes AB; + AB.reserve(this->size() + B.size()); + AB.insert(AB.end(), this->begin(), this->end()); + AB.insert(AB.end(), B.begin(), B.end()); + return AB; + } + + friend std::ostream& operator<<(std::ostream& out, const bytes& arr) { + if (arr.size() > 0) { + out << std::hex << std::setw(2) << std::setfill('0') + << (unsigned) arr[0]; + } + for (unsigned i = 1; i < arr.size(); i++) { + out << ":" << std::setw(2) << std::setfill('0') << (unsigned) arr[i]; + } + return out; + } + + operator std::string() { + std::string s(this->begin(),this->end()); + return s; + } +}; + +#endif /* BYTES_H_ */ diff --git a/src/datasets.cpp b/src/datasets.cpp new file mode 100644 index 0000000..63529b4 --- /dev/null +++ b/src/datasets.cpp @@ -0,0 +1,8 @@ +/* + * Datasets.cpp + * + * Created on: 02.10.2015 + * Author: jdi + */ + +#include "datasets.h" diff --git a/src/datasets.h b/src/datasets.h new file mode 100644 index 0000000..e570b18 --- /dev/null +++ b/src/datasets.h @@ -0,0 +1,31 @@ +/* + * Datasets.h + * + * Created on: 02.10.2015 + * Author: jdi + */ + +#ifndef DATASETS_H_ +#define DATASETS_H_ + +#include +#include "bytes.h" + +struct dataset { + short type; + short len; + bytes value; +}; + +class datasets : public std::map { +public: + datasets(){}; + datasets(std::initializer_list s) + { + for (dataset b : s) { + (*this)[b.type]=b; + } + } +}; + +#endif /* DATASETS_H_ */ diff --git a/src/lookupTable.cpp b/src/lookupTable.cpp new file mode 100644 index 0000000..389ef3c --- /dev/null +++ b/src/lookupTable.cpp @@ -0,0 +1,31 @@ +/* + * lookupTable.h + * + * Created on: 11.10.2015 + * Author: jdi + */ + +#include "lookupTable.h" + +lookupTable::lookupTable(std::initializer_list l) { + int i = 0; + this->data.resize(l.size()); + for (set s : l) { + this->data[i] = s; + this->left[s.type] = &this->data[i]; + this->right[s.name] = &this->data[i]; + i++; + } +} +const short& lookupTable::operator[](std::string s) { + return this->right[s]->type; +} +const std::string& lookupTable::operator[](short n) { + return this->left[n]->name; +} +bool lookupTable::exists(std::string s) { + return !(right.find(s) == right.end()); +} +bool lookupTable::exists(short n) { + return !(left.find(n) == left.end()); +} diff --git a/src/lookupTable.h b/src/lookupTable.h new file mode 100644 index 0000000..f293ca7 --- /dev/null +++ b/src/lookupTable.h @@ -0,0 +1,31 @@ +/* + * lookupTable.h + * + * Created on: 11.10.2015 + * Author: jdi + */ + +#ifndef LOOKUPTABLE_H_ +#define LOOKUPTABLE_H_ + +#include +#include + +class lookupTable { +public: + struct set { + short type; + std::string name; + }; + lookupTable(std::initializer_list l); + const short& operator[](std::string s); + const std::string& operator[](short n); + bool exists(std::string s); + bool exists(short n); +private: + std::vector data; + std::map left; + std::map right; +}; + +#endif /* LOOKUPTABLE_H_ */