#include "configFile.h" static char config_divider[] = ":"; static char config_eol[] = "\x0a"; char *configureFile::filename(char *configFile) { static char filename[512]; char *leaf; GetModuleFileName(0, filename, 512); leaf = strrchr(filename, '\\'); if (!leaf) { return 0; } leaf ++; strcpy(leaf, configFile); return filename; } bool configureFile::concat(char *label, char *buffer, unsigned int &space) { int loop; // iterate through the list to find it for (loop = 0; loop < configstr->count; loop ++) { if (strcmp(configstr->item[loop].label, label) == 0) { break; } } // abort if not found if (loop == configstr->count) { return false; } // don't concatenate if there's no space if (strlen(configstr->item[loop].data) > space) { return false; } strcat(buffer, configstr->item[loop].data); space -= strlen(configstr->item[loop].data); return true; } string configureFile::getString(string label) { for (int loop = 0; loop < configstr->count; loop ++) { if (strcmp(configstr->item[loop].label, label.c_str()) == 0) { return configstr->item[loop].data; } } return ""; } void configureFile::get(char *label, char **to) { int loop; for (loop = 0; loop < configstr->count; loop ++) { if (strcmp(configstr->item[loop].label, label) == 0) { *to = new char[strlen(configstr->item[loop].data) + 1]; if (to) { strcpy(*to, configstr->item[loop].data); return; } else { //Acorn: werr(0, msgs_lookup("MemConfg")); } } } *to = NULL; return; } bool configureFile::put(char *label, char *value) { int loop; void *newstr; for (loop = 0; loop < configstr->count; loop ++) { if (strcmp(configstr->item[loop].label, label) == 0) { strcpy(configstr->item[loop].data, value); return true; } } newstr = realloc(configstr, sizeof(config_str) + sizeof(config_item) * configstr->count); if (!newstr) { return false; } strcpy(configstr->item[configstr->count].label, label); strcpy(configstr->item[configstr->count].data, value); configstr->count ++; return true; } /** * Scan one line of the configuration file */ bool configureFile::scan(char *block, unsigned int &pos, unsigned int length) { int off = 0; void *newstr; if (pos == length) { return true; } // if it's a comment line, skip it straightaway if (block[pos] == '#') { while (block[pos] >= ' ') { block ++; } while (block[pos] < ' ') { block ++; if (block[pos-1] == '\0') { break; } } return true; } // otherwise create a new struct to cope with the list of configuration items newstr = realloc(configstr, sizeof(config_str) + sizeof(config_item) * configstr->count); if (!newstr) { free(configstr); configstr = NULL; return false; } configstr = (config_str *) newstr; // scan to the colon to get the label while (pos < length && block[pos] != ':') { configstr->item[configstr->count].label[off] = block[pos]; off ++; pos ++; } configstr->item[configstr->count].label[off] = '\0'; // read until we get past any leading whitespace or tabs off = 0; if (pos < length) { do { pos ++; } while (block[pos] <= ' ' && block[pos] != '\x0a' && block[pos] != '\x0d'); } // read until we reach a "magic" character - this is where we read the value while(pos < length && block[pos] >= ' ') { configstr->item[configstr->count].data[off] = block[pos]; pos ++; off ++; } configstr->item[configstr->count].data[off] = '\0'; // skip any magic characters if (pos < length) { do { pos ++; } while (block[pos] <= ' ' && pos < length); } // remember that we've added to the list configstr->count ++; return true; } bool configureFile::store(char *name) { char * varval; int loop; FILE * fhandle; varval = this->filename(name); fhandle = fopen(varval, "wb+"); if (fhandle) { loop = 0; while(loop < configstr->count) { fprintf(fhandle, "%s%s%s%s", configstr->item[loop].label, config_divider, configstr->item[loop].data, config_eol); loop ++; } fclose(fhandle); } //Acorn: free(varval); return TRUE; } configureFile::configureFile(char *name) { unsigned int length, newlength; unsigned int position; char * varval; char * block; FILE * fhandle; configstr = (config_str *) malloc(sizeof(config_str)); if (!configstr) { this->valid = false; return; } configstr->count = 0; varval = this->filename(name); fhandle = fopen(varval, "rb"); if (fhandle) { length = 0; //PC Method for fetching file block = (char *) malloc(20); while ((newlength = fread(&block[length], sizeof(char), 20, fhandle)) != 0) { void *newblock; length += newlength; newblock = realloc(block, length + 20); if (!newblock) { free(block); this->valid = false; fclose(fhandle); return; } block = (char *) newblock; } //end of PC method position = 0; while (position < length) { this->scan(block, position, length); } fclose(fhandle); free(block); this->valid = true; } else { // No config file MessageBox(0, "oops", "oops", MB_OK); this->valid = false; } //Acorn: free(varval); } configureFile::~configureFile() { if (configstr) { free(configstr); configstr = NULL; } }