Commit 56b4b3a8 authored by /jdi/'s avatar /jdi/
Browse files

json lib added

parent da8ac53f
/*
* Types.h
*
* Created on: 11.09.2015
* Author: jdi
*/
#ifndef TYPES_H_
#define TYPES_H_
#include <initializer_list>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <array>
#include "bytes.h"
#include "datasets.h"
class macAddr: public std::array<byte, 6> {
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<byte> 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<unsigned char, 6> {
public:
typedef std::array<unsigned char, 6> super;
using super::super;
mac_addr{00, 00, 00, 000};
};
*/
class ipAddr: public std::array<byte, 4> {
public:
ipAddr() {
*this= {0,0,0,0,0,0};
}
ipAddr(std::initializer_list<byte> 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<typename T>
std::vector<T> operator+(const std::vector<T> &A, const std::vector<T> &B) {
std::vector<T> 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<typename T>
std::vector<T> &operator+=(std::vector<T> &A, const std::vector<T> &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_ */
/*
* 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);
}
/*
* Bytes.h
*
* Created on: 02.10.2015
* Author: jdi
*/
#ifndef BYTES_H_
#define BYTES_H_
#include <initializer_list>
#include <functional>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <map>
typedef unsigned char byte;
class bytes: public std::vector<unsigned char> {
typedef std::vector<unsigned char> vector;
public:
using vector::operator[];
bytes() {
}
bytes(int n) :
vector(n) {
}
bytes(std::string);
bytes(std::initializer_list<uint8_t> 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_ */
/*
* Datasets.cpp
*
* Created on: 02.10.2015
* Author: jdi
*/
#include "datasets.h"
/*
* Datasets.h
*
* Created on: 02.10.2015
* Author: jdi
*/
#ifndef DATASETS_H_
#define DATASETS_H_
#include <map>
#include "bytes.h"
struct dataset {
short type;
short len;
bytes value;
};
class datasets : public std::map<short, dataset> {
public:
datasets(){};
datasets(std::initializer_list<dataset> s)
{
for (dataset b : s) {
(*this)[b.type]=b;
}
}
};
#endif /* DATASETS_H_ */
/*
* lookupTable.h
*
* Created on: 11.10.2015
* Author: jdi
*/
#include "lookupTable.h"
lookupTable::lookupTable(std::initializer_list<set> 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());
}
/*
* lookupTable.h
*
* Created on: 11.10.2015
* Author: jdi
*/
#ifndef LOOKUPTABLE_H_
#define LOOKUPTABLE_H_
#include <map>
#include <vector>
class lookupTable {
public:
struct set {
short type;
std::string name;
};
lookupTable(std::initializer_list<set> 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<set> data;
std::map<short, set*> left;
std::map<std::string, set*> right;
};
#endif /* LOOKUPTABLE_H_ */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment