#include <tIniFile.h>
Public Member Functions | |
tIniFile () | |
~tIniFile () | |
tIniFile (const char *filename) | |
void | LoadFile (const char *filename) |
bool | HasGroup (const char *group) |
bool | HasKey (const char *group, const char *key) |
IniValue | GetGroup (const char *group) |
tString | GetValue (const char *group, const char *key) |
std::deque< tString > | GetGroups () |
std::deque< tString > | GetKeys (const char *group) |
void | Dump () |
Private Attributes | |
tIniGroups | m_Groups |
Definition at line 48 of file tIniFile.h.
tIniFile::tIniFile | ( | ) |
tIniFile::~tIniFile | ( | ) |
tIniFile::tIniFile | ( | const char * | filename | ) |
Definition at line 32 of file tIniFile.cpp.
References LoadFile().
00032 { 00033 LoadFile(filename); 00034 }
void tIniFile::LoadFile | ( | const char * | filename | ) |
Definition at line 98 of file tIniFile.cpp.
References tString::Len(), m_Groups, tString::ReadLine(), tString::StartsWith(), tString::StripWhitespace(), tString::StrPos(), tString::SubStr(), and tString::Trim().
Referenced by tIniFile().
00098 { 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 }
bool tIniFile::HasGroup | ( | const char * | group | ) |
Definition at line 51 of file tIniFile.cpp.
References m_Groups.
Referenced by GetGroup().
00051 { 00052 tIniGroups::iterator aGroup = m_Groups.find( tString(group) ); 00053 00054 if(aGroup != m_Groups.end()) 00055 return true; 00056 return false; 00057 }
bool tIniFile::HasKey | ( | const char * | group, | |
const char * | key | |||
) |
Definition at line 59 of file tIniFile.cpp.
References m_Groups.
Referenced by GetValue().
00059 { 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 }
IniValue tIniFile::GetGroup | ( | const char * | group | ) |
Definition at line 71 of file tIniFile.cpp.
References HasGroup(), and m_Groups.
Referenced by eMusicTrack::LoadAATrack().
00071 { 00072 if( HasGroup(group) ) { 00073 tIniGroups::iterator aGroup = m_Groups.find( tString(group) ); 00074 return (*aGroup).second; 00075 } 00076 return IniValue(); 00077 }
tString tIniFile::GetValue | ( | const char * | group, | |
const char * | key | |||
) |
Definition at line 79 of file tIniFile.cpp.
References HasKey(), and m_Groups.
Referenced by eMusicTrack::LoadAATrack().
00079 { 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 }
std::deque< tString > tIniFile::GetGroups | ( | ) |
Definition at line 40 of file tIniFile.cpp.
References m_Groups.
00040 { 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 }
std::deque<tString> tIniFile::GetKeys | ( | const char * | group | ) |
void tIniFile::Dump | ( | ) |
Definition at line 89 of file tIniFile.cpp.
References m_Groups.
00089 { 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 }
tIniGroups tIniFile::m_Groups [private] |
Definition at line 67 of file tIniFile.h.
Referenced by Dump(), GetGroup(), GetGroups(), GetValue(), HasGroup(), HasKey(), and LoadFile().