00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
00081 if( HasKey(group, key) ) {
00082
00083 return m_Groups[tString(group)][tString(key)];
00084 }
00085
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
00106 } else {
00107
00108 return;
00109 }
00110 while( ! thefile.eof() && thefile.good() ) {
00111 tString oneLine;
00112
00113 oneLine.ReadLine( thefile );
00114
00115 oneLine = oneLine.Trim();
00116
00117 if( !oneLine.StartsWith("#") ) {
00118
00119 tString oneLine1(oneLine.StripWhitespace());
00120
00121 if(oneLine1.Len() < 2) continue;
00122
00123
00124 if(oneLine1.StartsWith("[") && oneLine1.EndsWith("]") ) {
00125 currentGroup = oneLine1.SubStr(1, oneLine.StrPos("]")-1);
00126 autokey = 0;
00127 continue;
00128 }
00129
00130 if(currentGroup.Len() < 1) continue;
00131
00132
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
00141
00142 char buf[30];
00143 sprintf(buf, "%d", autokey);
00144 key = buf;
00145 value = oneLine;
00146 autokey++;
00147 }
00148
00149
00150 m_Groups[currentGroup][tString(key)] = tString(value);
00151 }
00152 }
00153 }
00154