Important update:

- DataTree->GetStr return Member->Value or NULL if not found
- Rename DeviceCore EDataType to EMBDataType, avoid conflict with JSONpaser
- Add DataTree to FunctionCore with LoadConfig() method
- Change ProcessName to char * (from char[])
- JSONparse:
  - Rename RootPath to BasePath on all methods to reduce confusion
  - Allow FilePath to be NULL, add '/' if not present
  - Fixed Parsing sequence in ParseObject & ParseArray
This commit is contained in:
Charl Wentzel
2017-04-10 21:33:14 +02:00
parent 9b42d516a1
commit ee29625b9a
8 changed files with 89 additions and 66 deletions

View File

@@ -534,10 +534,10 @@ const char * CDataTree::GetStr( TDataMember * BaseMember, const char * Path, con
}
else if (Member && Create && (Member->Type == jtNull)) {
SetValue( Member, jtString, Default );
return Default;
return Member->Value;
}
else {
return Default;
return NULL;
}
}
//---------------------------------------------------------------------------
@@ -554,11 +554,11 @@ const char * CDataTree::GetStr( TDataMember * BaseMember, const char * Path, int
else if (Member && Create && (Member->Type == jtNull)) {
SetValue( Member, jtString, Default );
Len = Member->Len;
return Default;
return Member->Value;
}
else {
Len = strlen( Default );
return Default;
Len = 0;
return NULL;
}
}
//---------------------------------------------------------------------------

View File

