src/tools/tIniFile.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023 
00024 ***************************************************************************
00025 
00026 */
00027 
00028 #include "tIniFile.h"
00029 #include <iostream>
00030 #include <fstream>
00031 
00032 tIniFile::tIniFile(const char* filename) {
00033     LoadFile(filename);
00034 }
00035 
00036 tIniFile::~tIniFile() {
00037     // do nothing destructor
00038 }
00039 
00040 std::deque<tString> tIniFile::GetGroups() {
00041     tIniGroups::iterator aGroup;
00042     std::deque<tString> retList;
00043 
00044     for( aGroup = m_Groups.begin(); aGroup != m_Groups.end(); aGroup++) {
00045         retList.push_back( tString((*aGroup).first) );
00046     }
00047 
00048     return retList;
00049 }
00050 
00051 bool tIniFile::HasGroup(const char* group) {
00052     tIniGroups::iterator aGroup = m_Groups.find( tString(group) );
00053 
00054     if(aGroup != m_Groups.end())
00055         return true;
00056     return false;
00057 }
00058 
00059 bool tIniFile::HasKey(const char* group, const char* key) {
00060     tIniGroups::iterator aGroup = m_Groups.find( tString(group) );
00061 
00062     if(aGroup != m_Groups.end()) {
00063         IniValue::iterator aKey = (*aGroup).second.find( tString(key) );
00064         if( aKey != (*aGroup).second.end() ) {
00065             return true;
00066         }
00067     }
00068     return false;
00069 }
00070 
00071 IniValue tIniFile::GetGroup(const char* group) {
00072     if( HasGroup(group) ) {
00073         tIniGroups::iterator aGroup = m_Groups.find( tString(group) );
00074         return (*aGroup).second;
00075     }
00076     return IniValue();
00077 }
00078 
00079 tString tIniFile::GetValue(const char* group, const char* key) {
00080     //std::cout << "Getting value " << key << " from group " << group << "\n";
00081     if( HasKey(group, key) ) {
00082         //std::cout << "Found " << m_Groups[tString(group)][tString(key)] << "\n";
00083         return m_Groups[tString(group)][tString(key)];
00084     }
00085     //std::cout << "Not found!\n";
00086     return tString();
00087 }
00088 
00089 void tIniFile::Dump() {
00090     tIniGroups::iterator aGroup;
00091 
00092     std::cout << "Number of groups: " << m_Groups.size() << "\n";
00093     for(aGroup = m_Groups.begin(); aGroup != m_Groups.end(); aGroup++) {
00094         std::cout << "Key: " << (*aGroup).first << "\n";
00095     }
00096 }
00097 
00098 void tIniFile::LoadFile(const char* filename) {
00099     std::ifstream thefile(filename);
00100     tString currentGroup;
00101     int autokey = 0;
00102     tString key;
00103     tString value;
00104     if(thefile.good() ) {
00105         //std::cout << "Opened ini file\n";
00106     } else {
00107         //std::cout << "Couldn't open ini file: " << filename << "\n";
00108         return;
00109     }
00110     while( ! thefile.eof() && thefile.good() ) {
00111         tString oneLine;
00112 
00113         oneLine.ReadLine( thefile );
00114 
00115         oneLine = oneLine.Trim();
00116         // Ignore comments, comments can only be on lines by themselves
00117         if( !oneLine.StartsWith("#") ) {
00118             // First trim whitespace and see how big the line is
00119             tString oneLine1(oneLine.StripWhitespace()); //.StripWhitespace());
00120 
00121             if(oneLine1.Len() < 2) continue;
00122 
00123             // Now see if it's a group.  We use the stripped version for this
00124             if(oneLine1.StartsWith("[") && oneLine1.EndsWith("]") ) {
00125                 currentGroup = oneLine1.SubStr(1, oneLine.StrPos("]")-1);
00126                 autokey = 0;
00127                 continue;
00128             }
00129             // At this point, if we have no group, we ignore the whole line
00130             if(currentGroup.Len() < 1) continue;
00131 
00132             // Now see if it's a key=value pair.  We go back to the original line
00133             int equalPos = oneLine.StrPos("=");
00134             if(equalPos != -1) {
00135                 key = oneLine.SubStr(0, equalPos);
00136                 value = oneLine.SubStr(equalPos + 1);
00137                 key = key.StripWhitespace();
00138                 value = value.Trim();
00139             } else {
00140                 // If it's not a group, we have a current group, and it's not key=value,
00141                 // then we need to use our own generated keys and store the whole line
00142                 char buf[30];
00143                 sprintf(buf, "%d", autokey);
00144                 key = buf;
00145                 value = oneLine;
00146                 autokey++;
00147             }
00148             // Store the key value in the current group
00149             //std::cout << "Group: " << currentGroup << ", " << key << "=" << value << "\n";
00150             m_Groups[currentGroup][tString(key)] = tString(value);
00151         }
00152     }
00153 }
00154 

Generated on Sat Mar 15 22:56:00 2008 for Armagetron Advanced by  doxygen 1.5.4