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.
 
 
 
 

60 lines
1.7 KiB

#ifndef __CFGFILEPARSER_H__
#define __CFGFILEPARSER_H__
#include <string>
#include <unordered_map>
#include <utility>
#include <functional>
#include <algorithm>
#include "TLSFAlloc.h"
#include "Murmur3.h"
#include "tlsf.h"
#include <sys/stat.h>
#include <cstdint>
// let's try fast strings
namespace ntr{
typedef std::basic_string<char, std::char_traits<char>, TLSFAlloc<char>> fast_string;
size_t string_hash(const fast_string &str);
bool fast_string_compare(fast_string t1, fast_string t2);
typedef std::unordered_map<fast_string,fast_string,std::function<decltype(string_hash)>,std::function<decltype(fast_string_compare)>,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;
size_t bytesRead;
};
#endif // __CFGFILEPARSER_H__