@@ -217,7 +217,7 @@ bool CDeviceCore::DestroyDevice( TDevice ** Device )
}
//---------------------------------------------------------------------------
TDeviceParam * CDeviceCore::AddDeviceParam( TDevice * Device, const char * ParamName, EDataType DataType, int ParamLen )
TDeviceParam * CDeviceCore::AddDeviceParam( TDevice * Device, const char * ParamName, EMBDataType DataType, int ParamLen )
{
// Get register or end of list
TDeviceParam ** Param = &Device->FirstParam;

View File

@@ -17,7 +17,7 @@
//---------------------------------------------------------------------------
// Enumerated types
typedef enum { dtUnsigned16 = 0, dtSigned16 = 1, dtUnsigned32 = 2, dtSigned32 = 3, dtFloat32 = 4, dtString = 5 } EDataType;
typedef enum { dtUnsigned16 = 0, dtSigned16 = 1, dtUnsigned32 = 2, dtSigned32 = 3, dtFloat32 = 4, dtString = 5 } EMBDataType;
// Constants
const char DataTypeName[][20] = { "Unsigned16", "Signed16", "Unsigned32", "Signed32", "Float32", "String" };
@@ -41,7 +41,7 @@ struct SDevice {
// Data parameters of devices
struct SDeviceParam {
char * Name;
EDataType DataType;
EMBDataType DataType;
bool Scan;
TChannel * EventChannel;
@@ -182,7 +182,7 @@ public:
bool DestroyDevice( const char * DeviceName );
// Manage Params
TDeviceParam * AddDeviceParam( TDevice * Device, const char * ParamName, EDataType DataType, int ParamLen = 1 );
TDeviceParam * AddDeviceParam( TDevice * Device, const char * ParamName, EMBDataType DataType, int ParamLen = 1 );
bool DestroyDeviceParam( TDevice * Device, const char * ParamName );
// Update/Init Param values

View File

@@ -17,7 +17,7 @@
//---------------------------------------------------------------------------
extern char ProcessName[];
extern char * ProcessName;
//---------------------------------------------------------------------------
@@ -33,8 +33,12 @@ CFunctionCore::CFunctionCore( const char * FunctionName, CLogCore * pLog, EDebug
Name = NULL;
}
// Data Tree
DataTree = NULL;
BaseMember = NULL;
// Channels
FirstChannel = NULL;
FirstChannel = NULL;
// Output
Log = pLog;
@@ -92,7 +96,17 @@ CFunctionCore::~CFunctionCore()
if (Name) {
free( Name );
}
}
//---------------------------------------------------------------------------
bool CFunctionCore::LoadConfig( CDataTree * pDataTree, const char * pBasePath )
{
if (!(DataTree = pDataTree))
return false;
if (!(BaseMember = DataTree->GetMember( NULL, pBasePath, true )))
return false;
return true;
}
//---------------------------------------------------------------------------

View File

@@ -11,6 +11,7 @@
// redA Libraries
#include "LogCore.h"
#include "BufferCore.h"
#include "DataTreeCore.h"
// Standard C/C++ Libraries
#include <string.h>
@@ -52,6 +53,10 @@ protected:
// Function Definition
char * Name;
// Configuration
CDataTree * DataTree;
TDataMember * BaseMember;
// Channels
TChannel * FirstChannel;
@@ -77,6 +82,8 @@ public:
CFunctionCore( const char * FunctionName, CLogCore * pLog, EDebugLevel pDebugLevel, int pOuputDisplay );
virtual ~CFunctionCore();
virtual bool LoadConfig( CDataTree * DataTree, const char * BasePath );
// Miscellaneous
inline const char * GetName() { return Name; };

View File

@@ -54,16 +54,16 @@ CJSONparse::~CJSONparse()
}
//---------------------------------------------------------------------------
bool CJSONparse::WriteToScreen( const char * RootPath, const int Indent )
bool CJSONparse::WriteToScreen( const char * BasePath, const int Indent )
{
// Print to screen
WriteToHandle( RootPath, 1, Indent );
WriteToHandle( BasePath, 1, Indent );
return true;
}
//---------------------------------------------------------------------------
bool CJSONparse::WriteToFile( const char * RootPath, const char * Path, const char * FileName, const int Indent )
bool CJSONparse::WriteToFile( const char * BasePath, const char * Path, const char * FileName, const int Indent )
{
char FilePath[250] = "";
@@ -72,11 +72,11 @@ bool CJSONparse::WriteToFile( const char * RootPath, const char * Path, const ch
strcat( FilePath, FileName );
// Read file
return WriteToFile( RootPath, FilePath, Indent );
return WriteToFile( BasePath, FilePath, Indent );
}
//---------------------------------------------------------------------------
bool CJSONparse::WriteToFile( const char * RootPath, const char * FilePath, const int Indent )
bool CJSONparse::WriteToFile( const char * BasePath, const char * FilePath, const int Indent )
{
int Handle = -1;
@@ -98,7 +98,7 @@ bool CJSONparse::WriteToFile( const char * RootPath, const char * FilePath, cons
}
// Save to file
WriteToHandle( RootPath, Handle, Indent );
WriteToHandle( BasePath, Handle, Indent );
// Close file
close( Handle );
@@ -106,9 +106,9 @@ bool CJSONparse::WriteToFile( const char * RootPath, const char * FilePath, cons
}
//---------------------------------------------------------------------------
bool CJSONparse::WriteToHandle( const char * RootPath, const int Handle, const int Indent )
bool CJSONparse::WriteToHandle( const char * BasePath, const int Handle, const int Indent )
{
TDataMember * RootObject;
TDataMember * BaseMember;
// Validate
if (!DataTree) {
@@ -124,17 +124,17 @@ bool CJSONparse::WriteToHandle( const char * RootPath, const int Handle, const i
OutputHandle = Handle;
// Get Root object
if (!RootPath || !*RootPath) {
RootObject = DataTree->GetRootMember();
if (!BasePath || !*BasePath) {
BaseMember = DataTree->GetRootMember();
}
else if (!(RootObject = DataTree->GetMember( NULL, RootPath ))) {
else if (!(BaseMember = DataTree->GetMember( NULL, BasePath ))) {
Error = true;
sprintf( ErrorText, "Invalid root object path" );
return false;
}
// Print to file
PrintObject( RootObject, Indent );
PrintObject( BaseMember, Indent );
write( OutputHandle, "\n", 1 );
OutputHandle = -1;
@@ -142,20 +142,34 @@ bool CJSONparse::WriteToHandle( const char * RootPath, const int Handle, const i
}
//---------------------------------------------------------------------------
bool CJSONparse::ReadFromFile( const char * RootPath, const char * Path, const char * FileName )
bool CJSONparse::ReadFromFile( const char * BasePath, const char * Path, const char * FileName )
{
char FilePath[250] = "";
char FilePath[250] = "";
int PathLen = 0;
// Validate
if (!FileName) {
Error = true;
sprintf( ErrorText, "No File name specified" );
return false;
}
// Build file name
strcpy( FilePath, Path );
strcat( FilePath, FileName );
if (Path && *Path) {
strcpy( FilePath, Path );
PathLen = strlen( FilePath );
if (FilePath[PathLen] != '/') {
FilePath[PathLen++] = '/';
}
}
strcpy( &FilePath[PathLen], FileName );
// Read file
return ReadFromFile( RootPath, FilePath );
return ReadFromFile( BasePath, FilePath );
}
//---------------------------------------------------------------------------
bool CJSONparse::ReadFromFile( const char * RootPath, const char * FilePath )
bool CJSONparse::ReadFromFile( const char * BasePath, const char * FilePath )
{
int Handle = -1;
bool result = false;
@@ -183,7 +197,7 @@ bool CJSONparse::ReadFromFile( const char * RootPath, const char * FilePath )
}
// Continuously refill buffer while loading
result = ReadFromHandle( RootPath, Handle, true );
result = ReadFromHandle( BasePath, Handle, true );
// Close File
close( Handle );
@@ -191,7 +205,7 @@ bool CJSONparse::ReadFromFile( const char * RootPath, const char * FilePath )
}
//---------------------------------------------------------------------------
bool CJSONparse::ReadFromHandle( const char * RootPath, int Handle, bool pRefillBuffer )
bool CJSONparse::ReadFromHandle( const char * BasePath, int Handle, bool pRefillBuffer )
{
bool result = false;
@@ -217,7 +231,7 @@ bool CJSONparse::ReadFromHandle( const char * RootPath, int Handle, bool pRefill
// Continuously refill buffer while loading
RefillBuffer = pRefillBuffer;
result = ReadFromBuffer( RootPath );
result = ReadFromBuffer( BasePath );
RefillBuffer = false;
// Destroy buffer
@@ -228,9 +242,9 @@ bool CJSONparse::ReadFromHandle( const char * RootPath, int Handle, bool pRefill
}
//---------------------------------------------------------------------------
bool CJSONparse::ReadFromBuffer( const char * RootPath )
bool CJSONparse::ReadFromBuffer( const char * BasePath )
{
TDataMember * RootObject = NULL;
TDataMember * BaseMember = NULL;
// Validate
if (!DataTree || !Buffer) {
@@ -241,17 +255,17 @@ bool CJSONparse::ReadFromBuffer( const char * RootPath )
Error = false;
// Get/Create Root object
if (!RootPath || !*RootPath) {
RootObject = DataTree->GetRootMember();
if (!BasePath || !*BasePath) {
BaseMember = DataTree->GetRootMember();
}
else if (!(RootObject = DataTree->GetMember( NULL, RootPath, true ))) {
else if (!(BaseMember = DataTree->GetMember( NULL, BasePath, true ))) {
Error = true;
sprintf( ErrorText, "Invalid root object path" );
return false;
}
// Delete existing object contents
DataTree->Delete( RootObject, NULL );
DataTree->Delete( BaseMember, NULL );
// Position Counters
LineNo = 1;
@@ -259,7 +273,7 @@ bool CJSONparse::ReadFromBuffer( const char * RootPath )
// Parse Root Object
SkipWhiteSpace();
if (!ParseObject( RootObject )) {
if (!ParseObject( BaseMember )) {
if (!Error) {
Error = true;
CharNo += BufPos-Mark;
@@ -427,14 +441,14 @@ bool CJSONparse::ParseString( char ** Value, int &Len )
}
else {
Error = true;
CharNo += BufPos-Mark+1;
CharNo += BufPos-Mark;
sprintf( ErrorText, "Invalid escape sequence on line %d:%d", LineNo, CharNo );
return false;
}
}
else {
Error = true;
CharNo += BufPos-Mark+1;
CharNo += BufPos-Mark;
sprintf( ErrorText, "Un-escaped special character in string on line %d:%d", LineNo, CharNo );
return false;
}
@@ -538,14 +552,8 @@ bool CJSONparse::ParseObject( TDataMember * Object )
// Get Value
SkipWhiteSpace();
if (!ParseObject( Member ) &&
!Error &&
!ParseArray( Member ) &&
!Error &&
!ParseString( Member ) &&
!Error &&
!ParsePrimitive( Member ))
{
if (!ParseObject( Member ) && !Error && !ParseArray( Member ) && !Error && !ParseString( Member ) && !Error && !ParsePrimitive( Member ) ) {}
if (Error) {
// Destroy member
DataTree->Delete( Object, MemberName );
return false;
@@ -602,14 +610,8 @@ bool CJSONparse::ParseArray( TDataMember * Array )
// Get Value
SkipWhiteSpace();
if (!ParseObject( Member ) &&
!Error &&
!ParseArray( Member ) &&
!Error &&
!ParseString( Member ) &&
!Error &&
!ParsePrimitive( Member ))
{
if (!ParseObject( Member ) && !Error && !ParseArray( Member ) && !Error && !ParseString( Member ) && !Error && !ParsePrimitive( Member ) ) {}
if (Error) {
DataTree->DestroyMember( &Member );
return false;
}

View File

@@ -66,18 +66,18 @@ public:
bool CreateBuffer( int pBufLen );
bool FillBuffer();
void FreeBuffer();
bool ReadFromBuffer( const char * RootPath );
bool ReadFromBuffer( const char * BasePath );
// Input
bool ReadFromHandle( const char * RootPath, const int Handle, bool pRefillBuffer );
bool ReadFromFile( const char * RootPath, const char * Path, const char * FileName );
bool ReadFromFile( const char * RootPath, const char * FilePath );
bool ReadFromHandle( const char * BasePath, const int Handle, bool pRefillBuffer );
bool ReadFromFile( const char * BasePath, const char * Path, const char * FileName );
bool ReadFromFile( const char * BasePath, const char * FilePath );
// Output
bool WriteToHandle( const char * RootPath, const int Handle, const int Indent = 2 );
bool WriteToScreen( const char * RootPath, const int Indent = 2 );
bool WriteToFile( const char * RootPath, const char * Path, const char * FileName, const int Indent = 2 );
bool WriteToFile( const char * RootPath, const char * FilePath, const int Indent = 2 );
bool WriteToHandle( const char * BasePath, const int Handle, const int Indent = 2 );
bool WriteToScreen( const char * BasePath, const int Indent = 2 );
bool WriteToFile( const char * BasePath, const char * Path, const char * FileName, const int Indent = 2 );
bool WriteToFile( const char * BasePath, const char * FilePath, const int Indent = 2 );
const char * GetError() { return ((Error)? ErrorText : "Success"); };
};

View File

@@ -18,7 +18,7 @@
//---------------------------------------------------------------------------
extern char ProcessName[];
extern char * ProcessName;
//---------------------------------------------------------------------------