Backend/Database/Serializable.cpp
/*!
 * @brief serial to GList
 * @details deserializes a serial into a GList
 *
 * We expect the serial to look like this:
 * type,size,contents|type,size,contents|...|type,size,contents\|
 *
 * this function goes through, one item at a time, and extracts
 * the GType components (type, size, contents) and puts them in the list
 *
 * if any of the extractions fail, the loop quits and the partial list is returned
 *
 * @param serial the serial to extract
 * @param the length of the serial
 * @return the full list with all contents
 */
@brief serial to GList
@details deserializes a serial into a GList

We expect the serial to look like this:
type,size,contents|type,size,contents|...|type,size,contents\|

this function goes through, one item at a time, and extracts
the GType components (type, size, contents) and puts them in the list

if any of the extractions fail, the loop quits and the partial list is returned

@param serial the serial to extract
@param the length of the serial
@return the full list with all contents
@brief serial to GList @details deserializes a serial into a GList We expect the serial to look like this : type size contents|type size contents| ... |type size contents \ | this function goes through one item at a time and extracts the GType components ( type size contents ) and puts them in the list if any of the extractions fail the loop quits and the partial list is returned @param serial the serial to extract @param the length of the serial @return the full list with all contents
int Serializable::Deserialize(GList& retList, const GString& serial, int maxItems)
{
	if (serial.length() == 0)
		return 0;
	GString serialCopy = serial;
	int retLen = 0;
	int itemCounter = 0;
	int nextDel = 0; 
	const bool NOT_ESCAPED = false;
	do
	{
		nextDel = getDelimiterIdx(serialCopy, GString("|"), NOT_ESCAPED);
		int lastDel = getDelimiterIdx(serialCopy, GString("\\|"), NOT_ESCAPED);
		bool isLastBlock = (nextDel == lastDel+1);
		int delimiterLen = ((isLastBlock) && (lastDel > 0)) ? 2 : 1;
		GString cBlock = "";
		if((isLastBlock) && (lastDel > 0))
		{
			cBlock = serialCopy.substr(0, lastDel);
			serialCopy = "";
		}
		else
		{
			cBlock = serialCopy.substr(0, nextDel);
			serialCopy = serialCopy.substr(nextDel+1);
		}
		retLen = serialCopy.size();
		int newType = cBlock.substr(0, sizeof(int)).getInt();
		cBlock = cBlock.substr(sizeof(int)); 
		unsigned int newSize = (unsigned int)(cBlock.substr(0, sizeof(int)).getInt());
		cBlock = cBlock.substr(sizeof(int)); 
		GString newBlock = deserializeContent(cBlock);
		if(newBlock.length() != newSize)
			break;
		cBlock = cBlock.substr(newSize+delimiterLen); 
		retList.addObject(newType, newBlock, newSize);
		++itemCounter;
	} while ((nextDel > 0) && (maxItems>0? (itemCounter < maxItems):true));
	return retLen;
}
int Serializable :: Deserialize ( GList & retList , const GString & serial , int maxItems ) { if ( serial . length ( ) == 0 ) return 0 ; GString serialCopy = serial ; int retLen = 0 ; int itemCounter = 0 ; int nextDel = 0 ; const bool NOT_ESCAPED = false ; do { nextDel = getDelimiterIdx ( serialCopy , GString ( " " ) , NOT_ESCAPED ) ; int lastDel = getDelimiterIdx ( serialCopy , GString ( " \\ " ) , NOT_ESCAPED ) ; bool isLastBlock = ( nextDel == lastDel + 1 ) ; int delimiterLen = ( ( isLastBlock ) && ( lastDel > 0 ) ) ? 2 : 1 ; GString cBlock = " " ; if ( ( isLastBlock ) && ( lastDel > 0 ) ) { cBlock = serialCopy . substr ( 0 , lastDel ) ; serialCopy = " " ; } else { cBlock = serialCopy . substr ( 0 , nextDel ) ; serialCopy = serialCopy . substr ( nextDel + 1 ) ; } retLen = serialCopy . size ( ) ; int newType = cBlock . substr ( 0 , sizeof ( int ) ) . getInt ( ) ; cBlock = cBlock . substr ( sizeof ( int ) ) ; unsigned int newSize = ( unsigned int ) ( cBlock . substr ( 0 , sizeof ( int ) ) . getInt ( ) ) ; cBlock = cBlock . substr ( sizeof ( int ) ) ; GString newBlock = deserializeContent ( cBlock ) ; if ( newBlock . length ( ) != newSize ) break ; cBlock = cBlock . substr ( newSize + delimiterLen ) ; retList . addObject ( newType , newBlock , newSize ) ; ++ itemCounter ; } while ( ( nextDel > 0 ) && ( maxItems > 0 ? ( itemCounter < maxItems ) : true ) ) ; return retLen ; }
@brief serial to GList
 @details deserializes a serial into a GList
@brief serial to GList @details deserializes a serial into a GList
// copy the serial (keep the original intact) // delimiter // Strip the current block off // Get the Type from the buffer // plus the comma // Get the Size from the buffer // plus the comma // Get the Body from the buffer // plus the delimiter