- Implemented JSON type NULL - Implement dynamic buffer - Split parsing int separate re-usable methods - Implement basic parsing of an object - Add GetError() method - Add SkipWhiteSpace as inline method - Make parsing variable private attributes - Improve checking when getting parameters
137 lines
3.9 KiB
C++
137 lines
3.9 KiB
C++
/*
|
|
* ConfigCore.h
|
|
*
|
|
* Created on: 5 Mar 2017
|
|
* Author: wentzelc
|
|
*/
|
|
|
|
#ifndef REDACORE_CONFIGCORE_H_
|
|
#define REDACORE_CONFIGCORE_H_
|
|
|
|
// redA Libraries
|
|
/* none */
|
|
|
|
// Standard C/C++ Libraries
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include <stdio.h>
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
typedef enum { jtNull = 0, jtBool = 1, jtInt = 2, jtFloat = 3, jtString = 4, jtArray = 5, jtObject = 6 } EJSONtype;
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Structure prototypes
|
|
typedef struct SConfigParam TConfigParam;
|
|
|
|
// One Config Parameters
|
|
struct SConfigParam {
|
|
char * Name;
|
|
EJSONtype Type;
|
|
char * Value;
|
|
int Len;
|
|
|
|
TConfigParam * Next;
|
|
};
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
class CConfigCore
|
|
{
|
|
private:
|
|
TConfigParam * FirstParam;
|
|
|
|
// File operation
|
|
FILE * InputFile;
|
|
char * Buffer;
|
|
int BufLen;
|
|
|
|
// Parsing operation
|
|
char * BufEnd;
|
|
|
|
char * BufPos;
|
|
char * LineMark;
|
|
int LineNo;
|
|
|
|
bool Error;
|
|
char ErrorText[100];
|
|
|
|
// File Buffer operation
|
|
bool CreateBuffer( int pBufLen );
|
|
bool FillBuffer();
|
|
void FreeBuffer();
|
|
|
|
// Parsing functions
|
|
inline void SkipWhiteSpace() {
|
|
while (isspace(*BufPos)) {
|
|
if (*BufPos == '\n') {
|
|
LineMark = BufPos;
|
|
LineNo++;
|
|
}
|
|
BufPos++;
|
|
}
|
|
}
|
|
bool ParseObject( TConfigParam * Object );
|
|
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 );
|
|
|
|
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 );
|
|
|
|
const EJSONtype GetParamType( 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 );
|
|
|
|
bool DeleteParam( const char * Name );
|
|
bool DeleteAll();
|
|
|
|
bool LoadFile( const char * FilePath, int pBufLen = 500 );
|
|
bool SaveFile( const char * FilePath, const int ValueTab = 20 );
|
|
|
|
const char * GetError() { return ((Error)? ErrorText : "Success"); };
|
|
};
|
|
|
|
#endif /* REDACORE_CONFIGCORE_H_ */
|