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:
440
ConfigCore.cpp
440
ConfigCore.cpp
@@ -19,18 +19,27 @@
|
||||
CConfigCore::CConfigCore()
|
||||
{
|
||||
// Parameter tree
|
||||
FirstParam = NULL;
|
||||
RootObject = CreateParam( NULL );
|
||||
RootObject->Type = jtObject;
|
||||
|
||||
// File Operation
|
||||
InputFile = NULL;
|
||||
OutputFile = NULL;
|
||||
BufLen = 500;
|
||||
Buffer = NULL;
|
||||
BufEnd = NULL;
|
||||
Level = 0;
|
||||
|
||||
// Parsing operation
|
||||
BufPos = NULL;
|
||||
LineMark = NULL;
|
||||
LineNo = 0;
|
||||
|
||||
// Printing operation
|
||||
Spacer[0] = 0;
|
||||
SpacerLen = 0;
|
||||
|
||||
// Error reporting
|
||||
Error = false;
|
||||
ErrorText[0] = 0;
|
||||
}
|
||||
@@ -39,8 +48,22 @@ CConfigCore::CConfigCore()
|
||||
CConfigCore::~CConfigCore()
|
||||
{
|
||||
// Destroy Params
|
||||
while (FirstParam)
|
||||
DestroyParam( &FirstParam );
|
||||
DeleteAll();
|
||||
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 );
|
||||
if ((*Param)->Value)
|
||||
free( (*Param)->Value );
|
||||
while ((*Param)->FirstObject) {
|
||||
DestroyParam( &((*Param)->FirstObject) );
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// Check if exists
|
||||
if (!(Param = GetParamPtr( Name )))
|
||||
return false;
|
||||
// Validate
|
||||
if (!Parent || !Name || !*Name) {
|
||||
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 );
|
||||
Parent->Len--;
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool CConfigCore::DeleteAll()
|
||||
{
|
||||
while (FirstParam)
|
||||
DestroyParam( &FirstParam );
|
||||
while (RootObject->FirstObject) {
|
||||
DestroyParam( &(RootObject->FirstObject) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
@@ -103,87 +216,163 @@ bool CConfigCore::SetParam( TConfigParam * Param, EJSONtype Type, const char *
|
||||
Param->Type = Type;
|
||||
|
||||
// Set new value
|
||||
if (Type == jtNull) {
|
||||
switch (Type) {
|
||||
case jtNull:
|
||||
case jtObject:
|
||||
case jtArray:
|
||||
Param->Len = 0;
|
||||
Param->Value = NULL;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
|
||||
case jtString:
|
||||
case jtFloat:
|
||||
case jtInt:
|
||||
case jtBool:
|
||||
Param->Len = (Len == -1)? strlen(Value) : Len;
|
||||
Param->Value = (char *)malloc( sizeof(Param->Len+1) );
|
||||
if (Value) {
|
||||
memcpy( Param->Value, Value, Param->Len+1 );
|
||||
} else {
|
||||
memset( Param->Value, 0, Param->Len+1 );
|
||||
}
|
||||
Param->Value[Param->Len] = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
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
|
||||
Param = FindCreateParam( Name );
|
||||
SetParam( Param, jtString, Value, Len );
|
||||
|
||||
if (!(Param = GetParamPtr( Parent, Name ))) {
|
||||
*Param = CreateParam( Name );
|
||||
}
|
||||
SetParam( *Param, jtObject, NULL, -1 );
|
||||
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];
|
||||
|
||||
// Validate
|
||||
if (!Name || !*Name || !GetParent( ParentPath, &Parent )) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create new param if it doesn't exist
|
||||
Param = FindCreateParam( Name );
|
||||
if (!(Param = GetParamPtr( Parent, Name ))) {
|
||||
*Param = CreateParam( Name );
|
||||
}
|
||||
sprintf( ValueStr, "%ld", Value );
|
||||
SetParam( Param, jtInt, ValueStr, -1 );
|
||||
|
||||
return false;
|
||||
SetParam( *Param, jtInt, ValueStr, -1 );
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
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];
|
||||
|
||||
// Validate
|
||||
if (!Name || !*Name || !GetParent( ParentPath, &Parent )) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create new param if it doesn't exist
|
||||
Param = FindCreateParam( Name );
|
||||
if (!*(Param = GetParamPtr( Parent, Name ))) {
|
||||
*Param = CreateParam( Name );
|
||||
}
|
||||
sprintf( ValueStr, "%lf", Value );
|
||||
SetParam( Param, jtFloat, ValueStr, -1 );
|
||||
return false;
|
||||
SetParam( *Param, jtFloat, ValueStr, -1 );
|
||||
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
|
||||
Param = FindCreateParam( Name );
|
||||
SetParam( Param, jtBool, ((Value == 0)? "false" : "true"), -1 );
|
||||
return false;
|
||||
if (!*(Param = GetParamPtr( Parent, Name ))) {
|
||||
*Param = CreateParam( Name );
|
||||
}
|
||||
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
|
||||
Param = FindCreateParam( Name );
|
||||
SetParam( Param, jtNull, NULL, -1 );
|
||||
return false;
|
||||
if (!*(Param = GetParamPtr( Parent, Name ))) {
|
||||
*Param = CreateParam( Name );
|
||||
}
|
||||
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;
|
||||
|
||||
if ((Param = GetParam( Name ))) {
|
||||
// Validate
|
||||
if (!Name || !*Name || !GetParent( ParentPath, &Parent )) {
|
||||
return jtNull;
|
||||
}
|
||||
|
||||
// Return type
|
||||
if ((Param = GetParam( Parent, Name ))) {
|
||||
return Param->Type;
|
||||
}
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
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;
|
||||
|
||||
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;
|
||||
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;
|
||||
|
||||
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 );
|
||||
}
|
||||
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;
|
||||
|
||||
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 );
|
||||
}
|
||||
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;
|
||||
|
||||
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 );
|
||||
}
|
||||
else {
|
||||
@@ -264,12 +488,18 @@ bool CConfigCore::LoadFile( const char * FilePath, int pBufLen )
|
||||
TConfigParam * Object = NULL;
|
||||
|
||||
// Validate
|
||||
if (!FilePath || !FilePath[0])
|
||||
if (!FilePath || !FilePath[0]) {
|
||||
Error = true;
|
||||
sprintf( ErrorText, "No File path specified" );
|
||||
return false;
|
||||
}
|
||||
|
||||
// Open file
|
||||
if (!(InputFile = fopen( FilePath, "r" )))
|
||||
if (!(InputFile = fopen( FilePath, "r" ))) {
|
||||
Error = true;
|
||||
sprintf( ErrorText, "Could not open file" );
|
||||
return false;
|
||||
}
|
||||
|
||||
// Load Buffer
|
||||
CreateBuffer( pBufLen );
|
||||
@@ -286,7 +516,8 @@ bool CConfigCore::LoadFile( const char * FilePath, int pBufLen )
|
||||
|
||||
// Create Root object
|
||||
DeleteAll();
|
||||
Object = FindCreateParam( "root" );
|
||||
Object = RootObject;
|
||||
Level = 0;
|
||||
|
||||
// Parse Root Object
|
||||
SkipWhiteSpace();
|
||||
@@ -309,6 +540,7 @@ bool CConfigCore::LoadFile( const char * FilePath, int pBufLen )
|
||||
}
|
||||
|
||||
// Success
|
||||
fclose( InputFile );
|
||||
FreeBuffer();
|
||||
return true;
|
||||
}
|
||||
@@ -326,6 +558,7 @@ bool CConfigCore::CreateBuffer( int pBufLen )
|
||||
|
||||
// Reset markers
|
||||
BufEnd = Buffer;
|
||||
*BufEnd = 0;
|
||||
BufPos = Buffer;
|
||||
LineMark = Buffer;
|
||||
|
||||
@@ -338,13 +571,14 @@ bool CConfigCore::FillBuffer()
|
||||
int BufCount = 0;
|
||||
|
||||
// Read from file
|
||||
BufCount = fread( Buffer, 1, 500, InputFile );
|
||||
BufCount = fread( Buffer, 1, BufLen, InputFile );
|
||||
if (BufCount < 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Update Markers
|
||||
BufEnd += BufCount;
|
||||
BufEnd = Buffer + BufCount;
|
||||
*BufEnd = 0;
|
||||
BufPos = Buffer;
|
||||
LineMark = Buffer;
|
||||
|
||||
@@ -358,6 +592,11 @@ void CConfigCore::FreeBuffer()
|
||||
if (Buffer) {
|
||||
free( Buffer );
|
||||
Buffer = NULL;
|
||||
|
||||
// Update Markers
|
||||
BufEnd = NULL;
|
||||
BufPos = NULL;
|
||||
LineMark = NULL;
|
||||
}
|
||||
BufLen = 0;
|
||||
}
|
||||
@@ -365,6 +604,7 @@ void CConfigCore::FreeBuffer()
|
||||
|
||||
bool CConfigCore::ParseObject( TConfigParam * Object )
|
||||
{
|
||||
TConfigParam ** ParamPtr = NULL;
|
||||
TConfigParam * Param = NULL;
|
||||
char * ParamName = NULL;
|
||||
int Len = 0;
|
||||
@@ -378,13 +618,16 @@ bool CConfigCore::ParseObject( TConfigParam * Object )
|
||||
}
|
||||
BufPos++;
|
||||
|
||||
// Set Type
|
||||
Level++;
|
||||
SetParam( Object, jtObject, NULL, -1 );
|
||||
|
||||
while (true)
|
||||
{
|
||||
// Look for Param Name
|
||||
SkipWhiteSpace();
|
||||
if (*BufPos == 0) {
|
||||
Error = true;
|
||||
sprintf( ErrorText, "Expect quoted key name on line %d:%ld", LineNo, BufPos-LineMark );
|
||||
if (*BufPos == '}') {
|
||||
break;
|
||||
}
|
||||
else if (!ParseString( &ParamName, &Len )) {
|
||||
if (!Error) {
|
||||
@@ -399,8 +642,17 @@ bool CConfigCore::ParseObject( TConfigParam * Object )
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create parameter
|
||||
Param = FindCreateParam( ParamName );
|
||||
// Check if Param exists
|
||||
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
|
||||
SkipWhiteSpace();
|
||||
@@ -421,6 +673,9 @@ bool CConfigCore::ParseObject( TConfigParam * Object )
|
||||
return false;
|
||||
}
|
||||
|
||||
// One item added
|
||||
Object->Len++;
|
||||
|
||||
// Check if more parameters to follow
|
||||
SkipWhiteSpace();
|
||||
if (*BufPos != ',') {
|
||||
@@ -437,6 +692,7 @@ bool CConfigCore::ParseObject( TConfigParam * Object )
|
||||
return false;
|
||||
}
|
||||
BufPos++;
|
||||
Level--;
|
||||
|
||||
// success
|
||||
return true;
|
||||
@@ -584,7 +840,7 @@ bool CConfigCore::ParsePrimitive( char ** Value, int * pLen, EJSONtype * pType )
|
||||
*pLen = Len;
|
||||
*Value = (char*)malloc( Len+1 );
|
||||
memcpy( *Value, Mark, Len );
|
||||
*Value[Len] = 0;
|
||||
(*Value)[Len] = 0;
|
||||
}
|
||||
else {
|
||||
// Try conversion to float
|
||||
@@ -596,7 +852,7 @@ bool CConfigCore::ParsePrimitive( char ** Value, int * pLen, EJSONtype * pType )
|
||||
*pLen = Len;
|
||||
*Value = (char*)malloc( Len+1 );
|
||||
memcpy( *Value, Mark, Len );
|
||||
*Value[Len] = 0;
|
||||
(*Value)[Len] = 0;
|
||||
}
|
||||
else {
|
||||
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
|
||||
if (!FilePath || !FilePath[0])
|
||||
return false;
|
||||
@@ -626,19 +877,34 @@ bool CConfigCore::SaveFile( const char * FilePath, const int ValueTab )
|
||||
if (!(OutputFile = fopen( FilePath, "w" )))
|
||||
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
|
||||
for (Param = FirstParam; Param != NULL; (Param = Param->Next))
|
||||
for (Param = Object->FirstObject; Param != NULL; (Param = Param->Next))
|
||||
{
|
||||
// Write parameter name
|
||||
fprintf( OutputFile, "\"%s\":", 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 );
|
||||
}
|
||||
fprintf( OutputFile, "%s\"%s\": ", Spacer, Param->Name );
|
||||
|
||||
switch (Param->Type)
|
||||
{
|
||||
@@ -661,13 +927,23 @@ bool CConfigCore::SaveFile( const char * FilePath, const int ValueTab )
|
||||
break;
|
||||
|
||||
case jtObject :
|
||||
fprintf( OutputFile, "{},\n" );
|
||||
SaveObject( Param, Indent );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Close file
|
||||
fclose( OutputFile );
|
||||
// Shorten spacer
|
||||
SpacerLen -= 2;
|
||||
Spacer[SpacerLen] = 0;
|
||||
Level--;
|
||||
|
||||
// Closing brace
|
||||
if (Level == 0) {
|
||||
fprintf( OutputFile, "%s}\n", Spacer );
|
||||
}
|
||||
else {
|
||||
fprintf( OutputFile, "%s},\n", Spacer );
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
80
ConfigCore.h
80
ConfigCore.h
@@ -27,9 +27,11 @@ typedef enum { jtNull = 0, jtBool = 1, jtInt = 2, jtFloat = 3, jtString = 4, jtA
|
||||
typedef struct SConfigParam TConfigParam;
|
||||
|
||||
// One Config Parameters
|
||||
struct SConfigParam {
|
||||
struct SConfigParam
|
||||
{
|
||||
char * Name;
|
||||
EJSONtype Type;
|
||||
TConfigParam * FirstObject;
|
||||
char * Value;
|
||||
int Len;
|
||||
|
||||
@@ -41,23 +43,41 @@ struct SConfigParam {
|
||||
class CConfigCore
|
||||
{
|
||||
private:
|
||||
TConfigParam * FirstParam;
|
||||
TConfigParam * RootObject;
|
||||
|
||||
// File operation
|
||||
FILE * InputFile;
|
||||
FILE * OutputFile;
|
||||
char * Buffer;
|
||||
int BufLen;
|
||||
|
||||
// Parsing operation
|
||||
char * BufEnd;
|
||||
|
||||
char * BufPos;
|
||||
char * LineMark;
|
||||
int LineNo;
|
||||
int Level;
|
||||
|
||||
// Printing Operation
|
||||
char Spacer[100];
|
||||
int SpacerLen;
|
||||
|
||||
// Error
|
||||
bool Error;
|
||||
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
|
||||
bool CreateBuffer( int pBufLen );
|
||||
bool FillBuffer();
|
||||
@@ -77,54 +97,28 @@ private:
|
||||
bool ParseString( char ** Value, int * pLen = NULL, EJSONtype * pType = NULL );
|
||||
bool ParsePrimitive( char ** Value, int * pLen = NULL, EJSONtype * pType = NULL );
|
||||
|
||||
// Find Param
|
||||
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 );
|
||||
bool SaveObject( TConfigParam * Object, const int Indent );
|
||||
|
||||
public:
|
||||
CConfigCore();
|
||||
~CConfigCore();
|
||||
|
||||
bool SetParamStr( const char * Name, const char * Value = NULL, const int Len = -1 ); // Use Len param if Value contains NULL values
|
||||
bool SetParamInt( const char * Name, const long Value );
|
||||
bool SetParamFloat( const char * Name, const double Value );
|
||||
bool SetParamBool( const char * Name, const bool Value );
|
||||
bool SetParamNull( const char * Name );
|
||||
bool SetParamObject( const char * ParentPath, const char * Name );
|
||||
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 SetParamInt( const char * ParentPath, const char * Name, const long Value );
|
||||
bool SetParamFloat( const char * ParentPath, const char * Name, const double Value );
|
||||
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 * Name, int &Len, const char * Default = NULL );
|
||||
const long GetParamInt( const char * Name, long Default = 0 );
|
||||
const double GetParamFloat( const char * Name, double Default = 0.0 );
|
||||
const bool GetParamBool( const char * Name, bool Default = false );
|
||||
const char * GetParamStr( const char * ParentPath, const char * Name, const char * Default = NULL );
|
||||
const char * GetParamStr( const char * ParentPath, const char * Name, int &Len, const char * Default = NULL );
|
||||
const long GetParamInt( const char * ParentPath, const char * Name, long Default = 0 );
|
||||
const double GetParamFloat( const char * ParentPath, const char * Name, double Default = 0.0 );
|
||||
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 LoadFile( const char * FilePath, int pBufLen = 500 );
|
||||
|
||||
Reference in New Issue
Block a user