Code compacting of JSON & DataTree:
- Remove excessive {}
DataTree:
- Use "p" prefix in funtion parameters to avoid object parameter overlap
This commit is contained in:
142
DataTreeCore.cpp
142
DataTreeCore.cpp
@@ -64,9 +64,8 @@ CDataMember::CDataMember( CDataMember * pParent, const char * pName, const int p
|
||||
else {
|
||||
// Clear/reset parent if not object
|
||||
Parent = pParent;
|
||||
if ((Parent->Type != jtNull) && (Parent->Type != jtObject) && (Parent->Type != jtArray)) {
|
||||
if ((Parent->Type != jtNull) && (Parent->Type != jtObject) && (Parent->Type != jtArray))
|
||||
Parent->Clear();
|
||||
}
|
||||
|
||||
// Insert into Parent & Peer lists
|
||||
Parent->Len++;
|
||||
@@ -93,19 +92,15 @@ CDataMember::~CDataMember()
|
||||
// Remove from parent
|
||||
if (Parent) {
|
||||
Parent->Len--;
|
||||
if (this == Parent->LastChild) {
|
||||
if (this == Parent->LastChild)
|
||||
Parent->LastChild = PrevPeer;
|
||||
}
|
||||
if (this == Parent->FirstChild) {
|
||||
if (this == Parent->FirstChild)
|
||||
Parent->FirstChild = NextPeer;
|
||||
}
|
||||
}
|
||||
if (PrevPeer) {
|
||||
if (PrevPeer)
|
||||
PrevPeer->NextPeer = NextPeer;
|
||||
}
|
||||
if (NextPeer) {
|
||||
if (NextPeer)
|
||||
NextPeer->PrevPeer = PrevPeer;
|
||||
}
|
||||
|
||||
// Destroy value/children
|
||||
Clear();
|
||||
@@ -116,10 +111,10 @@ CDataMember::~CDataMember()
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
CDataMember * CDataMember::CreateChild( const char * Name, const int Len )
|
||||
CDataMember * CDataMember::CreateChild( const char * pName, const int pLen )
|
||||
{
|
||||
CDataMember * Member;
|
||||
Member = new CDataMember( this, Name, Len );
|
||||
Member = new CDataMember( this, pName, pLen );
|
||||
return Member;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
@@ -131,9 +126,8 @@ bool CDataMember::Clear()
|
||||
free( Value );
|
||||
|
||||
// Clear children
|
||||
while (FirstChild) {
|
||||
while (FirstChild)
|
||||
delete FirstChild;
|
||||
}
|
||||
|
||||
// Reset value
|
||||
Type = jtNull;
|
||||
@@ -169,9 +163,8 @@ CDataMember * CDataMember::GetChild( const char * Path, bool Create )
|
||||
int Count;
|
||||
|
||||
// Validate
|
||||
if (!Path || !*Path) {
|
||||
if (!Path || !*Path)
|
||||
return this;
|
||||
}
|
||||
|
||||
// Set init references
|
||||
Child = NULL;
|
||||
@@ -179,21 +172,17 @@ CDataMember * CDataMember::GetChild( const char * Path, bool Create )
|
||||
|
||||
// Split path
|
||||
Pos = (char*)Path;
|
||||
while (Member && *Pos)
|
||||
{
|
||||
while (Member && *Pos) {
|
||||
// Reset Child reference
|
||||
Child = NULL;
|
||||
|
||||
// Find delimiter
|
||||
if (*Pos == '[')
|
||||
{
|
||||
if (*Pos == '[') {
|
||||
// Validate
|
||||
if (Create && (Member->Type == jtNull)) {
|
||||
if (Create && (Member->Type == jtNull))
|
||||
Member->Type = jtArray; // Convert to array
|
||||
}
|
||||
if (Member->Type != jtArray) {
|
||||
if (Member->Type != jtArray)
|
||||
break; // Can't convert something else to an array
|
||||
}
|
||||
|
||||
// Set Index start
|
||||
Pos++;
|
||||
@@ -214,10 +203,9 @@ CDataMember * CDataMember::GetChild( const char * Path, bool Create )
|
||||
|
||||
// find end of list
|
||||
Child = &(Member->FirstChild);
|
||||
while (*Child) {
|
||||
while (*Child)
|
||||
Child = &((*Child)->NextPeer);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Get requested index
|
||||
Index = (int)strtoul( Key, &EndPos, 10 );
|
||||
@@ -235,25 +223,21 @@ CDataMember * CDataMember::GetChild( const char * Path, bool Create )
|
||||
|
||||
// Create element if needed
|
||||
if (!*Child && Create) {
|
||||
if ((Index == -1) || (Index == Count)) {
|
||||
if ((Index == -1) || (Index == Count))
|
||||
*Child = new CDataMember( Member, NULL );
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
// Skip separator
|
||||
if (*Pos == '/') {
|
||||
Pos++;
|
||||
}
|
||||
|
||||
// Validate
|
||||
if (Create && (Member->Type == jtNull)) {
|
||||
if (Create && (Member->Type == jtNull))
|
||||
Member->Type = jtObject; // Convert to object
|
||||
}
|
||||
if (Member->Type != jtObject) {
|
||||
if (Member->Type != jtObject)
|
||||
break; // Can't convert something else to an object
|
||||
}
|
||||
|
||||
// Get key value
|
||||
Key = Pos;
|
||||
@@ -265,13 +249,12 @@ CDataMember * CDataMember::GetChild( const char * Path, bool Create )
|
||||
|
||||
// Find next parent
|
||||
Child = &(Member->FirstChild);
|
||||
while (*Child && (((*Child)->NameLen != KeyLen) || strncasecmp( (*Child)->Name, Key, KeyLen ))) {
|
||||
while (*Child && (((*Child)->NameLen != KeyLen) || strncasecmp( (*Child)->Name, Key, KeyLen )))
|
||||
Child = &((*Child)->NextPeer);
|
||||
}
|
||||
if (!*Child && Create) {
|
||||
|
||||
if (!*Child && Create)
|
||||
*Child = new CDataMember( Member, Key, KeyLen );
|
||||
}
|
||||
}
|
||||
|
||||
// Next level
|
||||
Member = *Child;
|
||||
@@ -288,12 +271,10 @@ CDataMember * CDataMember::GetChFirstChild( const char * Path, bool Create )
|
||||
Member = (!Path || !*Path)? this : GetChild( Path, Create );
|
||||
|
||||
// Check if valid type
|
||||
if (!Member || ((Member->Type != jtObject) && (Member->Type != jtNull))) {
|
||||
if (!Member || ((Member->Type != jtObject) && (Member->Type != jtNull)))
|
||||
return NULL;
|
||||
}
|
||||
else if (Create && (Member->Type == jtNull)) {
|
||||
else if (Create && (Member->Type == jtNull))
|
||||
Member->SetObject(); // Set newly created member to object
|
||||
}
|
||||
|
||||
return Member->FirstChild;
|
||||
}
|
||||
@@ -309,12 +290,10 @@ CDataMember * CDataMember::GetChElement( const char * Path, const int Index, boo
|
||||
Member = (!Path || !*Path)? this : GetChild( Path, Create );
|
||||
|
||||
// Check if valid type
|
||||
if (!Member || ((Member->Type != jtArray) && (Member->Type != jtNull))) {
|
||||
if (!Member || ((Member->Type != jtArray) && (Member->Type != jtNull)))
|
||||
return NULL;
|
||||
}
|
||||
else if (Create && (Member->Type == jtNull)) {
|
||||
else if (Create && (Member->Type == jtNull))
|
||||
Member->SetArray(); // Set newly created member to array
|
||||
}
|
||||
|
||||
// Find element at position
|
||||
Count = 0;
|
||||
@@ -354,8 +333,7 @@ bool CDataMember::SetValuePtr( EJsonDataType pType, const char * pValue, int pL
|
||||
Clear();
|
||||
Type = pType;
|
||||
|
||||
if ((pType == jtString) || (pType == jtFloat) || (pType == jtInt) || (pType == jtBool))
|
||||
{
|
||||
if ((pType == jtString) || (pType == jtFloat) || (pType == jtInt) || (pType == jtBool)) {
|
||||
Len = (pLen == -1)? strlen(pValue) : pLen;
|
||||
Value = (char*)pValue;
|
||||
}
|
||||
@@ -370,8 +348,7 @@ bool CDataMember::SetValue( EJsonDataType pType, const char * pValue, int pLen
|
||||
Type = pType;
|
||||
|
||||
// Set new primitive value
|
||||
if ((pType == jtString) || (pType == jtFloat) || (pType == jtInt) || (pType == jtBool))
|
||||
{
|
||||
if ((pType == jtString) || (pType == jtFloat) || (pType == jtInt) || (pType == jtBool)) {
|
||||
Len = (pLen == -1)? strlen(pValue) : pLen;
|
||||
Value = strndup( pValue, Len );
|
||||
}
|
||||
@@ -384,9 +361,8 @@ bool CDataMember::SetChObject( const char * Path )
|
||||
CDataMember * Member;
|
||||
|
||||
// Validate
|
||||
if (!(Member = GetChild( Path, true ))) {
|
||||
if (!(Member = GetChild( Path, true )))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set as Object
|
||||
Member->SetValue( jtObject, NULL );
|
||||
@@ -399,9 +375,8 @@ bool CDataMember::SetChArray( const char * Path )
|
||||
CDataMember * Member;
|
||||
|
||||
// Validate
|
||||
if (!(Member = GetChild( Path, true ))) {
|
||||
if (!(Member = GetChild( Path, true )))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set as Object
|
||||
Member->SetValue( jtArray, NULL );
|
||||
@@ -409,71 +384,65 @@ bool CDataMember::SetChArray( const char * Path )
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool CDataMember::SetChStr( const char * Path, const char * Value, const int Len )
|
||||
bool CDataMember::SetChStr( const char * Path, const char * pValue, const int pLen )
|
||||
{
|
||||
CDataMember * Member;
|
||||
|
||||
// Validate
|
||||
if (!(Member = GetChild( Path, true ))) {
|
||||
if (!(Member = GetChild( Path, true )))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create Value
|
||||
if (!Value) {
|
||||
if (!pValue)
|
||||
Member->SetValue( jtString, "", 0 );
|
||||
}
|
||||
else {
|
||||
Member->SetValue( jtString, Value, Len );
|
||||
}
|
||||
else
|
||||
Member->SetValue( jtString, pValue, pLen );
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool CDataMember::SetChInt( const char * Path, const long Value, const char * Mask )
|
||||
bool CDataMember::SetChInt( const char * Path, const long pValue, const char * Mask )
|
||||
{
|
||||
CDataMember * Member;
|
||||
char ValueStr[20];
|
||||
|
||||
// Validate
|
||||
if (!(Member = GetChild( Path, true ))) {
|
||||
if (!(Member = GetChild( Path, true )))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create Value
|
||||
sprintf( ValueStr, ((Mask)? Mask : "%ld"), Value );
|
||||
sprintf( ValueStr, ((Mask)? Mask : "%ld"), pValue );
|
||||
Member->SetValue( jtInt, ValueStr );
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool CDataMember::SetChFloat( const char * Path, const double Value, const char * Mask )
|
||||
bool CDataMember::SetChFloat( const char * Path, const double pValue, const char * Mask )
|
||||
{
|
||||
CDataMember * Member;
|
||||
char ValueStr[20];
|
||||
|
||||
// Validate
|
||||
if (!(Member = GetChild( Path, true ))) {
|
||||
if (!(Member = GetChild( Path, true )))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create Value
|
||||
sprintf( ValueStr, ((Mask)? Mask : "%lf"), Value );
|
||||
sprintf( ValueStr, ((Mask)? Mask : "%lf"), pValue );
|
||||
Member->SetValue( jtFloat, ValueStr );
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool CDataMember::SetChBool( const char * Path, const bool Value )
|
||||
bool CDataMember::SetChBool( const char * Path, const bool pValue )
|
||||
{
|
||||
CDataMember * Member;
|
||||
|
||||
// Validate
|
||||
if (!(Member = GetChild( Path, true ))) {
|
||||
if (!(Member = GetChild( Path, true )))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create Value
|
||||
Member->SetValue( jtBool, ((Value == 0)? "0" : "1") );
|
||||
Member->SetValue( jtBool, ((pValue == 0)? "0" : "1") );
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
@@ -483,9 +452,8 @@ bool CDataMember::SetChNull( const char * Path )
|
||||
CDataMember * Member;
|
||||
|
||||
// Validate
|
||||
if (!(Member = GetChild( Path, true ))) {
|
||||
if (!(Member = GetChild( Path, true )))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create Value
|
||||
Member->SetValue( jtNull, NULL );
|
||||
@@ -498,9 +466,9 @@ const char * CDataMember::GetChName( const char * Path )
|
||||
CDataMember * Member;
|
||||
|
||||
// Validate
|
||||
if (!(Member = GetChild( Path, false ))) {
|
||||
if (!(Member = GetChild( Path, false )))
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
return Member->Name;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
@@ -510,9 +478,9 @@ EJsonDataType CDataMember::GetChType( const char * Path )
|
||||
CDataMember * Member;
|
||||
|
||||
// Validate
|
||||
if (!(Member = GetChild( Path, false ))) {
|
||||
if (!(Member = GetChild( Path, false )))
|
||||
return jtNull;
|
||||
}
|
||||
else
|
||||
return Member->Type;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
@@ -522,12 +490,10 @@ const int CDataMember::GetChLen( const char * Path )
|
||||
CDataMember * Member;
|
||||
|
||||
// Validate
|
||||
if ((Member = GetChild( Path, false ))) {
|
||||
if ((Member = GetChild( Path, false )))
|
||||
return Member->Len;
|
||||
}
|
||||
else {
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
@@ -550,23 +516,23 @@ const char * CDataMember::GetChStr( const char * Path, const char * Default, boo
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
const char * CDataMember::GetChStr( const char * Path, int &Len, const char * Default, bool Create )
|
||||
const char * CDataMember::GetChStr( const char * Path, int &pLen, const char * Default, bool Create )
|
||||
{
|
||||
CDataMember * Member;
|
||||
|
||||
// Validate
|
||||
if ((Member = GetChild( Path, Create )) &&
|
||||
((Member->Type == jtString) || (Member->Type == jtFloat) || (Member->Type == jtInt) || (Member->Type == jtBool)) ) {
|
||||
Len = Member->Len;
|
||||
pLen = Member->Len;
|
||||
return Member->Value;
|
||||
}
|
||||
else if (Member && Create && (Member->Type == jtNull)) {
|
||||
Member->SetStr( Default );
|
||||
Len = Member->Len;
|
||||
pLen = Member->Len;
|
||||
return Member->Value;
|
||||
}
|
||||
else {
|
||||
Len = 0;
|
||||
pLen = (Default? strlen(Default) : 0);
|
||||
return Default;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,10 +38,10 @@ class CDataMember
|
||||
CDataMember * NextPeer;
|
||||
|
||||
// Manage Members
|
||||
CDataMember * CreateChild( const char * Name, const int Len = -1 );
|
||||
CDataMember * CreateChild( const char * pName, const int pLen = -1 );
|
||||
|
||||
// Set Member value
|
||||
bool SetValue( EJsonDataType Type, const char * Value = NULL, int Len = -1 );
|
||||
bool SetValue( EJsonDataType pType, const char * pValue = NULL, int pLen = -1 );
|
||||
bool SetValuePtr( EJsonDataType pType, const char * pValue, int pLen = -1 );
|
||||
|
||||
public:
|
||||
@@ -50,16 +50,15 @@ public:
|
||||
~CDataMember();
|
||||
|
||||
const char * GetName() { return Name; };
|
||||
EJsonDataType GetType() { return Type; };
|
||||
const int GetLen() { return Len; };
|
||||
|
||||
const char * GetChName( const char * Path );
|
||||
EJsonDataType GetChType( const char * Path );
|
||||
const int GetChLen( const char * Path );
|
||||
|
||||
/* Check Type */
|
||||
|
||||
EJsonDataType GetType() { return Type; };
|
||||
EJsonDataType GetChType( const char * Path );
|
||||
|
||||
inline bool isNull() { return (Type == jtNull); };
|
||||
inline bool isBool() { return (Type == jtBool); };
|
||||
inline bool isInt() { return (Type == jtInt); };
|
||||
@@ -84,13 +83,13 @@ public:
|
||||
/* Read Values */
|
||||
|
||||
inline const char * GetStr( const char * Default = NULL ) { return GetChStr( NULL, Default, false ); };
|
||||
inline const char * GetStr( int &Len, const char * Default = NULL ) { return GetChStr( NULL, Len, Default, false ); };
|
||||
inline const char * GetStr( int &pLen, const char * Default = NULL ) { return GetChStr( NULL, pLen, Default, false ); };
|
||||
inline const bool GetBool( bool Default = false ) { return GetChBool( NULL, Default, false ); };
|
||||
inline const long GetInt( long Default = 0, const char * Mask = NULL ) { return GetChInt( NULL, Default, false, Mask ); };
|
||||
inline const double GetFloat( double Default = 0.0, const char * Mask = NULL ) { return GetChFloat( NULL, Default, false, Mask ); };
|
||||
|
||||
const char * GetChStr( const char * Path, const char * Default = NULL, bool Create = false );
|
||||
const char * GetChStr( const char * Path, int &Len, const char * Default = NULL, bool Create = false );
|
||||
const char * GetChStr( const char * Path, int &pLen, const char * Default = NULL, bool Create = false );
|
||||
const bool GetChBool( const char * Path, bool Default = false, bool Create = false );
|
||||
const long GetChInt( const char * Path, long Default = 0, bool Create = false, const char * Mask = NULL );
|
||||
const double GetChFloat( const char * Path, double Default = 0.0, bool Create = false, const char * Mask = NULL );
|
||||
@@ -101,19 +100,19 @@ public:
|
||||
inline bool SetObject() { return SetChObject( NULL ); };
|
||||
inline bool SetArray() { return SetChArray( NULL ); };
|
||||
|
||||
inline bool SetBool( const bool Value ) { return SetChBool( NULL, Value ); };
|
||||
inline bool SetInt( const long Value, const char * Mask = NULL ) { return SetChInt( NULL, Value, Mask ); };
|
||||
inline bool SetFloat( const double Value, const char * Mask = NULL ) { return SetChFloat( NULL, Value, Mask ); };
|
||||
inline bool SetStr( const char * Value = NULL, const int Len = -1 ) { return SetChStr( NULL, Value, Len ); };
|
||||
inline bool SetBool( const bool pValue ) { return SetChBool( NULL, pValue ); };
|
||||
inline bool SetInt( const long pValue, const char * Mask = NULL ) { return SetChInt( NULL, pValue, Mask ); };
|
||||
inline bool SetFloat( const double pValue, const char * Mask = NULL ) { return SetChFloat( NULL, pValue, Mask ); };
|
||||
inline bool SetStr( const char * pValue = NULL, const int pLen = -1 ) { return SetChStr( NULL, pValue, pLen ); };
|
||||
|
||||
bool SetChStr( const char * Path, const char * Value = NULL, const int Len = -1 ); // Use Len param if Value contains NULL values
|
||||
bool SetChStr( const char * Path, const char * pValue = NULL, const int pLen = -1 ); // Use Len param if Value contains NULL values
|
||||
bool SetChObject( const char * Path );
|
||||
bool SetChArray( const char * Path );
|
||||
|
||||
bool SetChNull( const char * Path );
|
||||
bool SetChBool( const char * Path, const bool Value );
|
||||
bool SetChInt( const char * Path, const long Value, const char * Mask = NULL );
|
||||
bool SetChFloat( const char * Path, const double Value, const char * Mask = NULL );
|
||||
bool SetChBool( const char * Path, const bool pValue );
|
||||
bool SetChInt( const char * Path, const long pValue, const char * Mask = NULL );
|
||||
bool SetChFloat( const char * Path, const double pValue, const char * Mask = NULL );
|
||||
|
||||
/* Clear / Remove values */
|
||||
|
||||
|
||||
@@ -31,9 +31,8 @@ CJSONparse::CJSONparse( CDataMember * pDataTree )
|
||||
CJSONparse::~CJSONparse()
|
||||
{
|
||||
// Destroy buffer
|
||||
if (Buffer) {
|
||||
if (Buffer)
|
||||
delete Buffer;
|
||||
}
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
@@ -118,10 +117,9 @@ bool CJSONparse::WriteToFile( const char * BasePath, const char * Path, const ch
|
||||
if (Path && *Path) {
|
||||
strcpy( FilePath, Path );
|
||||
PathLen = strlen( FilePath );
|
||||
if (FilePath[PathLen] != '/') {
|
||||
if (FilePath[PathLen] != '/')
|
||||
FilePath[PathLen++] = '/';
|
||||
}
|
||||
}
|
||||
strcpy( &FilePath[PathLen], FileName );
|
||||
|
||||
// Read file
|
||||
@@ -215,10 +213,9 @@ bool CJSONparse::ReadFromFile( const char * BasePath, const char * Path, const c
|
||||
if (Path && *Path) {
|
||||
strcpy( FilePath, Path );
|
||||
PathLen = strlen( FilePath );
|
||||
if (FilePath[PathLen] != '/') {
|
||||
if (FilePath[PathLen] != '/')
|
||||
FilePath[PathLen++] = '/';
|
||||
}
|
||||
}
|
||||
strcpy( &FilePath[PathLen], FileName );
|
||||
|
||||
// Read file
|
||||
@@ -357,9 +354,8 @@ bool CJSONparse::ReadFromBuffer( const char * BasePath )
|
||||
CharNo += BufPos-Mark;
|
||||
sprintf( ErrorText, "First entry in file must be an Object or Array on line %d:%d", LineNo, CharNo );
|
||||
}
|
||||
if (Error) {
|
||||
if (Error)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ensure remainder of file is empty
|
||||
SkipWhiteSpace();
|
||||
@@ -428,8 +424,7 @@ void CJSONparse::FreeBuffer()
|
||||
|
||||
void CJSONparse::SkipWhiteSpace()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
while (true) {
|
||||
// Append buffer if required
|
||||
if (!*BufPos && RefillBuffer)
|
||||
FillBuffer();
|
||||
@@ -458,9 +453,8 @@ bool CJSONparse::ParseString( char ** Value, int &Len )
|
||||
char HexVal[5] = "";
|
||||
|
||||
// Check for opening quote
|
||||
if (*BufPos != '"') {
|
||||
if (*BufPos != '"')
|
||||
return false;
|
||||
}
|
||||
|
||||
// Clear values
|
||||
*Value = NULL;
|
||||
@@ -470,12 +464,12 @@ bool CJSONparse::ParseString( char ** Value, int &Len )
|
||||
BufPos++;
|
||||
|
||||
// Check for closing quote
|
||||
while (true)
|
||||
{
|
||||
while (true) {
|
||||
// Check for special characters
|
||||
if ((EndMark = strpbrk( BufPos, "\"\\\n\t\b\f\n\r" ))) { /*"\"/\\\n\t\b\f\n\r" (forward slash included)*/
|
||||
BufPos = EndMark;
|
||||
} else if (RefillBuffer) {
|
||||
}
|
||||
else if (RefillBuffer) {
|
||||
FillBuffer();
|
||||
continue;
|
||||
}
|
||||
@@ -492,10 +486,9 @@ bool CJSONparse::ParseString( char ** Value, int &Len )
|
||||
}
|
||||
else if (*BufPos == '\\') {
|
||||
if (!*(BufPos+1) && RefillBuffer) {
|
||||
if (FillBuffer()) {
|
||||
if (FillBuffer())
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (*(BufPos+1) == 'u') {
|
||||
for (EndMark = BufPos+2; EndMark < BufPos+6; EndMark++) {
|
||||
if (!*EndMark && RefillBuffer) {
|
||||
@@ -538,8 +531,7 @@ bool CJSONparse::ParseString( char ** Value, int &Len )
|
||||
|
||||
// Convert value
|
||||
BufPos = Mark+1;
|
||||
while ((EndMark = strpbrk( BufPos, "\"\\" )))
|
||||
{
|
||||
while ((EndMark = strpbrk( BufPos, "\"\\" ))) {
|
||||
// Copy portion
|
||||
memcpy( ValuePos, BufPos, (EndMark-BufPos) );
|
||||
ValuePos += (EndMark-BufPos);
|
||||
@@ -586,16 +578,14 @@ bool CJSONparse::ParseObject( CDataMember * Object )
|
||||
int Len = 0;
|
||||
|
||||
// Check for start of Object
|
||||
if (*BufPos != '{') {
|
||||
if (*BufPos != '{')
|
||||
return false;
|
||||
}
|
||||
BufPos++;
|
||||
|
||||
// Set Type
|
||||
Object->SetValue( jtObject );
|
||||
|
||||
while (true)
|
||||
{
|
||||
while (true) {
|
||||
// Evaluate key name
|
||||
SkipWhiteSpace();
|
||||
if (*BufPos == '}') {
|
||||
@@ -666,21 +656,18 @@ bool CJSONparse::ParseArray( CDataMember * Array )
|
||||
CDataMember ** Member;
|
||||
|
||||
// Check for start of Object
|
||||
if (*BufPos != '[') {
|
||||
if (*BufPos != '[')
|
||||
return false;
|
||||
}
|
||||
BufPos++;
|
||||
|
||||
// Set Type
|
||||
Array->SetValue( jtArray );
|
||||
Member = &(Array->FirstChild);
|
||||
while (true)
|
||||
{
|
||||
while (true) {
|
||||
// Look for Member Name
|
||||
SkipWhiteSpace();
|
||||
if (*BufPos == ']') {
|
||||
if (*BufPos == ']')
|
||||
break;
|
||||
}
|
||||
|
||||
// Add new element
|
||||
*Member = new CDataMember( Array );
|
||||
@@ -724,9 +711,8 @@ bool CJSONparse::ParseString( CDataMember * Member )
|
||||
int Len = 0;
|
||||
|
||||
// Try to parse
|
||||
if (!ParseString( &Value, Len )) {
|
||||
if (!ParseString( &Value, Len ))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set string
|
||||
Member->SetValuePtr( jtString, Value, Len );
|
||||
@@ -737,7 +723,6 @@ bool CJSONparse::ParseString( CDataMember * Member )
|
||||
|
||||
bool CJSONparse::ParsePrimitive( CDataMember * Member )
|
||||
{
|
||||
char * Value = NULL;
|
||||
int Len = 0;
|
||||
char * EndMark;
|
||||
|
||||
@@ -772,19 +757,13 @@ bool CJSONparse::ParsePrimitive( CDataMember * Member )
|
||||
// Try conversion to int
|
||||
strtol( Mark, &EndMark, 10 );
|
||||
if (EndMark == BufPos) {
|
||||
Value = (char*)malloc( Len+1 );
|
||||
memcpy( Value, Mark, Len );
|
||||
Value[Len] = 0;
|
||||
Member->SetValuePtr( jtInt, Value, Len );
|
||||
Member->SetValue( jtInt, Mark, Len );
|
||||
}
|
||||
else {
|
||||
// Try conversion to float
|
||||
strtod( Mark, &EndMark );
|
||||
if (EndMark == BufPos) {
|
||||
Value = (char*)malloc( Len+1 );
|
||||
memcpy( Value, Mark, Len );
|
||||
Value[Len] = 0;
|
||||
Member->SetValuePtr( jtFloat, Value, Len );
|
||||
Member->SetValue( jtFloat, Mark, Len );
|
||||
}
|
||||
else {
|
||||
Error = true;
|
||||
@@ -810,8 +789,7 @@ bool CJSONparse::PrintString( char * String, int Len )
|
||||
|
||||
// Content
|
||||
BufPos = String;
|
||||
while (true)
|
||||
{
|
||||
while (true) {
|
||||
// Scan for special chars
|
||||
Mark = BufPos;
|
||||
while ((*BufPos >= 32) && (*BufPos <= 126) && (*BufPos != '\\') && /*(*BufPos != '/') &&*/ (*BufPos != '"'))
|
||||
@@ -880,8 +858,7 @@ bool CJSONparse::PrintObject( CDataMember * Object, const int Indent )
|
||||
}
|
||||
|
||||
// Save parameters
|
||||
for (Member = Object->FirstChild; Member != NULL; (Member = Member->NextPeer))
|
||||
{
|
||||
for (Member = Object->FirstChild; Member != NULL; (Member = Member->NextPeer)) {
|
||||
// Whitespace around first bracket
|
||||
if (Indent) {
|
||||
if (First) {
|
||||
@@ -894,20 +871,21 @@ bool CJSONparse::PrintObject( CDataMember * Object, const int Indent )
|
||||
}
|
||||
|
||||
// Print key name
|
||||
if (!PrintString( Member->Name, strlen(Member->Name) ))
|
||||
if (!PrintString( Member->Name, strlen(Member->Name) )) {
|
||||
return false;
|
||||
if (Indent) {
|
||||
}
|
||||
else if (Indent) {
|
||||
if (Print( this, " : ", 3 ) < 0)
|
||||
return false;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
if (Print( this, ":", 1 ) < 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Print value
|
||||
Last = (++Count >= Object->Len);
|
||||
switch (Member->Type)
|
||||
{
|
||||
switch (Member->Type) {
|
||||
case jtNull :
|
||||
if (Print( this, "null", 4 ) < 0)
|
||||
return false;
|
||||
@@ -968,6 +946,7 @@ bool CJSONparse::PrintObject( CDataMember * Object, const int Indent )
|
||||
}
|
||||
if (Print( this, "}", 1 ) < 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
@@ -998,8 +977,7 @@ bool CJSONparse::PrintArray( CDataMember * Array, const int Indent )
|
||||
}
|
||||
|
||||
// Save parameters
|
||||
for (Member = Array->FirstChild; Member != NULL; (Member = Member->NextPeer))
|
||||
{
|
||||
for (Member = Array->FirstChild; Member != NULL; (Member = Member->NextPeer)) {
|
||||
// Whitespace around brace
|
||||
if (Indent) {
|
||||
if (First) {
|
||||
@@ -1012,8 +990,7 @@ bool CJSONparse::PrintArray( CDataMember * Array, const int Indent )
|
||||
}
|
||||
|
||||
Last = (++Count >= Array->Len);
|
||||
switch (Member->Type)
|
||||
{
|
||||
switch (Member->Type) {
|
||||
case jtNull :
|
||||
if (Print( this, "null", 4 ) < 0)
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user