Important Update:

- JSONparseCore:
  - Implement output to string
  - Added method WriteToString()
  - Create function pointers which is selected for correct output:
    JSONstrOutput or JSONfileOutput -> Print()
  - Replace all write() statements with Print() statements
  - Modify PrintStr not use dprintf, but sprintf to temp buffer
This commit is contained in:
Charl Wentzel
2019-04-07 10:25:44 +02:00
parent 7acdd8f704
commit f20a9a676f
2 changed files with 126 additions and 46 deletions

View File

@@ -11,6 +11,7 @@
// Standard C/C++ Libraries
#include <string.h>
#include <ctype.h>
#include <unistd.h>
// redA Libraries
#include "DataTreeCore.h"
@@ -18,14 +19,26 @@
//---------------------------------------------------------------------------
// Pointer template for File/String Output functions
class CJSONparse;
inline ssize_t JSONstrOutput( CJSONparse * JSONparse, const char * Data, size_t Len );
inline ssize_t JSONfileOutput( CJSONparse * JSONparse, const char * Data, size_t Len );
typedef ssize_t (*FOutput)( CJSONparse * JSONparse, const char * Data, size_t Len );
//---------------------------------------------------------------------------
class CJSONparse
{
private:
protected:
CDataMember * DataTree = NULL;
// File operation
int InputHandle = -1;
int OutputHandle = -1;
int InputHandle = -1;
int OutputHandle = -1;
char * OutputStr = NULL;
size_t OutputMaxLen = 0;
size_t OutputPos = 0;
CShiftBuffer * Buffer = NULL;
@@ -37,6 +50,7 @@ private:
bool RefillBuffer = false;
// Printing Operation
FOutput Print = NULL;
char Spacer[100] = "";
int SpacerLen = 0;
@@ -77,12 +91,33 @@ public:
bool ReadFromFile( const char * BasePath, const char * FilePath );
// Output
bool WriteToString( const char * BasePath, char * TargetStr, int &MaxLen, const int Indent = 0 );
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"); };
friend inline ssize_t JSONstrOutput( CJSONparse * JSONparse, const char * Data, size_t Len );
friend inline ssize_t JSONfileOutput( CJSONparse * JSONparse, const char * Data, size_t Len );
};
//---------------------------------------------------------------------------
inline ssize_t JSONstrOutput( CJSONparse * JSONparse, const char * Data, size_t Len )
{
if (JSONparse->OutputPos + Len > JSONparse->OutputMaxLen) return -1;
memcpy( &JSONparse->OutputStr[JSONparse->OutputPos], Data, Len );
JSONparse->OutputPos += Len;
JSONparse->OutputStr[JSONparse->OutputPos] = 0;
return Len;
}
//---------------------------------------------------------------------------
inline ssize_t JSONfileOutput( CJSONparse * JSONparse, const char * Data, size_t Len )
{
return write( JSONparse->OutputHandle, Data, Len );
}
//---------------------------------------------------------------------------
#endif /* REDACORE_JSONPARSECORE_H_ */