Important Update:

- Added new Class: ConfigCore (Working but incomplete)
  - Provides basic parameter listing (bool, int, float, string)
  - Read/writes parameters from/to file
  - Support JSON format for key-value pair primitive and strings values
  - Support for objects and arrays to follow
This commit is contained in:
Charl Wentzel
2017-03-08 04:05:26 +02:00
parent f57b340be6
commit 9b180c77f5
4 changed files with 574 additions and 2 deletions

97
ConfigCore.h Normal file
View File

@@ -0,0 +1,97 @@
/*
* 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>
//---------------------------------------------------------------------------
typedef enum { jtNone = 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;
// 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 );
bool SetParamInt( const char * Name, const long Value );
bool SetParamFloat( const char * Name, const double Value );
bool SetParamBool( const char * Name, const bool Value );
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 );
bool SaveFile( const char * FilePath, const int ValueTab = 20 );
};
#endif /* REDACORE_CONFIGCORE_H_ */