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

@@ -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;
}