use RageFile

This commit is contained in:
Glenn Maynard
2003-12-04 08:32:35 +00:00
parent 54b1610dc4
commit e176d533b1
+17 -25
View File
@@ -15,8 +15,6 @@
#include "RageUtil.h" #include "RageUtil.h"
#include "RageLog.h" #include "RageLog.h"
#include "RageFile.h" #include "RageFile.h"
#include <string.h>
#include <errno.h>
IniFile::IniFile(CString inipath) IniFile::IniFile(CString inipath)
{ {
@@ -37,24 +35,20 @@ void IniFile::SetPath(CString newpath)
// returns true if successful, false otherwise // returns true if successful, false otherwise
bool IniFile::ReadFile() bool IniFile::ReadFile()
{ {
LOG->Trace("INI: Reading '%s'",path.c_str() ); CHECKPOINT_M( ssprintf("Reading '%s'",path.c_str()) );
FILE *f = fopen(path, "r");
if (f == NULL) RageFile f;
if( !f.Open( path ) )
{ {
LOG->Trace("Reading '%s' failed: %s", path.c_str(), strerror(errno)); LOG->Trace( "Reading '%s' failed: %s", path.c_str(), f.GetError().c_str() );
error = ssprintf("Unable to open ini file: %s", strerror(errno)); error = ssprintf("Unable to open ini file: %s", f.GetError().c_str() );
return 0; return 0;
} }
CString keyname; CString keyname;
char buf[10240]; CString line;
while (fgets(buf, sizeof(buf), f)) while( f.GetLine(line) )
{ {
buf[sizeof(buf)-1]=0;
CString line(buf);
// LOG->Trace("Read line '%s'", line.c_str());
if(line.size() >= 3 && if(line.size() >= 3 &&
line[0] == '\xef' && line[0] == '\xef' &&
line[1] == '\xbb' && line[1] == '\xbb' &&
@@ -68,9 +62,6 @@ bool IniFile::ReadFile()
if (line == "") if (line == "")
continue; continue;
StripCrnl(line);
// LOG->Trace("Stripped: '%s'", line.c_str());
if (line.substr(0, 2) == "//" || line.substr(0) == "#") if (line.substr(0, 2) == "//" || line.substr(0) == "#")
continue; /* comment */ continue; /* comment */
@@ -90,31 +81,32 @@ bool IniFile::ReadFile()
} }
} }
fclose(f); return true;
return 1;
} }
// writes data stored in class to ini file // writes data stored in class to ini file
void IniFile::WriteFile() void IniFile::WriteFile()
{ {
FILE* fp = fopen( path, "w" ); RageFile f;
if( !f.Open( path, RageFile::WRITE ) )
if( fp == NULL ) {
LOG->Trace( "Writing '%s' failed: %s", path.c_str(), f.GetError().c_str() );
error = ssprintf("Unable to open ini file for writing: %s", f.GetError().c_str() );
return; return;
}
for (keymap::const_iterator k = keys.begin(); k != keys.end(); ++k) for (keymap::const_iterator k = keys.begin(); k != keys.end(); ++k)
{ {
if (k->second.empty()) if (k->second.empty())
continue; continue;
fprintf( fp, "[%s]\n", k->first.c_str() ); f.PutLine( ssprintf("[%s]", k->first.c_str()) );
for (key::const_iterator i = k->second.begin(); i != k->second.end(); ++i) for (key::const_iterator i = k->second.begin(); i != k->second.end(); ++i)
fprintf( fp, "%s=%s\n", i->first.c_str(), i->second.c_str() ); f.PutLine( ssprintf("%s=%s\n", i->first.c_str(), i->second.c_str()) );
fprintf( fp, "\n" ); f.PutLine( "" );
} }
fclose( fp );
} }
// deletes all stored ini data // deletes all stored ini data