You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

78 lines
2.2 KiB

#ifndef __CFGFILEPARSER_H__
#define __CFGFILEPARSER_H__
#include <string>
#include <unordered_map>
#include <utility>
#include <algorithm>
#include "TLSFAlloc.h"
#include <sys/stat.h>
#include <cstdint>
#include "Murmur3.h"
// let's try fast strings
namespace ntr{
typedef std::basic_string<char, std::char_traits<char>, TLSFAlloc<char>> fast_string;
class StringHash{
public:
std::size_t operator() (const fast_string& s) const {
size_t out = 0;
// 32-bit hash only, all others overflow. RIP AMD64 users...
MurmurHash3_x86_32 ( &s, sizeof(s), 7904542L, &out );
return out;
}
};
class StringComparison{
public:
bool operator ()(const fast_string &a, const fast_string &b) const {
int i = strcmp(a.c_str(), b.c_str());
if (!i){
return true;
}
else{
return false;
}
}
};
typedef std::unordered_map<fast_string,fast_string,StringHash,StringComparison,TLSFAlloc<std::pair<const fast_string, fast_string>>> stringmap;
}
// Class which holds the parsed configuration options after
// being processed by CFGFileParser. No longer a "trivial class"
// just select by cfg->GlobalSettings....
struct BrowserConfiguration{
ntr::stringmap Settings;
// fast allocation!
static void* operator new(std::size_t n){
if (void *mem = tlsf_malloc(n)) {
return mem;
} throw std::bad_alloc {};
}
static void operator delete(void *p) {
tlsf_free(p);
}
void clear();
};
class CFGFileParser {
public:
CFGFileParser(const char* filename);
~CFGFileParser();
void ParseText();
void WriteConfig(BrowserConfiguration &config);
static void* operator new(std::size_t n) {
if (void *mem = tlsf_malloc(n)){
return mem;
} throw std::bad_alloc {};
}
static void operator delete(void *p) {
tlsf_free(p);
}
private:
FILE *cfg_file;
char *buffer;
struct stat *cfg_fileinfo = reinterpret_cast<struct stat*>(tlsf_malloc(sizeof(struct stat)));
BrowserConfiguration *cfg = new BrowserConfiguration();
//std::pair<ntr::fast_string,ntr::fast_string>
size_t bytesRead;
};
#endif // __CFGFILEPARSER_H__