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