Important Update: (ConfigCore)

- Implemented full support for multi-layer objects
- Implemented RootObject for all other elements
- Added method CreateParam()
- Converted GetParam methods from inline to normal methods
- Added GetParent method
- Use Object->Len to count no of child objects
This commit is contained in:
Charl Wentzel
2017-03-18 23:03:51 +02:00
parent 508cf0fb0d
commit bb07ef63bf
2 changed files with 443 additions and 173 deletions

View File

@@ -19,18 +19,27 @@
CConfigCore::CConfigCore() CConfigCore::CConfigCore()
{ {
// Parameter tree // Parameter tree
FirstParam = NULL; RootObject = CreateParam( NULL );
RootObject->Type = jtObject;
// File Operation // File Operation
InputFile = NULL; InputFile = NULL;
OutputFile = NULL;
BufLen = 500; BufLen = 500;
Buffer = NULL; Buffer = NULL;
BufEnd = NULL; BufEnd = NULL;
Level = 0;
// Parsing operation // Parsing operation
BufPos = NULL; BufPos = NULL;
LineMark = NULL; LineMark = NULL;
LineNo = 0; LineNo = 0;
// Printing operation
Spacer[0] = 0;
SpacerLen = 0;
// Error reporting
Error = false; Error = false;
ErrorText[0] = 0; ErrorText[0] = 0;
} }
@@ -39,8 +48,22 @@ CConfigCore::CConfigCore()
CConfigCore::~CConfigCore() CConfigCore::~CConfigCore()
{ {
// Destroy Params // Destroy Params
while (FirstParam) DeleteAll();
DestroyParam( &FirstParam ); DestroyParam( &RootObject );
}
//---------------------------------------------------------------------------
TConfigParam * CConfigCore::CreateParam( const char * Name )
{
TConfigParam * Param;
Param = (TConfigParam *)calloc( 1, sizeof(TConfigParam) );
if (Name && *Name)
{
Param->Name = (char *)malloc( strlen( Name )+1 );
strcpy( Param->Name, Name );
}
return Param;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@@ -60,6 +83,9 @@ bool CConfigCore::DestroyParam( TConfigParam ** Param )
free( (*Param)->Name ); free( (*Param)->Name );
if ((*Param)->Value) if ((*Param)->Value)
free( (*Param)->Value ); free( (*Param)->Value );
while ((*Param)->FirstObject) {
DestroyParam( &((*Param)->FirstObject) );
}
free( *Param ); free( *Param );
@@ -70,24 +96,111 @@ bool CConfigCore::DestroyParam( TConfigParam ** Param )
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool CConfigCore::DeleteParam( const char * Name ) bool CConfigCore::GetParent( const char * ParentPath, TConfigParam **Parent )
{
TConfigParam * Param;
char * ParentName;
char * Pos;
// Validate
if (!Parent) {
return false;
}
// Set first Parent
*Parent = RootObject;
// Split path
Pos = (char*)ParentPath;
while (*Pos)
{
// Set Name start
ParentName = Pos;
// Find delimeter
while (*Pos && (*Pos != '/')) {
Pos++;
}
// Zero terminate name
if (*Pos) {
*Pos = 0;
Pos++;
}
// Find next parent
Param = (*Parent)->FirstObject;
while (Param && strcasecmp( Param->Name, ParentName )) {
Param = Param->Next;
}
if (!Param) {
return false;
}
// Set Next parent
*Parent = Param;
}
return true;
}
//---------------------------------------------------------------------------
TConfigParam * CConfigCore::GetParam( TConfigParam * Parent, const char * Name )
{
TConfigParam * Param;
// Validate
if (!Parent || !Name || !*Name) {
return NULL;
}
// Get Param
Param = Parent->FirstObject;
while (Param && strcasecmp( Param->Name, Name ))
Param = Param->Next;
return Param;
}
//---------------------------------------------------------------------------
TConfigParam ** CConfigCore::GetParamPtr( TConfigParam * Parent, const char * Name )
{ {
TConfigParam ** Param; TConfigParam ** Param;
// Check if exists // Validate
if (!(Param = GetParamPtr( Name ))) if (!Parent || !Name || !*Name) {
return false; return NULL;
}
// Destory // Get Param
Param = &(Parent->FirstObject);
while (Param && strcasecmp( (*Param)->Name, Name ))
Param = &((*Param)->Next);
return Param;
}
//---------------------------------------------------------------------------
bool CConfigCore::DeleteParam( const char * ParentPath, const char * Name )
{
TConfigParam * Parent;
TConfigParam ** Param;
// Check if exists
if (!GetParent( ParentPath, &Parent ) ||
!(Param = GetParamPtr( Parent, Name ))) {
return false;
}
// Destroy
DestroyParam( Param ); DestroyParam( Param );
Parent->Len--;
return true; return true;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool CConfigCore::DeleteAll() bool CConfigCore::DeleteAll()
{ {
while (FirstParam) while (RootObject->FirstObject) {
DestroyParam( &FirstParam ); DestroyParam( &(RootObject->FirstObject) );
}
return true; return true;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@@ -103,87 +216,163 @@ bool CConfigCore::SetParam( TConfigParam * Param, EJSONtype Type, const char *
Param->Type = Type; Param->Type = Type;
// Set new value // Set new value
if (Type == jtNull) { switch (Type) {
case jtNull:
case jtObject:
case jtArray:
Param->Len = 0; Param->Len = 0;
Param->Value = NULL; Param->Value = NULL;
} break;
else {
case jtString:
case jtFloat:
case jtInt:
case jtBool:
Param->Len = (Len == -1)? strlen(Value) : Len; Param->Len = (Len == -1)? strlen(Value) : Len;
Param->Value = (char *)malloc( sizeof(Param->Len+1) ); Param->Value = (char *)malloc( sizeof(Param->Len+1) );
if (Value) {
memcpy( Param->Value, Value, Param->Len+1 ); memcpy( Param->Value, Value, Param->Len+1 );
} else {
memset( Param->Value, 0, Param->Len+1 );
}
Param->Value[Param->Len] = 0; Param->Value[Param->Len] = 0;
break;
} }
return true; return true;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool CConfigCore::SetParamStr( const char * Name, const char * Value, const int Len ) bool CConfigCore::SetParamObject( const char * ParentPath, const char * Name )
{ {
TConfigParam * Param; TConfigParam * Parent;
TConfigParam ** Param;
// Validate
if (!Name || !*Name || !GetParent( ParentPath, &Parent )) {
return false;
}
// Create new param if it doesn't exist // Create new param if it doesn't exist
Param = FindCreateParam( Name ); if (!(Param = GetParamPtr( Parent, Name ))) {
SetParam( Param, jtString, Value, Len ); *Param = CreateParam( Name );
}
SetParam( *Param, jtObject, NULL, -1 );
return true; return true;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool CConfigCore::SetParamInt( const char * Name, const long Value ) bool CConfigCore::SetParamStr( const char * ParentPath, const char * Name, const char * Value, const int Len )
{ {
TConfigParam * Param = NULL; TConfigParam * Parent;
TConfigParam ** Param;
// Validate
if (!Name || !*Name || !GetParent( ParentPath, &Parent )) {
return false;
}
// Create new param if it doesn't exist
if (!(Param = GetParamPtr( Parent, Name ))) {
*Param = CreateParam( Name );
}
SetParam( *Param, jtString, Value, Len );
return true;
}
//---------------------------------------------------------------------------
bool CConfigCore::SetParamInt( const char * ParentPath, const char * Name, const long Value )
{
TConfigParam * Parent;
TConfigParam ** Param;
char ValueStr[50]; char ValueStr[50];
// Validate
if (!Name || !*Name || !GetParent( ParentPath, &Parent )) {
return false;
}
// Create new param if it doesn't exist // Create new param if it doesn't exist
Param = FindCreateParam( Name ); if (!(Param = GetParamPtr( Parent, Name ))) {
*Param = CreateParam( Name );
}
sprintf( ValueStr, "%ld", Value ); sprintf( ValueStr, "%ld", Value );
SetParam( Param, jtInt, ValueStr, -1 ); SetParam( *Param, jtInt, ValueStr, -1 );
return true;
return false;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool CConfigCore::SetParamFloat( const char * Name, const double Value ) bool CConfigCore::SetParamFloat( const char * ParentPath, const char * Name, const double Value )
{ {
TConfigParam * Param = NULL; TConfigParam * Parent;
TConfigParam ** Param;
char ValueStr[50]; char ValueStr[50];
// Validate
if (!Name || !*Name || !GetParent( ParentPath, &Parent )) {
return false;
}
// Create new param if it doesn't exist // Create new param if it doesn't exist
Param = FindCreateParam( Name ); if (!*(Param = GetParamPtr( Parent, Name ))) {
*Param = CreateParam( Name );
}
sprintf( ValueStr, "%lf", Value ); sprintf( ValueStr, "%lf", Value );
SetParam( Param, jtFloat, ValueStr, -1 ); SetParam( *Param, jtFloat, ValueStr, -1 );
return false; return true;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool CConfigCore::SetParamBool( const char * Name, const bool Value ) bool CConfigCore::SetParamBool( const char * ParentPath, const char * Name, const bool Value )
{ {
TConfigParam * Param = NULL; TConfigParam * Parent;
TConfigParam ** Param;
// Validate
if (!Name || !*Name || !GetParent( ParentPath, &Parent )) {
return false;
}
// Create new param if it doesn't exist // Create new param if it doesn't exist
Param = FindCreateParam( Name ); if (!*(Param = GetParamPtr( Parent, Name ))) {
SetParam( Param, jtBool, ((Value == 0)? "false" : "true"), -1 ); *Param = CreateParam( Name );
return false; }
SetParam( *Param, jtBool, ((Value == 0)? "false" : "true"), -1 );
return true;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool CConfigCore::SetParamNull( const char * Name ) bool CConfigCore::SetParamNull( const char * ParentPath, const char * Name )
{ {
TConfigParam * Param = NULL; TConfigParam * Parent;
TConfigParam ** Param;
// Validate
if (!Name || !*Name || !GetParent( ParentPath, &Parent )) {
return false;
}
// Create new param if it doesn't exist // Create new param if it doesn't exist
Param = FindCreateParam( Name ); if (!*(Param = GetParamPtr( Parent, Name ))) {
SetParam( Param, jtNull, NULL, -1 ); *Param = CreateParam( Name );
return false; }
SetParam( *Param, jtNull, NULL, -1 );
return true;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
const EJSONtype CConfigCore::GetParamType( const char * Name ) const EJSONtype CConfigCore::GetParamType( const char * ParentPath, const char * Name )
{ {
TConfigParam * Parent;
TConfigParam * Param; TConfigParam * Param;
if ((Param = GetParam( Name ))) { // Validate
if (!Name || !*Name || !GetParent( ParentPath, &Parent )) {
return jtNull;
}
// Return type
if ((Param = GetParam( Parent, Name ))) {
return Param->Type; return Param->Type;
} }
else { else {
@@ -192,11 +381,18 @@ const EJSONtype CConfigCore::GetParamType( const char * Name )
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
const char * CConfigCore::GetParamStr( const char * Name, const char * Default ) const char * CConfigCore::GetParamStr( const char * ParentPath, const char * Name, const char * Default )
{ {
TConfigParam * Parent;
TConfigParam * Param; TConfigParam * Param;
if ((Param = GetParam( Name )) && (Param->Type == jtString)) { // Validate
if (!Name || !*Name || !GetParent( ParentPath, &Parent )) {
return Default;
}
// Return value
if ((Param = GetParam( Parent, Name )) && (Param->Type == jtString)) {
return Param->Value; return Param->Value;
} }
else { else {
@@ -205,11 +401,18 @@ const char * CConfigCore::GetParamStr( const char * Name, const char * Default )
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
const char * CConfigCore::GetParamStr( const char * Name, int &Len, const char * Default ) const char * CConfigCore::GetParamStr( const char * ParentPath, const char * Name, int &Len, const char * Default )
{ {
TConfigParam * Parent;
TConfigParam * Param; TConfigParam * Param;
if ((Param = GetParam( Name )) && (Param->Type == jtString)) { // Validate
if (!Name || !*Name || !GetParent( ParentPath, &Parent )) {
return Default;
}
// Return value
if ((Param = GetParam( Parent, Name )) && (Param->Type == jtString)) {
Len = Param->Len; Len = Param->Len;
return Param->Value; return Param->Value;
} }
@@ -220,11 +423,18 @@ const char * CConfigCore::GetParamStr( const char * Name, int &Len, const char *
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
const long CConfigCore::GetParamInt( const char * Name, long Default ) const long CConfigCore::GetParamInt( const char * ParentPath, const char * Name, long Default )
{ {
TConfigParam * Parent;
TConfigParam * Param; TConfigParam * Param;
if ((Param = GetParam( Name )) && (Param->Type == jtInt)) { // Validate
if (!Name || !*Name || !GetParent( ParentPath, &Parent )) {
return Default;
}
// Return value
if ((Param = GetParam( Parent, Name )) && (Param->Type == jtInt)) {
return strtol( Param->Value, NULL, 10 ); return strtol( Param->Value, NULL, 10 );
} }
else { else {
@@ -233,11 +443,18 @@ const long CConfigCore::GetParamInt( const char * Name, long Default )
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
const double CConfigCore::GetParamFloat( const char * Name, double Default ) const double CConfigCore::GetParamFloat( const char * ParentPath, const char * Name, double Default )
{ {
TConfigParam * Parent;
TConfigParam * Param; TConfigParam * Param;
if ((Param = GetParam( Name )) && (Param->Type == jtFloat)) { // Validate
if (!Name || !*Name || !GetParent( ParentPath, &Parent )) {
return Default;
}
// Return value
if ((Param = GetParam( Parent, Name )) && (Param->Type == jtFloat)) {
return strtod( Param->Value, NULL ); return strtod( Param->Value, NULL );
} }
else { else {
@@ -246,11 +463,18 @@ const double CConfigCore::GetParamFloat( const char * Name, double Default )
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
const bool CConfigCore::GetParamBool( const char * Name, bool Default ) const bool CConfigCore::GetParamBool( const char * ParentPath, const char * Name, bool Default )
{ {
TConfigParam * Parent;
TConfigParam * Param; TConfigParam * Param;
if ((Param = GetParam( Name )) && (Param->Type == jtBool)) { // Validate
if (!Name || !*Name || !GetParent( ParentPath, &Parent )) {
return Default;
}
// Return value
if ((Param = GetParam( Parent, Name )) && (Param->Type == jtBool)) {
return ((!strcasecmp( Param->Value, "true" ))? true : false ); return ((!strcasecmp( Param->Value, "true" ))? true : false );
} }
else { else {
@@ -264,12 +488,18 @@ bool CConfigCore::LoadFile( const char * FilePath, int pBufLen )
TConfigParam * Object = NULL; TConfigParam * Object = NULL;
// Validate // Validate
if (!FilePath || !FilePath[0]) if (!FilePath || !FilePath[0]) {
Error = true;
sprintf( ErrorText, "No File path specified" );
return false; return false;
}
// Open file // Open file
if (!(InputFile = fopen( FilePath, "r" ))) if (!(InputFile = fopen( FilePath, "r" ))) {
Error = true;
sprintf( ErrorText, "Could not open file" );
return false; return false;
}
// Load Buffer // Load Buffer
CreateBuffer( pBufLen ); CreateBuffer( pBufLen );
@@ -286,7 +516,8 @@ bool CConfigCore::LoadFile( const char * FilePath, int pBufLen )
// Create Root object // Create Root object
DeleteAll(); DeleteAll();
Object = FindCreateParam( "root" ); Object = RootObject;
Level = 0;
// Parse Root Object // Parse Root Object
SkipWhiteSpace(); SkipWhiteSpace();
@@ -309,6 +540,7 @@ bool CConfigCore::LoadFile( const char * FilePath, int pBufLen )
} }
// Success // Success
fclose( InputFile );
FreeBuffer(); FreeBuffer();
return true; return true;
} }
@@ -326,6 +558,7 @@ bool CConfigCore::CreateBuffer( int pBufLen )
// Reset markers // Reset markers
BufEnd = Buffer; BufEnd = Buffer;
*BufEnd = 0;
BufPos = Buffer; BufPos = Buffer;
LineMark = Buffer; LineMark = Buffer;
@@ -338,13 +571,14 @@ bool CConfigCore::FillBuffer()
int BufCount = 0; int BufCount = 0;
// Read from file // Read from file
BufCount = fread( Buffer, 1, 500, InputFile ); BufCount = fread( Buffer, 1, BufLen, InputFile );
if (BufCount < 1) { if (BufCount < 1) {
return false; return false;
} }
// Update Markers // Update Markers
BufEnd += BufCount; BufEnd = Buffer + BufCount;
*BufEnd = 0;
BufPos = Buffer; BufPos = Buffer;
LineMark = Buffer; LineMark = Buffer;
@@ -358,6 +592,11 @@ void CConfigCore::FreeBuffer()
if (Buffer) { if (Buffer) {
free( Buffer ); free( Buffer );
Buffer = NULL; Buffer = NULL;
// Update Markers
BufEnd = NULL;
BufPos = NULL;
LineMark = NULL;
} }
BufLen = 0; BufLen = 0;
} }
@@ -365,6 +604,7 @@ void CConfigCore::FreeBuffer()
bool CConfigCore::ParseObject( TConfigParam * Object ) bool CConfigCore::ParseObject( TConfigParam * Object )
{ {
TConfigParam ** ParamPtr = NULL;
TConfigParam * Param = NULL; TConfigParam * Param = NULL;
char * ParamName = NULL; char * ParamName = NULL;
int Len = 0; int Len = 0;
@@ -378,13 +618,16 @@ bool CConfigCore::ParseObject( TConfigParam * Object )
} }
BufPos++; BufPos++;
// Set Type
Level++;
SetParam( Object, jtObject, NULL, -1 );
while (true) while (true)
{ {
// Look for Param Name // Look for Param Name
SkipWhiteSpace(); SkipWhiteSpace();
if (*BufPos == 0) { if (*BufPos == '}') {
Error = true; break;
sprintf( ErrorText, "Expect quoted key name on line %d:%ld", LineNo, BufPos-LineMark );
} }
else if (!ParseString( &ParamName, &Len )) { else if (!ParseString( &ParamName, &Len )) {
if (!Error) { if (!Error) {
@@ -399,8 +642,17 @@ bool CConfigCore::ParseObject( TConfigParam * Object )
return false; return false;
} }
// Create parameter // Check if Param exists
Param = FindCreateParam( ParamName ); ParamPtr = &(Object->FirstObject);
while (*ParamPtr && strcasecmp( (*ParamPtr)->Name, ParamName )) {
ParamPtr = &((*ParamPtr)->Next);
}
// If not exist, add to end of list
if (!*ParamPtr) {
*ParamPtr = CreateParam( NULL );
(*ParamPtr)->Name = ParamName;
}
Param = *ParamPtr;
// Check for delimiter // Check for delimiter
SkipWhiteSpace(); SkipWhiteSpace();
@@ -421,6 +673,9 @@ bool CConfigCore::ParseObject( TConfigParam * Object )
return false; return false;
} }
// One item added
Object->Len++;
// Check if more parameters to follow // Check if more parameters to follow
SkipWhiteSpace(); SkipWhiteSpace();
if (*BufPos != ',') { if (*BufPos != ',') {
@@ -437,6 +692,7 @@ bool CConfigCore::ParseObject( TConfigParam * Object )
return false; return false;
} }
BufPos++; BufPos++;
Level--;
// success // success
return true; return true;
@@ -584,7 +840,7 @@ bool CConfigCore::ParsePrimitive( char ** Value, int * pLen, EJSONtype * pType )
*pLen = Len; *pLen = Len;
*Value = (char*)malloc( Len+1 ); *Value = (char*)malloc( Len+1 );
memcpy( *Value, Mark, Len ); memcpy( *Value, Mark, Len );
*Value[Len] = 0; (*Value)[Len] = 0;
} }
else { else {
// Try conversion to float // Try conversion to float
@@ -596,7 +852,7 @@ bool CConfigCore::ParsePrimitive( char ** Value, int * pLen, EJSONtype * pType )
*pLen = Len; *pLen = Len;
*Value = (char*)malloc( Len+1 ); *Value = (char*)malloc( Len+1 );
memcpy( *Value, Mark, Len ); memcpy( *Value, Mark, Len );
*Value[Len] = 0; (*Value)[Len] = 0;
} }
else { else {
Error = true; Error = true;
@@ -611,13 +867,8 @@ bool CConfigCore::ParsePrimitive( char ** Value, int * pLen, EJSONtype * pType )
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool CConfigCore::SaveFile( const char * FilePath, const int ValueTab ) bool CConfigCore::SaveFile( const char * FilePath, const int Indent )
{ {
FILE * OutputFile = NULL;
TConfigParam * Param;
int SpacerLen;
char Spacer[50];
// Validate // Validate
if (!FilePath || !FilePath[0]) if (!FilePath || !FilePath[0])
return false; return false;
@@ -626,19 +877,34 @@ bool CConfigCore::SaveFile( const char * FilePath, const int ValueTab )
if (!(OutputFile = fopen( FilePath, "w" ))) if (!(OutputFile = fopen( FilePath, "w" )))
return false; return false;
// Save Root object
Level = 0;
SaveObject( RootObject, Indent );
// Close file
fclose( OutputFile );
return false;
}
//---------------------------------------------------------------------------
bool CConfigCore::SaveObject( TConfigParam * Object, const int Indent )
{
TConfigParam * Param;
// Opening brace
fprintf( OutputFile, "{\n" );
// Extend spacer
Level++;
memset( &Spacer[SpacerLen], ' ', 2 );
SpacerLen += 2;
Spacer[SpacerLen] = 0;
// Save parameters // Save parameters
for (Param = FirstParam; Param != NULL; (Param = Param->Next)) for (Param = Object->FirstObject; Param != NULL; (Param = Param->Next))
{ {
// Write parameter name // Write parameter name
fprintf( OutputFile, "\"%s\":", Param->Name ); fprintf( OutputFile, "%s\"%s\": ", Spacer, Param->Name );
// Create fixed offset for values
SpacerLen = ValueTab - (strlen(Param->Name) + 3);
if (SpacerLen > 0) {
memset( Spacer, ' ', SpacerLen );
Spacer[SpacerLen] = 0;
fwrite( Spacer, 1, SpacerLen, OutputFile );
}
switch (Param->Type) switch (Param->Type)
{ {
@@ -661,13 +927,23 @@ bool CConfigCore::SaveFile( const char * FilePath, const int ValueTab )
break; break;
case jtObject : case jtObject :
fprintf( OutputFile, "{},\n" ); SaveObject( Param, Indent );
break; break;
} }
} }
// Close file // Shorten spacer
fclose( OutputFile ); SpacerLen -= 2;
Spacer[SpacerLen] = 0;
Level--;
// Closing brace
if (Level == 0) {
fprintf( OutputFile, "%s}\n", Spacer );
}
else {
fprintf( OutputFile, "%s},\n", Spacer );
}
return false; return false;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@@ -27,9 +27,11 @@ typedef enum { jtNull = 0, jtBool = 1, jtInt = 2, jtFloat = 3, jtString = 4, jtA
typedef struct SConfigParam TConfigParam; typedef struct SConfigParam TConfigParam;
// One Config Parameters // One Config Parameters
struct SConfigParam { struct SConfigParam
{
char * Name; char * Name;
EJSONtype Type; EJSONtype Type;
TConfigParam * FirstObject;
char * Value; char * Value;
int Len; int Len;
@@ -41,23 +43,41 @@ struct SConfigParam {
class CConfigCore class CConfigCore
{ {
private: private:
TConfigParam * FirstParam; TConfigParam * RootObject;
// File operation // File operation
FILE * InputFile; FILE * InputFile;
FILE * OutputFile;
char * Buffer; char * Buffer;
int BufLen; int BufLen;
// Parsing operation // Parsing operation
char * BufEnd; char * BufEnd;
char * BufPos; char * BufPos;
char * LineMark; char * LineMark;
int LineNo; int LineNo;
int Level;
// Printing Operation
char Spacer[100];
int SpacerLen;
// Error
bool Error; bool Error;
char ErrorText[100]; char ErrorText[100];
// Manage Parameters
TConfigParam * CreateParam( const char * Name );
bool DestroyParam( TConfigParam ** Param );
// Find Param
bool GetParent( const char * ParentPath, TConfigParam **Parent );
TConfigParam * GetParam( TConfigParam * Parent, const char * Name );
TConfigParam ** GetParamPtr( TConfigParam * Parent, const char * Name );
// Set Param value
bool SetParam( TConfigParam * Param, EJSONtype Type, const char * Value, const int Len );
// File Buffer operation // File Buffer operation
bool CreateBuffer( int pBufLen ); bool CreateBuffer( int pBufLen );
bool FillBuffer(); bool FillBuffer();
@@ -77,54 +97,28 @@ private:
bool ParseString( char ** Value, int * pLen = NULL, EJSONtype * pType = NULL ); bool ParseString( char ** Value, int * pLen = NULL, EJSONtype * pType = NULL );
bool ParsePrimitive( char ** Value, int * pLen = NULL, EJSONtype * pType = NULL ); bool ParsePrimitive( char ** Value, int * pLen = NULL, EJSONtype * pType = NULL );
// Find Param bool SaveObject( TConfigParam * Object, const int Indent );
inline TConfigParam * GetParam( const char * Name ) {
TConfigParam * Param = FirstParam;
while (Param && strcasecmp( Param->Name, Name ))
Param = Param->Next;
return Param;
}
inline TConfigParam ** GetParamPtr( const char * Name ) {
TConfigParam ** Param = &FirstParam;
while (*Param && strcasecmp( (*Param)->Name, Name ))
Param = &((*Param)->Next);
return Param;
}
// Create Param
inline TConfigParam * FindCreateParam( const char * Name ) {
TConfigParam ** Param = NULL;
if (!*(Param = GetParamPtr( Name ))) {
*Param = (TConfigParam *)calloc( 1, sizeof(TConfigParam) );
(*Param)->Name = (char *)calloc( 1, strlen( Name )+1 );
strcpy( (*Param)->Name, Name );
}
return (*Param);
}
// Manage Parameters
bool SetParam( TConfigParam * Param, EJSONtype Type, const char * Value, const int Len );
bool DestroyParam( TConfigParam ** Param );
public: public:
CConfigCore(); CConfigCore();
~CConfigCore(); ~CConfigCore();
bool SetParamStr( const char * Name, const char * Value = NULL, const int Len = -1 ); // Use Len param if Value contains NULL values bool SetParamObject( const char * ParentPath, const char * Name );
bool SetParamInt( const char * Name, const long Value ); bool SetParamStr( const char * ParentPath, const char * Name, const char * Value = NULL, const int Len = -1 ); // Use Len param if Value contains NULL values
bool SetParamFloat( const char * Name, const double Value ); bool SetParamInt( const char * ParentPath, const char * Name, const long Value );
bool SetParamBool( const char * Name, const bool Value ); bool SetParamFloat( const char * ParentPath, const char * Name, const double Value );
bool SetParamNull( const char * Name ); bool SetParamBool( const char * ParentPath, const char * Name, const bool Value );
bool SetParamNull( const char * ParentPath, const char * Name );
const EJSONtype GetParamType( const char * Name ); const EJSONtype GetParamType( const char * ParentPath, const char * Name );
const char * GetParamStr( const char * Name, const char * Default = NULL ); const char * GetParamStr( const char * ParentPath, const char * Name, const char * Default = NULL );
const char * GetParamStr( const char * Name, int &Len, const char * Default = NULL ); const char * GetParamStr( const char * ParentPath, const char * Name, int &Len, const char * Default = NULL );
const long GetParamInt( const char * Name, long Default = 0 ); const long GetParamInt( const char * ParentPath, const char * Name, long Default = 0 );
const double GetParamFloat( const char * Name, double Default = 0.0 ); const double GetParamFloat( const char * ParentPath, const char * Name, double Default = 0.0 );
const bool GetParamBool( const char * Name, bool Default = false ); const bool GetParamBool( const char * ParentPath, const char * Name, bool Default = false );
bool DeleteParam( const char * Name ); bool DeleteParam( const char * ParentPath, const char * Name );
bool DeleteAll(); bool DeleteAll();
bool LoadFile( const char * FilePath, int pBufLen = 500 ); bool LoadFile( const char * FilePath, int pBufLen = 500 );