diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/modules/json/JSONChildren.cpp | 195 | ||||
| -rw-r--r-- | src/modules/json/JSONChildren.h | 466 | ||||
| -rw-r--r-- | src/modules/json/JSONDebug.cpp | 132 | ||||
| -rw-r--r-- | src/modules/json/JSONDebug.h | 136 | ||||
| -rw-r--r-- | src/modules/json/JSONNode.h | 4 | 
5 files changed, 465 insertions, 468 deletions
diff --git a/src/modules/json/JSONChildren.cpp b/src/modules/json/JSONChildren.cpp index f55f0fa676..f52b3eb954 100644 --- a/src/modules/json/JSONChildren.cpp +++ b/src/modules/json/JSONChildren.cpp @@ -1,98 +1,97 @@ -/* - -Miranda IM: the free IM client for Microsoft* Windows* - -Copyright 2000-2009 Miranda ICQ/IM project, -all portions of this codebase are copyrighted to the people -listed in contributors.txt. - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. -*/ - -#include "..\..\core\commonheaders.h" - -#include "JSONChildren.h" -#include "JSONNode.h" - -void jsonChildren::inc(void){ -    if (mysize == mycapacity){  //it's full -	   if (!mycapacity){  //the array hasn't been created yet -		  JSON_ASSERT(!array, JSON_TEXT("Expanding a 0 capacity array, but not null")); -		  #ifdef JSON_LESS_MEMORY -			 array = json_malloc<JSONNode*>(1); -			 mycapacity = 1; -		  #else -			 array = json_malloc<JSONNode*>(8);  //8 seems average for JSON, and it's only 64 bytes -			 mycapacity = 8; -		  #endif -	   } else { -		  #ifdef JSON_LESS_MEMORY -			 mycapacity += 1;  //increment the size of the array -		  #else -			 mycapacity <<= 1;  //double the size of the array -		  #endif -		  array = json_realloc<JSONNode*>(array, mycapacity); -	   } -    } -} - - -void jsonChildren::inc(json_index_t amount){ -    if (!amount) return; -    if (mysize + amount >= mycapacity){  //it's full -	   if (!mycapacity){  //the array hasn't been created yet -		  JSON_ASSERT(!array, JSON_TEXT("Expanding a 0 capacity array, but not null")); -		  #ifdef JSON_LESS_MEMORY -			 array = json_malloc<JSONNode*>(amount); -			 mycapacity = amount; -		  #else -			 array = json_malloc<JSONNode*>(amount > 8 ? amount : 8);  //8 seems average for JSON, and it's only 64 bytes -			 mycapacity = amount > 8 ? amount : 8; -		  #endif -	   } else { -		  #ifdef JSON_LESS_MEMORY -			 mycapacity = mysize + amount;  //increment the size of the array -		  #else -			 while(mysize + amount > mycapacity){ -				mycapacity <<= 1;  //double the size of the array -			 } -		  #endif -		  array = json_realloc<JSONNode*>(array, mycapacity); -	   } -    } -} - -//actually deletes everything within the vector, this is safe to do on an empty or even a null array -void jsonChildren::deleteAll(void){ -    json_foreach((*this), runner){ -        JSON_ASSERT(*runner, JSON_TEXT("a null pointer within the children")); -	   JSONNode::deleteJSONNode(*runner);  //this is why I can't do forward declaration -    } -} - -void jsonChildren::doerase(JSONNode ** position, json_index_t number){ -    JSON_ASSERT(array, JSON_TEXT("erasing something from a null array 2")); -    JSON_ASSERT(position >= array, JSON_TEXT("position is beneath the start of the array 2")); -    JSON_ASSERT(position + number <= array + mysize, JSON_TEXT("erasing out of bounds 2")); -    if (position + number >= array + mysize){ -	   mysize = (json_index_t)(position - array); -	   #ifndef JSON_ISO_STRICT -		  JSON_ASSERT((long long)position - (long long)array >= 0, JSON_TEXT("doing negative allocation")); -	   #endif -    } else { -	   memmove(position, position + number, (mysize - (position - array) - number) * sizeof(JSONNode *)); -	   mysize -= number; -    } -} +/*
 +
 +Miranda IM: the free IM client for Microsoft* Windows*
 +
 +Copyright 2000-2009 Miranda ICQ/IM project,
 +all portions of this codebase are copyrighted to the people
 +listed in contributors.txt.
 +
 +This program is free software; you can redistribute it and/or
 +modify it under the terms of the GNU General Public License
 +as published by the Free Software Foundation; either version 2
 +of the License, or (at your option) any later version.
 +
 +This program is distributed in the hope that it will be useful,
 +but WITHOUT ANY WARRANTY; without even the implied warranty of
 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 +GNU General Public License for more details.
 +
 +You should have received a copy of the GNU General Public License
 +along with this program; if not, write to the Free Software
 +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 +*/
 +
 +#include "..\..\core\commonheaders.h"
 +
 +#include "JSONChildren.h"
 +#include "JSONNode.h"
 +
 +void jsonChildren::inc(void){
 +	if (mysize == mycapacity){  //it's full
 +		if (!mycapacity){  //the array hasn't been created yet
 +			JSON_ASSERT(!array, JSON_TEXT("Expanding a 0 capacity array, but not null"));
 +			#ifdef JSON_LESS_MEMORY
 +				array = json_malloc<JSONNode*>(1);
 +				mycapacity = 1;
 +			#else
 +				array = json_malloc<JSONNode*>(8);  //8 seems average for JSON, and it's only 64 bytes
 +				mycapacity = 8;
 +			#endif
 +		} else {
 +			#ifdef JSON_LESS_MEMORY
 +				mycapacity += 1;  //increment the size of the array
 +			#else
 +				mycapacity <<= 1;  //double the size of the array
 +			#endif
 +			array = json_realloc<JSONNode*>(array, mycapacity);
 +		}
 +	}
 +}
 +
 +void jsonChildren::inc(json_index_t amount){
 +	if (!amount) return;
 +	if (mysize + amount >= mycapacity){  //it's full
 +		if (!mycapacity){  //the array hasn't been created yet
 +			JSON_ASSERT(!array, JSON_TEXT("Expanding a 0 capacity array, but not null"));
 +			#ifdef JSON_LESS_MEMORY
 +				array = json_malloc<JSONNode*>(amount);
 +				mycapacity = amount;
 +			#else
 +				array = json_malloc<JSONNode*>(amount > 8 ? amount : 8);  //8 seems average for JSON, and it's only 64 bytes
 +				mycapacity = amount > 8 ? amount : 8;
 +			#endif
 +		} else {
 +			#ifdef JSON_LESS_MEMORY
 +				mycapacity = mysize + amount;  //increment the size of the array
 +			#else
 +				while(mysize + amount > mycapacity){
 +					mycapacity <<= 1;  //double the size of the array
 +				}
 +			#endif
 +			array = json_realloc<JSONNode*>(array, mycapacity);
 +		}
 +	}
 +}
 +
 +//actually deletes everything within the vector, this is safe to do on an empty or even a null array
 +void jsonChildren::deleteAll(void){
 +	json_foreach((*this), runner){
 +		JSON_ASSERT(*runner, JSON_TEXT("a null pointer within the children"));
 +		JSONNode::deleteJSONNode(*runner);  //this is why I can't do forward declaration
 +	}
 +}
 +
 +void jsonChildren::doerase(JSONNode ** position, json_index_t number){
 +	JSON_ASSERT(array, JSON_TEXT("erasing something from a null array 2"));
 +	JSON_ASSERT(position >= array, JSON_TEXT("position is beneath the start of the array 2"));
 +	JSON_ASSERT(position + number <= array + mysize, JSON_TEXT("erasing out of bounds 2"));
 +	if (position + number >= array + mysize){
 +		mysize = (json_index_t)(position - array);
 +		#ifndef JSON_ISO_STRICT
 +			JSON_ASSERT((long long)position - (long long)array >= 0, JSON_TEXT("doing negative allocation"));
 +		#endif
 +	} else {
 +		memmove(position, position + number, (mysize - (position - array) - number) * sizeof(JSONNode *));
 +		mysize -= number;
 +	}
 +}
 diff --git a/src/modules/json/JSONChildren.h b/src/modules/json/JSONChildren.h index d7a38bb9dc..ce697ffd77 100644 --- a/src/modules/json/JSONChildren.h +++ b/src/modules/json/JSONChildren.h @@ -1,234 +1,232 @@ -#ifndef JSONCHILDREN_H -#define JSONCHILDREN_H - -#include "JSONMemory.h" -#include "JSONDebug.h"  //for JSON_ASSERT macro - -#define json_foreach(children, iterator)\ -    JSONNode ** iterator = children.begin();\ -    for(JSONNode ** iterator##_end = children.end(); iterator != iterator##_end; ++iterator) - -/* - This class is essentially a vector that has been heavily optimized for the specific purpose - of holding JSONNode children.  It acts the same way as a vector, it has a automatically - expanding array.  On destruction, this container automatically destroys everything contained - in it as well, so that you libJSON doesn't have to do that. -  - T is JSONNode*, I can't define it that way directly because JSONNode uses this container, and because - the container deletes the children automatically, forward declaration can't be used - */ - -class JSONNode;  //forward declaration - -class jsonChildren { -public: -    //starts completely empty and the array is not allocated -    jsonChildren(void) : array(0), mysize(0), mycapacity(0) { } -     -    //deletes the array and everything that is contained within it (using delete) -    ~jsonChildren(void){ -	   if (array){  //the following function calls are safe, but take more time than a check here -		  deleteAll(); -		  libjson_free<JSONNode*>(array); -	   } -    } -     -    //increase the size of the array -    void inc(json_index_t amount); -    void inc(void); -     -    //Adds something to the vector, doubling the array if necessary -    void push_back(JSONNode * item){ -	   inc(); -	   array[mysize++] = item; -    } -     -    //Adds something to the front of the vector, doubling the array if necessary -    void push_front(JSONNode * item){ -	   inc(); -	   memmove(array + 1, array, mysize++ * sizeof(JSONNode *)); -	   array[0] = item; -    } -     -    //gets an item out of the vector by it's position -    inline JSONNode * operator[] (json_index_t position) const { -	   JSON_ASSERT(position < mysize, JSON_TEXT("Using [] out of bounds")); -	   JSON_ASSERT(position < mycapacity, JSON_TEXT("Using [] out of bounds")); -	   JSON_ASSERT(array, JSON_TEXT("Array is null")); -	   return array[position]; -    } -     -    //returns the allocated capacity, but keep in mind that some might not be valid -    inline json_index_t capacity() const { -	   return mycapacity; -    } -     -    //returns the number of valid objects within the vector -    inline json_index_t size() const { -	   return mysize; -    } -     -    //tests whether or not the vector is empty -    inline bool empty() const { -	   return mysize == 0; -    } -     -    //clears (and deletes) everything from the vector and sets it's size to 0 -    inline void clear() { -	   if (array){  //don't bother clearing anything if there is nothing in it -		  JSON_ASSERT(mycapacity != 0, JSON_TEXT("mycapacity is not zero, but array is null")); -		  deleteAll(); -		  mysize = 0; -	   } -	   JSON_ASSERT(mysize == 0, JSON_TEXT("mysize is not zero after clear")); -    } -     -    //returns the beginning of the array -    inline JSONNode ** begin(void) const { -	   return array; -    } -     -    //returns the end of the array -    inline JSONNode ** end(void) const { -	   return array + mysize; -    } -     -    //makes sure that even after shirnking and expanding, the iterator is in same relative position -    struct iteratorKeeper { -    public: -	   #ifdef JSON_LIBRARY -		  iteratorKeeper(jsonChildren * pthis, JSONNode ** & position) :  -			 myRelativeOffset((json_index_t)(position - pthis -> array)), -	   #else -		  iteratorKeeper(jsonChildren * pthis, JSONNode ** & position, bool reverse = false) :  -			 myRelativeOffset(reverse ? (json_index_t)(pthis -> array + (size_t)pthis -> mysize - position) : (json_index_t)(position - pthis -> array)), -			 myReverse(reverse), -	   #endif -			 myChildren(pthis),  -			 myPos(position){} - -	   ~iteratorKeeper(void){ -		  #ifdef JSON_LIBRARY -			 myPos = myChildren -> array + myRelativeOffset; -		  #else -			 if (myReverse){ -				myPos = myChildren -> array + myChildren -> mysize - myRelativeOffset; -			 } else { -				myPos = myChildren -> array + myRelativeOffset; -			 } -		  #endif -	   } -    private: -	   iteratorKeeper(const iteratorKeeper &); -	   iteratorKeeper & operator = (const iteratorKeeper &); -	    -	   jsonChildren * myChildren; -	   JSONNode ** & myPos; -	   json_index_t myRelativeOffset; -	   #ifndef JSON_LIBRARY -		  bool myReverse BITS(1); -	   #endif -    }; -     -    //This function DOES NOT delete the item it points to -    inline void erase(JSONNode ** & position){ -	   JSON_ASSERT(array, JSON_TEXT("erasing something from a null array 1")); -	   JSON_ASSERT(position >= array, JSON_TEXT("position is beneath the start of the array 1")); -	   JSON_ASSERT(position <= array + mysize, JSON_TEXT("erasing out of bounds 1")); -	   memmove(position, position + 1, (mysize-- - (position - array) - 1) * sizeof(JSONNode *)); -	   iteratorKeeper ik(this, position); -	   shrink(); -    } -     -    //This function DOES NOT delete the item it points to -    inline void erase(JSONNode ** & position, json_index_t number){ -	   doerase(position, number); -	   iteratorKeeper ik(this, position); -	   shrink(); -    } -     -     -    //This function DOES NOT delete the item it points to -    inline void erase(JSONNode ** position, json_index_t number, JSONNode ** & starter){ -	   doerase(position, number); -	   iteratorKeeper ik(this, starter); -	   shrink(); -    } -     -    #ifdef JSON_LIBRARY -	   void insert(JSONNode ** & position, JSONNode * item){ -    #else -	   void insert(JSONNode ** & position, JSONNode * item, bool reverse = false){ -    #endif -	   //position isnt relative to array because of realloc -	   JSON_ASSERT(position >= array, JSON_TEXT("position is beneath the start of the array insert 1")); -	   JSON_ASSERT(position <= array + mysize, JSON_TEXT("position is above the end of the array insert 1")); -	   { -		  #ifdef JSON_LIBRARY -			 iteratorKeeper ik(this, position); -		  #else -			 iteratorKeeper ik(this, position, reverse); -		  #endif -		  inc(); -	   } -	   memmove(position + 1, position, (mysize++ - (position - array)) * sizeof(JSONNode *)); -	   *position = item; -    } - -    void insert(JSONNode ** & position, JSONNode ** items, json_index_t num){ -	   JSON_ASSERT(position >= array, JSON_TEXT("position is beneath the start of the array insert 2")); -	   JSON_ASSERT(position <= array + mysize, JSON_TEXT("position is above the end of the array insert 2")); -	   { -		  iteratorKeeper ik(this, position); -		  inc(num); -	   } -	   const size_t ptrs = ((JSONNode **)(array + mysize)) - position; -	   memmove(position + num, position, ptrs * sizeof(JSONNode *)); -	   memcpy(position, items, num * sizeof(JSONNode *)); -	   mysize += num; -    } -     -    inline void reserve(json_index_t amount){ -	   JSON_ASSERT(!array, JSON_TEXT("reserve is not meant to expand a preexisting array")); -	   JSON_ASSERT(!mycapacity, JSON_TEXT("reservec is not meant to expand a preexisting array")); -	   JSON_ASSERT(!mysize, JSON_TEXT("reserves is not meant to expand a preexisting array")); -	   array = json_malloc<JSONNode*>(mycapacity = amount); -    } - -    inline void reserve2(json_index_t amount){ -	   if (array){ -		  if (mycapacity < amount) inc(amount - mycapacity); -	   } else { -		  reserve(amount); -	   } -    } -		   -    //shrinks the array to only as large as it needs to be to hold everything within it -    inline void shrink() { -	   if (mysize == 0){  //size is zero, we should completely free the array -		  libjson_free<JSONNode*>(array);  //free does checks for a null pointer, so don't bother checking -		  array = 0; -		  #ifdef JSON_LESS_MEMORY -			 } else {  //need to shrink it, using realloc -				JSON_ASSERT(array, JSON_TEXT("shrinking a null array that is not size 0")); -				array = json_realloc<JSONNode*>(array, mysize); -		  #endif   -	   } -	   mycapacity = mysize; -    } -JSON_PRIVATE -    //to make sure it's not copyable -    jsonChildren(const jsonChildren &); -    jsonChildren & operator = (const jsonChildren &); -     -    void deleteAll(void);  //implemented in JSONNode.cpp -    void doerase(JSONNode ** position, json_index_t number); -     -    JSONNode ** array;  //the expandable array - -    json_index_t mysize;	     //the number of valid items -    json_index_t mycapacity;   //the number of possible items -}; - - -#endif +#ifndef JSONCHILDREN_H
 +#define JSONCHILDREN_H
 +
 +#include "JSONMemory.h"
 +#include "JSONDebug.h"  //for JSON_ASSERT macro
 +
 +#define json_foreach(children, iterator)\
 +	JSONNode ** iterator = children.begin();\
 +	for(JSONNode ** iterator##_end = children.end(); iterator != iterator##_end; ++iterator)
 +
 +/*
 + This class is essentially a vector that has been heavily optimized for the specific purpose
 + of holding JSONNode children.  It acts the same way as a vector, it has a automatically
 + expanding array.  On destruction, this container automatically destroys everything contained
 + in it as well, so that you libJSON doesn't have to do that.
 + 
 + T is JSONNode*, I can't define it that way directly because JSONNode uses this container, and because
 + the container deletes the children automatically, forward declaration can't be used
 + */
 +
 +class JSONNode;  //forward declaration
 +
 +class jsonChildren {
 +public:
 +	//starts completely empty and the array is not allocated
 +	jsonChildren(void) : array(0), mysize(0), mycapacity(0) { }
 +
 +	//deletes the array and everything that is contained within it (using delete)
 +	~jsonChildren(void){
 +		if (array){  //the following function calls are safe, but take more time than a check here
 +			deleteAll();
 +			libjson_free<JSONNode*>(array);
 +		}
 +	}
 +
 +	//increase the size of the array
 +	void inc(json_index_t amount);
 +	void inc(void);
 +
 +	//Adds something to the vector, doubling the array if necessary
 +	void push_back(JSONNode * item){
 +		inc();
 +		array[mysize++] = item;
 +	}
 +
 +	//Adds something to the front of the vector, doubling the array if necessary
 +	void push_front(JSONNode * item){
 +		inc();
 +		memmove(array + 1, array, mysize++ * sizeof(JSONNode *));
 +		array[0] = item;
 +	}
 +
 +	//gets an item out of the vector by it's position
 +	inline JSONNode * operator[] (json_index_t position) const {
 +		JSON_ASSERT(position < mysize, JSON_TEXT("Using [] out of bounds"));
 +		JSON_ASSERT(position < mycapacity, JSON_TEXT("Using [] out of bounds"));
 +		JSON_ASSERT(array, JSON_TEXT("Array is null"));
 +		return array[position];
 +	}
 +
 +	//returns the allocated capacity, but keep in mind that some might not be valid
 +	inline json_index_t capacity() const {
 +		return mycapacity;
 +	}
 +
 +	//returns the number of valid objects within the vector
 +	inline json_index_t size() const {
 +		return mysize;
 +	}
 +
 +	//tests whether or not the vector is empty
 +	inline bool empty() const {
 +		return mysize == 0;
 +	}
 +
 +	//clears (and deletes) everything from the vector and sets it's size to 0
 +	inline void clear() {
 +		if (array){  //don't bother clearing anything if there is nothing in it
 +			JSON_ASSERT(mycapacity != 0, JSON_TEXT("mycapacity is not zero, but array is null"));
 +			deleteAll();
 +			mysize = 0;
 +		}
 +		JSON_ASSERT(mysize == 0, JSON_TEXT("mysize is not zero after clear"));
 +	}
 +
 +	//returns the beginning of the array
 +	inline JSONNode ** begin(void) const {
 +		return array;
 +	}
 +
 +	//returns the end of the array
 +	inline JSONNode ** end(void) const {
 +		return array + mysize;
 +	}
 +
 +	//makes sure that even after shirnking and expanding, the iterator is in same relative position
 +	struct iteratorKeeper {
 +	public:
 +		#ifdef JSON_LIBRARY
 +			iteratorKeeper(jsonChildren * pthis, JSONNode ** & position) : 
 +				myRelativeOffset((json_index_t)(position - pthis -> array)),
 +		#else
 +			iteratorKeeper(jsonChildren * pthis, JSONNode ** & position, bool reverse = false) : 
 +				myRelativeOffset(reverse ? (json_index_t)(pthis -> array + (size_t)pthis -> mysize - position) : (json_index_t)(position - pthis -> array)),
 +				myReverse(reverse),
 +		#endif
 +		myChildren(pthis), 
 +		myPos(position){}
 +
 +		~iteratorKeeper(void){
 +			#ifdef JSON_LIBRARY
 +				myPos = myChildren -> array + myRelativeOffset;
 +			#else
 +				if (myReverse){
 +					myPos = myChildren -> array + myChildren -> mysize - myRelativeOffset;
 +				} else {
 +					myPos = myChildren -> array + myRelativeOffset;
 +				}
 +			#endif
 +		}
 +	private:
 +		iteratorKeeper(const iteratorKeeper &);
 +		iteratorKeeper & operator = (const iteratorKeeper &);
 +
 +		jsonChildren * myChildren;
 +		JSONNode ** & myPos;
 +		json_index_t myRelativeOffset;
 +		#ifndef JSON_LIBRARY
 +			bool myReverse BITS(1);
 +		#endif
 +	};
 +
 +	//This function DOES NOT delete the item it points to
 +	inline void erase(JSONNode ** & position){
 +		JSON_ASSERT(array, JSON_TEXT("erasing something from a null array 1"));
 +		JSON_ASSERT(position >= array, JSON_TEXT("position is beneath the start of the array 1"));
 +		JSON_ASSERT(position <= array + mysize, JSON_TEXT("erasing out of bounds 1"));
 +		memmove(position, position + 1, (mysize-- - (position - array) - 1) * sizeof(JSONNode *));
 +		iteratorKeeper ik(this, position);
 +		shrink();
 +	}
 +
 +	//This function DOES NOT delete the item it points to
 +	inline void erase(JSONNode ** & position, json_index_t number){
 +		doerase(position, number);
 +		iteratorKeeper ik(this, position);
 +		shrink();
 +	}
 +
 +	//This function DOES NOT delete the item it points to
 +	inline void erase(JSONNode ** position, json_index_t number, JSONNode ** & starter){
 +		doerase(position, number);
 +		iteratorKeeper ik(this, starter);
 +		shrink();
 +	}
 +
 +	#ifdef JSON_LIBRARY
 +		void insert(JSONNode ** & position, JSONNode * item){
 +	#else
 +		void insert(JSONNode ** & position, JSONNode * item, bool reverse = false){
 +	#endif
 +		//position isnt relative to array because of realloc
 +		JSON_ASSERT(position >= array, JSON_TEXT("position is beneath the start of the array insert 1"));
 +		JSON_ASSERT(position <= array + mysize, JSON_TEXT("position is above the end of the array insert 1"));
 +		{
 +			#ifdef JSON_LIBRARY
 +				iteratorKeeper ik(this, position);
 +			#else
 +				iteratorKeeper ik(this, position, reverse);
 +			#endif
 +			inc();
 +		}
 +		memmove(position + 1, position, (mysize++ - (position - array)) * sizeof(JSONNode *));
 +		*position = item;
 +	}
 +
 +	void insert(JSONNode ** & position, JSONNode ** items, json_index_t num){
 +		JSON_ASSERT(position >= array, JSON_TEXT("position is beneath the start of the array insert 2"));
 +		JSON_ASSERT(position <= array + mysize, JSON_TEXT("position is above the end of the array insert 2"));
 +		{
 +			iteratorKeeper ik(this, position);
 +			inc(num);
 +		}
 +		const size_t ptrs = ((JSONNode **)(array + mysize)) - position;
 +		memmove(position + num, position, ptrs * sizeof(JSONNode *));
 +		memcpy(position, items, num * sizeof(JSONNode *));
 +		mysize += num;
 +	}
 +
 +	inline void reserve(json_index_t amount){
 +		JSON_ASSERT(!array, JSON_TEXT("reserve is not meant to expand a preexisting array"));
 +		JSON_ASSERT(!mycapacity, JSON_TEXT("reservec is not meant to expand a preexisting array"));
 +		JSON_ASSERT(!mysize, JSON_TEXT("reserves is not meant to expand a preexisting array"));
 +		array = json_malloc<JSONNode*>(mycapacity = amount);
 +	}
 +
 +	inline void reserve2(json_index_t amount){
 +		if (array){
 +			if (mycapacity < amount) inc(amount - mycapacity);
 +		} else {
 +			reserve(amount);
 +		}
 +	}
 +
 +	//shrinks the array to only as large as it needs to be to hold everything within it
 +	inline void shrink() {
 +		if (mysize == 0){  //size is zero, we should completely free the array
 +			libjson_free<JSONNode*>(array);  //free does checks for a null pointer, so don't bother checking
 +			array = 0;
 +			#ifdef JSON_LESS_MEMORY
 +				} else {  //need to shrink it, using realloc
 +					JSON_ASSERT(array, JSON_TEXT("shrinking a null array that is not size 0"));
 +					array = json_realloc<JSONNode*>(array, mysize);
 +			#endif
 +		}
 +		mycapacity = mysize;
 +	}
 +JSON_PRIVATE
 +	//to make sure it's not copyable
 +	jsonChildren(const jsonChildren &);
 +	jsonChildren & operator = (const jsonChildren &);
 +
 +	void deleteAll(void);  //implemented in JSONNode.cpp
 +	void doerase(JSONNode ** position, json_index_t number);
 +
 +	JSONNode ** array;  //the expandable array
 +
 +	json_index_t mysize;	     //the number of valid items
 +	json_index_t mycapacity;   //the number of possible items
 +};
 +
 +#endif
 diff --git a/src/modules/json/JSONDebug.cpp b/src/modules/json/JSONDebug.cpp index 990a594679..118ea3a52b 100644 --- a/src/modules/json/JSONDebug.cpp +++ b/src/modules/json/JSONDebug.cpp @@ -1,66 +1,66 @@ -/* - -Miranda IM: the free IM client for Microsoft* Windows* - -Copyright 2000-2009 Miranda ICQ/IM project, -all portions of this codebase are copyrighted to the people -listed in contributors.txt. - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. -*/ - -#include "..\..\core\commonheaders.h" - -#include "libJSON.h" -#include "m_json.h" - -#ifdef JSON_DEBUG - -#ifdef JSON_STDERROR -    #include <iostream>  //need std::cerr -#else -    //otherwise, use a callback to tell the end user what happened -    json_error_callback_t ErrorCallback = 0; -    void JSONDebug::register_callback(json_error_callback_t callback){ -	   ErrorCallback = callback; -    } -#endif - -//Something went wrong or an assert failed -void JSONDebug::_JSON_FAIL(const json_string & msg){ -    #ifdef JSON_STDERROR  //no callback, just use stderror -	   #ifndef JSON_UNICODE -		  std::cerr << msg << std::endl; -	   #else -		  std::cerr << std::string(msg.begin(), msg.end()) << std::endl; -	   #endif -    #else -	   if (ErrorCallback){  //only do anything if the callback is registered -		  #ifdef JSON_LIBRARY -			 ErrorCallback(msg.c_str()); -		  #else -			 ErrorCallback(msg); -		  #endif -	   } -    #endif -} - -//asserts that condition is true, more useful than cassert because it lets you keep going -void JSONDebug::_JSON_ASSERT(bool condition, const json_string & msg){ -    if (!condition){ -	   _JSON_FAIL(msg); -    } -} -#endif +/*
 +
 +Miranda IM: the free IM client for Microsoft* Windows*
 +
 +Copyright 2000-2009 Miranda ICQ/IM project,
 +all portions of this codebase are copyrighted to the people
 +listed in contributors.txt.
 +
 +This program is free software; you can redistribute it and/or
 +modify it under the terms of the GNU General Public License
 +as published by the Free Software Foundation; either version 2
 +of the License, or (at your option) any later version.
 +
 +This program is distributed in the hope that it will be useful,
 +but WITHOUT ANY WARRANTY; without even the implied warranty of
 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 +GNU General Public License for more details.
 +
 +You should have received a copy of the GNU General Public License
 +along with this program; if not, write to the Free Software
 +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 +*/
 +
 +#include "..\..\core\commonheaders.h"
 +
 +#include "libJSON.h"
 +#include "m_json.h"
 +
 +#ifdef JSON_DEBUG
 +
 +#ifdef JSON_STDERROR
 +	#include <iostream>  //need std::cerr
 +#else
 +	//otherwise, use a callback to tell the end user what happened
 +	json_error_callback_t ErrorCallback = 0;
 +	void JSONDebug::register_callback(json_error_callback_t callback){
 +		ErrorCallback = callback;
 +	}
 +#endif
 +
 +//Something went wrong or an assert failed
 +void JSONDebug::_JSON_FAIL(const json_string & msg){
 +	#ifdef JSON_STDERROR  //no callback, just use stderror
 +		#ifndef JSON_UNICODE
 +			std::cerr << msg << std::endl;
 +		#else
 +			std::cerr << std::string(msg.begin(), msg.end()) << std::endl;
 +		#endif
 +	#else
 +		if (ErrorCallback){  //only do anything if the callback is registered
 +			#ifdef JSON_LIBRARY
 +				ErrorCallback(msg.c_str());
 +			#else
 +				ErrorCallback(msg);
 +			#endif
 +		}
 +	#endif
 +}
 +
 +//asserts that condition is true, more useful than cassert because it lets you keep going
 +void JSONDebug::_JSON_ASSERT(bool condition, const json_string & msg){
 +	if (!condition){
 +		_JSON_FAIL(msg);
 +	}
 +}
 +#endif
 diff --git a/src/modules/json/JSONDebug.h b/src/modules/json/JSONDebug.h index 1d4c89fbaa..ff1cedf8fc 100644 --- a/src/modules/json/JSONDebug.h +++ b/src/modules/json/JSONDebug.h @@ -1,68 +1,68 @@ -#ifndef JSON_DEBUG_H -#define JSON_DEBUG_H - -#include "JSONDefs.h" -#include "JSONOptions.h" - -#ifdef JSON_UNIT_TEST -    #define JSON_PRIVATE -#else -    #define JSON_PRIVATE private: -#endif - -#ifdef JSON_DEBUG -    #ifdef JSON_SAFE -	   #define JSON_ASSERT_SAFE(condition, msg, code)\ -		  {\ -			 if (!(condition)) {\ -				JSON_FAIL(msg);\ -				code\ -			 }\ -		  } -	   #define JSON_FAIL_SAFE(msg, code)\ -		  {\ -			 JSON_FAIL(msg);\ -			 code\ -		  } -    #else -	   #define JSON_ASSERT_SAFE(condition, msg, code) JSON_ASSERT(condition, msg) -	   #define JSON_FAIL_SAFE(msg, code) JSON_FAIL(msg) -    #endif - -    #define JSON_FAIL JSONDebug::_JSON_FAIL	 -    #define JSON_ASSERT JSONDebug::_JSON_ASSERT	 - -    class JSONDebug { -    public: -	   #ifndef JSON_STDERROR -		  static void register_callback(json_error_callback_t callback); -	   #endif -	   static void _JSON_FAIL(const json_string & msg); -	   static void _JSON_ASSERT(bool condition, const json_string & msg); -    }; -#else -    #ifdef JSON_SAFE -	   #define JSON_ASSERT_SAFE(condition, msg, code)\ -		  {\ -			 if (!(condition)) {\ -				code\ -			 }\ -		  } -	   #define JSON_FAIL_SAFE(msg, code)\ -		  {\ -			 code\ -		  } -    #else -	   #define JSON_ASSERT_SAFE(condition, msg, code) -	   #define JSON_FAIL_SAFE(msg, code) -    #endif - -    #define JSON_ASSERT(condition, msg) -    #define JSON_FAIL(msg) -#endif - -static const json_string EMPTY_STRING; -static const std::string EMPTY_STRING2; - -#endif - +#ifndef JSON_DEBUG_H
 +#define JSON_DEBUG_H
 +
 +#include "JSONDefs.h"
 +#include "JSONOptions.h"
 +
 +#ifdef JSON_UNIT_TEST
 +	#define JSON_PRIVATE
 +#else
 +	#define JSON_PRIVATE private:
 +#endif
 +
 +#ifdef JSON_DEBUG
 +	#ifdef JSON_SAFE
 +		#define JSON_ASSERT_SAFE(condition, msg, code)\
 +			{\
 +				if (!(condition)) {\
 +					JSON_FAIL(msg);\
 +					code\
 +				}\
 +			}
 +		#define JSON_FAIL_SAFE(msg, code)\
 +			{\
 +				JSON_FAIL(msg);\
 +				code\
 +			}
 +	#else
 +		#define JSON_ASSERT_SAFE(condition, msg, code) JSON_ASSERT(condition, msg)
 +		#define JSON_FAIL_SAFE(msg, code) JSON_FAIL(msg)
 +	#endif
 +
 +	#define JSON_FAIL JSONDebug::_JSON_FAIL
 +	#define JSON_ASSERT JSONDebug::_JSON_ASSERT
 +
 +	class JSONDebug {
 +	public:
 +		#ifndef JSON_STDERROR
 +			static void register_callback(json_error_callback_t callback);
 +		#endif
 +		static void _JSON_FAIL(const json_string & msg);
 +		static void _JSON_ASSERT(bool condition, const json_string & msg);
 +	};
 +#else
 +	#ifdef JSON_SAFE
 +		#define JSON_ASSERT_SAFE(condition, msg, code)\
 +			{\
 +				if (!(condition)) {\
 +					code\
 +				}\
 +			}
 +		#define JSON_FAIL_SAFE(msg, code)\
 +			{\
 +				code\
 +			}
 +	#else
 +		#define JSON_ASSERT_SAFE(condition, msg, code)
 +		#define JSON_FAIL_SAFE(msg, code)
 +	#endif
 +
 +	#define JSON_ASSERT(condition, msg)
 +	#define JSON_FAIL(msg)
 +#endif
 +
 +static const json_string EMPTY_STRING;
 +static const std::string EMPTY_STRING2;
 +
 +#endif
 +
 diff --git a/src/modules/json/JSONNode.h b/src/modules/json/JSONNode.h index 0c4f19d08d..3552f9f73c 100644 --- a/src/modules/json/JSONNode.h +++ b/src/modules/json/JSONNode.h @@ -61,7 +61,7 @@      #define DECLARE_FOR_ALL_TYPES_CONST(foo)\  	   foo(char) const; foo(unsigned char) const;\  	   foo(short) const; foo(unsigned short) const;\ -	   foo(int)const; foo(unsigned int) const;\ +	   foo(int) const; foo(unsigned int) const;\  	   foo(long) const; foo(unsigned long) const;\  	   foo(float) const; foo(double) const;\  	   foo(bool) const;\ @@ -72,7 +72,7 @@      #define IMPLEMENT_FOR_ALL_NUMBERS(foo)\  	   foo(char) foo(unsigned char)\  	   foo(short) foo(unsigned short)\ -	   foo(int)foo(unsigned int)\ +	   foo(int) foo(unsigned int)\  	   foo(long) foo(unsigned long)\  	   foo(float) foo(double)  | 
