From 8c43857b739566be1e59d61aec192ef63e77cbb1 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Sat, 8 Sep 2012 16:08:29 +0000 Subject: Added the ItemToString() and ItemTypeToString() functions. Ref.: http://forum.mc-server.org/showthread.php?tid=434&pid=4506#pid4506 git-svn-id: http://mc-server.googlecode.com/svn/trunk@847 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- iniFile/iniFile.cpp | 147 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 88 insertions(+), 59 deletions(-) (limited to 'iniFile') diff --git a/iniFile/iniFile.cpp b/iniFile/iniFile.cpp index 530c05178..76f71284f 100644 --- a/iniFile/iniFile.cpp +++ b/iniFile/iniFile.cpp @@ -52,68 +52,97 @@ cIniFile::cIniFile( const string iniPath) bool cIniFile::ReadFile() { - // Normally you would use ifstream, but the SGI CC compiler has - // a few bugs with ifstream. So ... fstream used. - fstream f; - string line; - string keyname, valuename, value; - string::size_type pLeft, pRight; - - f.open( (FILE_IO_PREFIX + path).c_str(), ios::in); - if ( f.fail()) - return false; + // Normally you would use ifstream, but the SGI CC compiler has + // a few bugs with ifstream. So ... fstream used. + fstream f; + string line; + string keyname, valuename, value; + string::size_type pLeft, pRight; + + f.open( (FILE_IO_PREFIX + path).c_str(), ios::in); + if ( f.fail()) + return false; + + while (getline( f, line)) + { + // To be compatible with Win32, check for existence of '\r'. + // Win32 files have the '\r' and Unix files don't at the end of a line. + // Note that the '\r' will be written to INI files from + // Unix so that the created INI file can be read under Win32 + // without change. + unsigned int lineLength = line.length(); + if (lineLength == 0) + { + continue; + } + if ( line[lineLength - 1] == '\r') + { + line = line.substr( 0, lineLength - 1); + } + + if (line.length() == 0) + { + continue; + } + + // Check that the user hasn't opened a binary file by checking the first + // character of each line! + if ( !isprint( line[0])) + { + printf( "Failing on char %d\n", line[0]); + f.close(); + return false; + } + if (( pLeft = line.find_first_of(";#[=")) == string::npos) + { + continue; + } + + switch ( line[pLeft]) + { + case '[': + { + if ( + ((pRight = line.find_last_of("]")) != string::npos) && + (pRight > pLeft) + ) + { + keyname = line.substr( pLeft + 1, pRight - pLeft - 1); + AddKeyName( keyname); + } + break; + } + + case '=': + { + valuename = line.substr( 0, pLeft); + value = line.substr( pLeft + 1); + SetValue( keyname, valuename, value); + break; + } + + case ';': + case '#': + { + if (names.size() == 0) + { + HeaderComment( line.substr( pLeft + 1)); + } + else + { + KeyComment( keyname, line.substr( pLeft + 1)); + } + break; + } + } // switch (line[pLeft]) + } // while (getline()) - while( getline( f, line)) { - // To be compatible with Win32, check for existence of '\r'. - // Win32 files have the '\r' and Unix files don't at the end of a line. - // Note that the '\r' will be written to INI files from - // Unix so that the created INI file can be read under Win32 - // without change. - unsigned int lineLength = line.length(); - if(lineLength == 0) continue; - if ( line[lineLength - 1] == '\r') - line = line.substr( 0, lineLength - 1); - - if ( line.length()) { - // Check that the user hasn't opened a binary file by checking the first - // character of each line! - if ( !isprint( line[0])) { - printf( "Failing on char %d\n", line[0]); f.close(); - return false; - } - if (( pLeft = line.find_first_of(";#[=")) != string::npos) { - switch ( line[pLeft]) { - case '[': - if ((pRight = line.find_last_of("]")) != string::npos && - pRight > pLeft) { - keyname = line.substr( pLeft + 1, pRight - pLeft - 1); - AddKeyName( keyname); - } - break; - - case '=': - valuename = line.substr( 0, pLeft); - value = line.substr( pLeft + 1); - SetValue( keyname, valuename, value); - break; - - case ';': - case '#': - if ( !names.size()) - HeaderComment( line.substr( pLeft + 1)); - else - KeyComment( keyname, line.substr( pLeft + 1)); - break; + if (names.size() > 0) + { + return true; } - } - } - } - - f.close(); - if ( names.size()) - return true; - return false; + return false; } -- cgit v1.2.3