Files
redAcore/JSONparseCore.h
Charl Wentzel f20a9a676f 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
2019-04-07 10:25:44 +02:00

124 lines
4.1 KiB
C++

/*
* JSONparseCore.h
*
* Created on: 5 Mar 2017
* Author: wentzelc
*/
#ifndef REDACORE_JSONPARSECORE_H_
#define REDACORE_JSONPARSECORE_H_
// Standard C/C++ Libraries
#include <string.h>
#include <ctype.h>
#include <unistd.h>
// redA Libraries
#include "DataTreeCore.h"
#include "CharBufferCore.h"
//---------------------------------------------------------------------------
// 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
{
protected:
CDataMember * DataTree = NULL;
// File operation
int InputHandle = -1;
int OutputHandle = -1;
char * OutputStr = NULL;
size_t OutputMaxLen = 0;
size_t OutputPos = 0;
CShiftBuffer * Buffer = NULL;
// Parsing operation
char * BufPos = NULL;
char * Mark = NULL;
int LineNo = 0;
int CharNo = 0;
bool RefillBuffer = false;
// Printing Operation
FOutput Print = NULL;
char Spacer[100] = "";
int SpacerLen = 0;
// Error
bool Error = false;
char ErrorText[100] = "";
// Parsing functions
void SkipWhiteSpace();
bool ParseString( char ** Value, int &pLen );
bool ParseObject( CDataMember * Object );
bool ParseArray( CDataMember * Array );
bool ParseString( CDataMember * Member );
bool ParsePrimitive( CDataMember * Member );
bool PrintString( char * String, int Len );
bool PrintObject( CDataMember * Object, const int Indent );
bool PrintArray( CDataMember * Object, const int Indent );
public:
CJSONparse();
CJSONparse( CDataMember * pDataTree );
~CJSONparse();
// Tree
bool SetBase( CDataMember * Object );
// Buffer operation
bool CreateBuffer( int pBufLen );
bool FillBuffer();
void FreeBuffer();
bool ReadFromBuffer( const char * BasePath );
// Input
bool ReadFromString( const char * BasePath, const char * InString, const int Len );
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 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_ */