/*********************************************************************** * copyright (c) 2005 Christian Dickmann * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 * * of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * * MA 02111-1307, USA. * * * * ------------------------------------------------------------------- * * project: NSIS-Project * * author : Christian Dickmann * ***********************************************************************/ #include "ConfigFileReader.h" #include "GistException.h" ConfigFileReader::ConfigFileReader() { fh = NULL; } ConfigFileReader::~ConfigFileReader() { } void ConfigFileReader::loadFile(char * filename) { fh = fopen(filename, "r"); if (fh == NULL) { throw new GistException("Unable to load Config File"); } } void ConfigFileReader::closeFile() { if (fh != NULL) { fclose(fh); }; fh = NULL; } void ConfigFileReader::trim(char * str) { unsigned int len = strlen(str); char * end = str + (len - 1); while (end >= str) { if (*end == ' ' || *end == '\t' || *end == '\r' || *end == '\n' ) { *end = 0; end--; len--; } else { break; }; }; if (end < str) { *str = 0; return; } char * start = str; while(start < end) { if (*start == ' ' || *start == '\t' || *start == '\r' || *start == '\n' ) { start++; } else { break; }; } if (start != str) { memmove(str, start, strlen(start) + 1); } } bool ConfigFileReader::readNextCommand(char * command, int maxCmdLength, char * value, int maxValueLength) { if (fh == NULL) { throw new GistException("No config file open"); }; char buffer[100]; while(true) { if (fgets(buffer, sizeof(buffer), fh) == NULL) { return false; } trim(buffer); if (strlen(buffer) == 0 || *buffer == '#') { continue; }; break; }; char * p = buffer; char * start = buffer; char * end = buffer + strlen(buffer); while (p < end) { if (*p == '=') { if ((p - start) > maxCmdLength) { throw new GistException("Command too long"); }; *p = 0; trim(start); strcpy(command, start); break; }; p++; }; if (p >= end) { throw new GistException("Parse error"); }; trim(p + 1); start = p + 1; p = start; if ((end - start) > maxValueLength) { throw new GistException("Value too long"); } strcpy(value, start); return true; } bool ConfigFileReader::isTrueAnswer(char * value) { if (strcmp(value, "true") == 0) { return true; } if (strcmp(value, "yes") == 0) { return true; } if (strcmp(value, "1") == 0) { return true; } return false; } bool ConfigFileReader::isFalseAnswer(char * value) { if (strcmp(value, "false") == 0) { return true; } if (strcmp(value, "no") == 0) { return true; } if (strcmp(value, "0") == 0) { return true; } return false; } bool ConfigFileReader::getBooleanAnswer(char * value) { if (isTrueAnswer(value)) { return true; } else if (isFalseAnswer(value)) { return false; } throw new GistException("Parameter not valid"); } std::vector ConfigFileReader::getInterfaces(char * str) { std::vector result; int len = strlen(str); char * end = str + len; char * p = str; while (p <= end) { if (*p == ' ' || *p == '\t') { if (str == p) { str++; } p++; continue; } if (*p == ',' || *p == ';' || *p == 0 || *p == '#') { char * newStart = p; while (p > str && (*(p - 1) == ' ' || *(p - 1) == '\t')) { p--; } int interfaceLength = p - str; if (interfaceLength > 0) { char * interfaceStr = new char[interfaceLength + 1]; memcpy(interfaceStr, str, interfaceLength); interfaceStr[interfaceLength] = 0; result.push_back(interfaceStr); } str = newStart + 1; if (*newStart == '#') { break; } } p++; } return result; }