allow writing to a buffer
This commit is contained in:
@@ -31,24 +31,15 @@ void IniFile::SetPath(CString newpath)
|
|||||||
path = newpath;
|
path = newpath;
|
||||||
}
|
}
|
||||||
|
|
||||||
// reads ini file specified using IniFile::SetPath()
|
|
||||||
// returns true if successful, false otherwise
|
void IniFile::ReadBuf( const CString &buf )
|
||||||
bool IniFile::ReadFile()
|
|
||||||
{
|
{
|
||||||
FILE *f = fopen(path, "r");
|
|
||||||
|
|
||||||
if (f == NULL)
|
|
||||||
{
|
|
||||||
error = ssprintf("Unable to open ini file: %s", strerror(errno));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
CString keyname;
|
CString keyname;
|
||||||
char buf[10240];
|
CStringArray lines;
|
||||||
while (fgets(buf, sizeof(buf), f))
|
split(buf, "\n", lines, true);
|
||||||
|
for( unsigned i = 0; i < lines.size(); ++i )
|
||||||
{
|
{
|
||||||
buf[sizeof(buf)-1]=0;
|
CString line = lines[i];
|
||||||
CString line(buf);
|
|
||||||
|
|
||||||
if(line.GetLength() >= 3 &&
|
if(line.GetLength() >= 3 &&
|
||||||
line[0] == '\xef' &&
|
line[0] == '\xef' &&
|
||||||
@@ -85,31 +76,71 @@ bool IniFile::ReadFile()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(f);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// writes data stored in class to ini file
|
void IniFile::WriteBuf( CString &buf ) const
|
||||||
void IniFile::WriteFile()
|
|
||||||
{
|
{
|
||||||
FILE* fp = fopen( path, "w" );
|
buf = "";
|
||||||
|
|
||||||
if( fp == NULL )
|
|
||||||
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", (const char *) k->first );
|
buf += ssprintf( "[%s]\r\n", (const char *) k->first );
|
||||||
|
|
||||||
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", (const char *)i->first, (const char *)i->second );
|
buf += ssprintf( "%s=%s\r\n", (const char *)i->first, (const char *)i->second );
|
||||||
|
|
||||||
fprintf( fp, "\n" );
|
buf += ssprintf( "\r\n" );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// reads ini file specified using IniFile::SetPath()
|
||||||
|
// returns true if successful, false otherwise
|
||||||
|
bool IniFile::ReadFile()
|
||||||
|
{
|
||||||
|
FILE *f = fopen(path, "rb");
|
||||||
|
|
||||||
|
if (f == NULL)
|
||||||
|
{
|
||||||
|
error = ssprintf("Unable to open ini file: %s", strerror(errno));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int size = GetFileSizeInBytes(path);
|
||||||
|
char *buf = new char[size+1];
|
||||||
|
int ret = fread(buf, 1, size, f);
|
||||||
|
if ( ret != size )
|
||||||
|
{
|
||||||
|
if( ferror(f) )
|
||||||
|
error = ssprintf("Unable to read ini file: %s", strerror(errno));
|
||||||
|
else
|
||||||
|
error = ssprintf("Unexpected eof in INI");
|
||||||
|
delete[] buf;
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ReadBuf(buf);
|
||||||
|
|
||||||
|
delete[] buf;
|
||||||
|
fclose(f);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// writes data stored in class to ini file
|
||||||
|
void IniFile::WriteFile()
|
||||||
|
{
|
||||||
|
CString buf;
|
||||||
|
WriteBuf( buf );
|
||||||
|
FILE* fp = fopen( path, "wb" );
|
||||||
|
|
||||||
|
if( fp == NULL )
|
||||||
|
return;
|
||||||
|
|
||||||
|
fwrite(buf.GetBuffer(), buf.GetLength(), 1, fp);
|
||||||
fclose( fp );
|
fclose( fp );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -58,6 +58,9 @@ public:
|
|||||||
//writes data stored in class to ini file
|
//writes data stored in class to ini file
|
||||||
void WriteFile();
|
void WriteFile();
|
||||||
|
|
||||||
|
void ReadBuf( const CString &buf );
|
||||||
|
void WriteBuf( CString &buf ) const;
|
||||||
|
|
||||||
//deletes all stored ini data
|
//deletes all stored ini data
|
||||||
void Reset();
|
void Reset();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user