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.

62 lines
2.3 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);
if (!bytesRead) {
std::cout << "no config" << std::endl;
// clean up!
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 512 bytes, increases if necessary.
char* directives = static_cast<char*>(tlsf_calloc(512, 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 >= 512){
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 in the first iteration
// how about an array of keys, and of values? where key[i] and value[i] are correspondent
while (tmp != nullptr){
// separate key and value here, will write up in the morning
// Writes the fully parsed configuration to a BrowserConfiguration
// object supplied by the user.
void CFGFileParser::WriteConfig(BrowserConfiguration &config){
config.Settings = cfg->Settings;