Location: PHPKode > projects > Fretsweb > fretsweb-1.2/fretsweb/admin/phprealizer.php
<?php

/*
Fretsweb - A Frets on Fire chart server
Copyright (C) 2009  Daan Sprenkels

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 3 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, see <http://www.gnu.org/licenses/>.

Thanks to Morten "Snefturp" Pedersen for making the origin of this file
*/

#require_once "admin/common.php";

// Convert a hex string to ascii
function hexToAscii($hexString)
	{
	$stringLen = strlen($hexString);
	if ($stringLen % 2 != 0) {
		return "Error, odd-length string.";
	}
	
	for ($i = 0; $i < $stringLen; $i += 2)
	{
		$decimalValue = hexdec(substr($hexString, $i, 2));
		$returnString .= chr($decimalValue);
	}
	return $returnString;
}

// Read a line, i.e to the next \n or \r
function readLine($string)
	{
	global $currentPos;
	$line = "";
	while ($currentPos < strlen($string))
	// Read a complete line
	{
		$char = substr($string, $currentPos++, 1);
		if ($char == "\n" || $char  == "\r")
		{
			break;
		}
		else {
			$line .= $char;
		}
	}
	return $line;
}

// Uncerealize the string. This is what should be called.
function uncerealize($cerealizedString)
{
	global $currentPos;
	$currentPos = 0;
	$length = strlen($cerealizedString);
	$numOfObjects = 0;
	$buffer;
	$result = array();
	if (!strcmp(substr($cerealizedString,0, strlen("cereal!")), "cereal1") == 0)
	{
		echo "Not a valid cerealized string!";
	} 
	else
	{
		$currentPos += strlen("cereal1\n");
		
		// Figure out how many objects we have
		$numOfObjects = readLine($cerealizedString);		
		
		for($i = 0; $i < $numOfObjects; $i++)
		{
			array_push($result,parseObjects($cerealizedString));	
		}
		
		$result = parseReferences($cerealizedString, $result, $numOfObjects);
		
	}
	return $result;
}


// Internal function that parses the list of objects and returns them in an orded array
function parseObjects($cerealizedString) {
	global $currentPos;
	$length = strlen($cerealizedString);
	$result = array();	
	while ($currentPos < $length) {
		$line = readLine($cerealizedString);
		if (strcmp($line, "dict") == 0) { // dictionary
			return array("dict");
		}
		else if (strcmp($line, "list") == 0) { // list
			return array("list");
		} 
		else if (strcmp($line, "set") == 0) { // set
			return array("set");
		} 
		else if (strcmp($line, "tuple") == 0) { // tuple
			$tupObjectCount = readLine($cerealizedString);
			return parseTuple($cerealizedString, $tupObjectCount);
			
		}
	    else if (strcmp(substr($line, 0, 1), "i") == 0) { // integer
			$int = substr($line, 1, strlen($line) - 1);
			return $int;
		} 
		else if (strcmp(substr($line, 0, 1), "s") == 0) { // string
			$strLen = "";
			for ($i = 1; $i < strlen($line); $i++) 
			{
				$strLen .= substr($line, $i, 1);
			}				
			$string = substr($cerealizedString, $currentPos, $strLen);
			$currentPos += $strLen;				
			return $string;
		} 
	}
}

// Parses a tuple
function parseTuple($string, $objectCount) {
	global $currentPos;
	$objectsFound = 0;
	$length = strlen($string);
	$result = array();
	while ($objectsFound < $objectCount) {
		array_push($result, parseObjects($string, $currentPos));
		$objectsFound++;		
	}
	return $result;	
}

// Parses the reference list, reorders the objects so it's correct
function parseReferences($string, $result, $numOfObjects) {
	global $currentPos;
	$refStart = $currentPos;
	$tmpResult = array();
	$length = strlen($string);
	
	for ($currentObject = 0; $currentObject < $numOfObjects; $currentObject++) {
		if (strcmp($result[$currentObject][0], "dict") == 0) {
			$dict = array();
			// read number of items, then read
			$itemNum = readLine($string);
			for ($i = 0; $i < $itemNum; $i++) {
				$ref = readLine($string);
				$ref = substr($ref, 1, strlen($ref));
				$id = readLine($string);
				$id = substr($id, 1, strlen($id));
				array_push($dict, array($id, $ref));				
			}
			array_push($tmpResult, $dict);
		}
		elseif (strcmp($result[$currentObject][0], "list") == 0)
		{
			$list = array();
			
			$itemNum = readLine($string);			
			for ($i = 0; $i < $itemNum; $i++)
			{
				$id = readLine($string);
				array_push($list, $result[substr($id,1,strlen($id))]);
			}
			array_push($tmpResult, $list);
		}
		elseif (strcmp($result[$currentObject][0], "set") == 0)
		{
			// Not implemented
		}
	}
	
	$finalResult = $tmpResult[0];
	for ($i = 0; $i < count($dict); $i++) {
		$finalResult[$i][1] = $tmpResult[$dict[$i][1]];
	}
	return $finalResult;
}

?>
Return current item: Fretsweb