Files
redAcore/ConfigCore.h
Charl Wentzel bb07ef63bf 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
2017-03-18 23:03:51 +02:00

131 lines
4.0 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;
TConfigParam * FirstObject;
char * Value;
int Len;
TConfigParam * Next;
};
//---------------------------------------------------------------------------
class CConfigCore
{
private:
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();
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 );
bool SaveObject( TConfigParam * Object, const int Indent );
public:
CConfigCore();
~CConfigCore();
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 * ParentPath, const char * Name );
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 * ParentPath, 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_ */