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.
 
 
 
 

86 lines
2.8 KiB

#include "CFGFileParser.h"
#include <climits>
#include <string.h>
CFGFileParser::CFGFileParser(const char* filename){
// Open the config file, get its size,
// allocate the buffer, read it into
// the buffer, close the file
cfg_file = fopen(filename, "rb"); // On NT, opening in text mode translates \n into \r\n
stat(filename, cfg_fileinfo);
buffer = static_cast<char*>(tlsf_calloc(cfg_fileinfo->st_size & INT_MAX, sizeof(char) & INT_MAX));
cfg = new BrowserConfiguration();
bytesRead = fread(buffer, sizeof(char) & INT_MAX, cfg_fileinfo->st_size & INT_MAX, cfg_file);
fclose(cfg_file);
}
CFGFileParser::~CFGFileParser(){
// clean up!
tlsf_free(buffer);
}
void CFGFileParser::ParseText() {
// Initial buffer for pass 1. tmp holds the config file (fmr. buffer)
// token holds the actual token. pass1_length to keep track of memory,
// reallocate as needed.
char *tmp, *token;
size_t pass1_length = 0;
// Second pass. All comments and .tags stripped out. Starts off at 4KB, increases if necessary.
char* directives = static_cast<char*>(tlsf_calloc(4096, sizeof(char)));
token = strtok_r(buffer, "\n", &tmp);
while (tmp != nullptr) {
if (token[0] == '#' || token[0] == '.' || token[0] == '\n'){ // Comment, Perl directive, or single <LF> found, skip over
token = strtok_r(nullptr, "\n", &tmp);
}
else { // Config directive found, add to second-pass buffer
// once we reach the 4KB mark, realloc exactly enough to keep going
if (pass1_length >= 4096){
tlsf_realloc(directives, pass1_length+strlen(token)+2);
}
strcat(directives, token);
strcat(directives, "\t");
pass1_length = strlen(directives);
token = strtok_r(nullptr, "\n", &tmp); // continue
}
}
// First pass complete, second pass: break up into single directives and <key, value> pairs
tmp = reinterpret_cast<char*>(45); // just some random junk to avoid breaking out
char *key, *value;
while (tmp != nullptr){
key = strtok_r(directives, ":", &tmp);
value = strtok_r(nullptr, "\t", &tmp);
//cfg->Settings.insert({key, value});
}
}
// Writes the fully parsed configuration to a BrowserConfiguration
// object supplied by the user.
void CFGFileParser::WriteConfig(BrowserConfiguration &config){
config.Settings = cfg->Settings;
}
namespace ntr{
size_t string_hash(const fast_string &str){
size_t out = 0;
// instead of tying this to i386/amd64, because someone is eventually going to
// port this to ARM or some other RISC nonsense
#if UINTPTR_MAX == 0xffffffff
MurmurHash3_x86_128 ( &str, sizeof(str), 7904542L, &out );
#elif UINTPTR_MAX == 0xffffffffffffffff
MurmurHash3_x64_128 ( &str, sizeof(str), 5484754L, &out );
#else
#endif
return out;
}
bool fast_string_compare(fast_string t1, fast_string t2){
int i = strcmp(t1.c_str(), t2.c_str());
if (!i){
return true;
}
else{
return false;
}
}
}