Important update: (untested)
- Switch from FILE pointers to file descriptors (e.g. fopen() -> open()) - Replace internal buffer with CShiftBuffer
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
@@ -23,9 +24,9 @@ CConfigCore::CConfigCore()
|
||||
RootObject->Type = jtObject;
|
||||
|
||||
// File Operation
|
||||
InputFile = NULL;
|
||||
OutputFile = NULL;
|
||||
BufLen = 500;
|
||||
InputHandle = -1;
|
||||
OutputHandle = -1;
|
||||
|
||||
Buffer = NULL;
|
||||
BufEnd = NULL;
|
||||
Level = 0;
|
||||
@@ -495,7 +496,7 @@ bool CConfigCore::LoadFile( const char * FilePath, int pBufLen )
|
||||
}
|
||||
|
||||
// Open file
|
||||
if (!(InputFile = fopen( FilePath, "r" ))) {
|
||||
if (!(InputHandle = open( FilePath, O_RDONLY ))) {
|
||||
Error = true;
|
||||
sprintf( ErrorText, "Could not open file" );
|
||||
return false;
|
||||
@@ -540,7 +541,7 @@ bool CConfigCore::LoadFile( const char * FilePath, int pBufLen )
|
||||
}
|
||||
|
||||
// Success
|
||||
fclose( InputFile );
|
||||
close( InputHandle );
|
||||
FreeBuffer();
|
||||
return true;
|
||||
}
|
||||
@@ -553,14 +554,12 @@ bool CConfigCore::CreateBuffer( int pBufLen )
|
||||
return false;
|
||||
|
||||
// Create buffer
|
||||
BufLen = pBufLen;
|
||||
Buffer = (char*)malloc( BufLen+1 );
|
||||
Buffer = new CShiftBuffer( pBufLen );
|
||||
|
||||
// Reset markers
|
||||
BufEnd = Buffer;
|
||||
*BufEnd = 0;
|
||||
BufPos = Buffer;
|
||||
LineMark = Buffer;
|
||||
Buffer->PeekDirect( &BufPos, 0 );
|
||||
BufEnd = BufPos;
|
||||
LineMark = BufPos;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -568,19 +567,13 @@ bool CConfigCore::CreateBuffer( int pBufLen )
|
||||
|
||||
bool CConfigCore::FillBuffer()
|
||||
{
|
||||
int BufCount = 0;
|
||||
|
||||
// Read from file
|
||||
BufCount = fread( Buffer, 1, BufLen, InputFile );
|
||||
if (BufCount < 1) {
|
||||
return false;
|
||||
}
|
||||
Buffer->ReadFromFD( InputHandle );
|
||||
|
||||
// Update Markers
|
||||
BufEnd = Buffer + BufCount;
|
||||
*BufEnd = 0;
|
||||
BufPos = Buffer;
|
||||
LineMark = Buffer;
|
||||
Buffer->PeekDirect( &BufEnd, Buffer->Len() );
|
||||
Buffer->PeekDirect( &BufPos, 0 );
|
||||
LineMark = BufPos;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -590,7 +583,7 @@ void CConfigCore::FreeBuffer()
|
||||
{
|
||||
// Destroy buffer
|
||||
if (Buffer) {
|
||||
free( Buffer );
|
||||
delete Buffer;
|
||||
Buffer = NULL;
|
||||
|
||||
// Update Markers
|
||||
@@ -598,7 +591,6 @@ void CConfigCore::FreeBuffer()
|
||||
BufPos = NULL;
|
||||
LineMark = NULL;
|
||||
}
|
||||
BufLen = 0;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
@@ -874,7 +866,7 @@ bool CConfigCore::SaveFile( const char * FilePath, const int Indent )
|
||||
return false;
|
||||
|
||||
// Open file
|
||||
if (!(OutputFile = fopen( FilePath, "w" )))
|
||||
if (!(OutputHandle = open( FilePath, O_WRONLY )))
|
||||
return false;
|
||||
|
||||
// Save Root object
|
||||
@@ -882,7 +874,7 @@ bool CConfigCore::SaveFile( const char * FilePath, const int Indent )
|
||||
SaveObject( RootObject, Indent );
|
||||
|
||||
// Close file
|
||||
fclose( OutputFile );
|
||||
close( OutputHandle );
|
||||
return false;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
@@ -892,7 +884,7 @@ bool CConfigCore::SaveObject( TConfigParam * Object, const int Indent )
|
||||
TConfigParam * Param;
|
||||
|
||||
// Opening brace
|
||||
fprintf( OutputFile, "{\n" );
|
||||
dprintf( OutputHandle, "{\n" );
|
||||
|
||||
// Extend spacer
|
||||
Level++;
|
||||
@@ -904,26 +896,26 @@ bool CConfigCore::SaveObject( TConfigParam * Object, const int Indent )
|
||||
for (Param = Object->FirstObject; Param != NULL; (Param = Param->Next))
|
||||
{
|
||||
// Write parameter name
|
||||
fprintf( OutputFile, "%s\"%s\": ", Spacer, Param->Name );
|
||||
dprintf( OutputHandle, "%s\"%s\": ", Spacer, Param->Name );
|
||||
|
||||
switch (Param->Type)
|
||||
{
|
||||
case jtNull :
|
||||
fprintf( OutputFile, "%s,\n", "null" );
|
||||
dprintf( OutputHandle, "%s,\n", "null" );
|
||||
break;
|
||||
|
||||
case jtBool :
|
||||
case jtInt :
|
||||
case jtFloat :
|
||||
fprintf( OutputFile, "%s,\n", Param->Value );
|
||||
dprintf( OutputHandle, "%s,\n", Param->Value );
|
||||
break;
|
||||
|
||||
case jtString :
|
||||
fprintf( OutputFile, "\"%s\",\n", Param->Value );
|
||||
dprintf( OutputHandle, "\"%s\",\n", Param->Value );
|
||||
break;
|
||||
|
||||
case jtArray :
|
||||
fprintf( OutputFile, "[],\n" );
|
||||
dprintf( OutputHandle, "[],\n" );
|
||||
break;
|
||||
|
||||
case jtObject :
|
||||
@@ -939,10 +931,10 @@ bool CConfigCore::SaveObject( TConfigParam * Object, const int Indent )
|
||||
|
||||
// Closing brace
|
||||
if (Level == 0) {
|
||||
fprintf( OutputFile, "%s}\n", Spacer );
|
||||
dprintf( OutputHandle, "%s}\n", Spacer );
|
||||
}
|
||||
else {
|
||||
fprintf( OutputFile, "%s},\n", Spacer );
|
||||
dprintf( OutputHandle, "%s},\n", Spacer );
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user