src/SAL_config.cpp

00001 /*
00002     SAL - Simple Application Library
00003     Copyright (C) 2006-2006 Kronon
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Lesser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2.1 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019     Kronon
00020     kronon88@users.sourceforge.net
00021 */
00022 
00023 //SAL config file parser
00024 //Created by Kronon
00025 
00026 //The commands that read the config files
00027 
00028 #include "SAL_config.h"
00029 
00030 //Doxy gen information
00031 
00041 salConf::salConf(const char * url, int size)
00042 {
00043         conf_nodes = new conf_node[size];
00044         length=size;
00045         
00046         ifstream config_file(url);
00047         string line, option_name, option_value;
00048         int val_start;
00049         
00050         //Search for config options
00051         while (config_file.good())      //Read all the lines in the config file
00052         {
00053                 getline( config_file, line);
00054                 //Check if first char = # because then the line is tagged out
00055                 if(line.substr(0,1) == "#")
00056                         continue;       //Get next line
00057                 
00058                 //Check if the option exist and if so then use the option
00059                 val_start = line.find(" ");
00060                 option_name = line.substr(0, val_start);
00061                 for(int i = 0; i < length; i++) //Find the option in the option array or find the next empty spot
00062                 {
00063                         if(conf_nodes[i].name == option_name || conf_nodes[i].name == "")
00064                         {
00065                                 conf_nodes[i].name = option_name;       //TODO: check if string gets copied
00066                                 conf_nodes[i].value=line.substr(val_start+1);
00067                                 break;
00068                         }
00069                 }
00070         }
00071         config_file.close();
00072 }
00073 
00079 salConf::~salConf()
00080 {
00081         //TODO: why crash?
00082 //      delete conf_nodes;
00083 }
00084 
00092 //Get option value as int
00093 int salConf::getInt(string option_name)
00094 {
00095         //Find the correct element
00096         for(int i = 0; i < length; i++) //Find the option in the option array or find the next empty spot
00097         {
00098                 if(conf_nodes[i].name == option_name)
00099                 {
00100                         return(atoi(conf_nodes[i].value.c_str()));
00101                 }
00102         }
00103         return -1;
00104 }
00105 
00113 string salConf::getString(string option_name)
00114 {
00115         //Find the correct element
00116         for(int i = 0; i < length; i++) //Find the option in the option array or find the next empty spot
00117         {
00118                 if(conf_nodes[i].name == option_name)
00119                 {
00120                         return(conf_nodes[i].value);
00121                 }
00122         }
00123         return "";
00124 }
00125 
00134 TTF_Font * salConf::getFont(string option_uri, string option_size)
00135 {
00136         const char *uri=NULL;
00137         int size=0;
00138         //Find the correct elements
00139         for(int i = 0; i < length; i++) //Find the option in the option array or find the next empty spot
00140         {
00141                 if(conf_nodes[i].name == option_uri)
00142                 {
00143                         uri = conf_nodes[i].value.c_str();
00144                 }
00145                 else if(conf_nodes[i].name == option_size)
00146                 {
00147                         size = atoi(conf_nodes[i].value.c_str());
00148                 }
00149         }
00150         return salLoadFont(uri, size);
00151 }
00152 
00160 Uint32 salConf::getColor(string option_name)
00161 {
00162         string color_code="";
00163         //Find the correct element
00164         for(int i = 0; i < length; i++) //Find the option in the option array or find the next empty spot
00165         {
00166                 if(conf_nodes[i].name == option_name)
00167                 {
00168                         color_code = conf_nodes[i].value;
00169                 }
00170         }
00171         return convert_color(color_code);
00172 }
00173 
00182 //TODO: modify header file
00183 Uint32 salConf::convert_color(string color)
00184 {       
00185         //TODO:Build the convertor
00186         if(color == "COLOR_BLACK")
00187                         return COLOR_BLACK;
00188         if(color == "COLOR_WHITE")
00189                         return COLOR_WHITE;
00190         if(color == "COLOR_DARKGREY")
00191                         return COLOR_DARKGREY;
00192         if(color == "COLOR_LIGHTGREY")
00193                         return COLOR_LIGHTGREY;
00194         if(color == "COLOR_ORANGE")
00195                         return COLOR_ORANGE;
00196         if(color == "COLOR_LIGHTORANGE")
00197                         return COLOR_LIGHTORANGE;
00198         if(color == "COLOR_DARKORANGE")
00199                         return COLOR_DARKORANGE;
00200         if(color == "COLOR_RED")
00201                         return COLOR_RED;
00202         if(color == "COLOR_LIGHTRED")
00203                         return COLOR_LIGHTRED;
00204         if(color == "COLOR_DARKRED")
00205                         return COLOR_DARKRED;
00206         if(color == "COLOR_GREEN")
00207                         return COLOR_GREEN;
00208         if(color == "COLOR_BLUE")
00209                         return COLOR_BLUE;
00210         if(color == "COLOR_YELLOW")
00211                         return COLOR_YELLOW;
00212         return COLOR_BLACK;
00213 }
00214