4d7293d0f5
--HG-- branch : gsoc2011-kerozcak
180 lines
No EOL
4.1 KiB
PHP
180 lines
No EOL
4.1 KiB
PHP
<?php
|
|
class UxtParser
|
|
{
|
|
var $pipeline_directory = "/home/kaczorek/projects/webtt/distfiles/translation/";
|
|
|
|
function removeComments($str)
|
|
{
|
|
/* while (($cstart = mb_strpos($str, "/*", $offset)) !== false)
|
|
{
|
|
$cend = mb_strpos();
|
|
}*/
|
|
// var_dump($str);
|
|
//As a pertinent note, there's an issue with this function where parsing any string longer than 94326 characters long will silently return null. So be careful where you use it at.
|
|
//http://pl.php.net/manual/en/function.preg-replace.php#98843
|
|
$returnString = preg_replace('!/\*.*?\*/!s', '', $str); // /* .*? */ s
|
|
// PHP 5.2.0
|
|
// if (PREG_NO_ERROR !== preg_last_error())
|
|
if ($returnString === null)
|
|
{
|
|
$returnStr = $str;
|
|
// exception
|
|
}
|
|
return $returnString;
|
|
}
|
|
|
|
function removeBOM($str)
|
|
{
|
|
// mb_internal_encoding("ISO-8859-2");
|
|
// var_dump(substr($str, 0, 3));
|
|
if(($bom = substr($str, 0,3)) == pack("CCC",0xef,0xbb,0xbf))
|
|
{
|
|
// var_dump("jest bom");
|
|
$bla = substr($str, 3);
|
|
// var_dump($bla);
|
|
return $bla;
|
|
}
|
|
else
|
|
{
|
|
// var_dump($bom);
|
|
return $str;
|
|
}
|
|
}
|
|
|
|
function parseLine($str)
|
|
{
|
|
$arr = array();
|
|
// var_dump(mb_internal_encoding());
|
|
// mb_internal_encoding("ISO-8859-2");
|
|
if (mb_substr($str, 0, 2) == "//")
|
|
{
|
|
if (mb_substr($str, 0, 7) == "// DIFF")
|
|
{
|
|
list($j, $type, $command, $args) = explode(" ", $str);
|
|
$command = mb_strtolower($command);
|
|
if ($command == "add" || $command == "changed")
|
|
$index = intval($args);
|
|
else
|
|
unset($type);
|
|
}
|
|
/* else if (mb_substr($str, 0, 8) == "// INDEX")
|
|
{
|
|
list($j, $type, $index) = explode(" ", $str);
|
|
// $arr = explode(" ", $str);
|
|
}*/
|
|
/* if (!isset($type))
|
|
{
|
|
var_dump(isset($type));
|
|
debug_print_backtrace();
|
|
}
|
|
var_dump($type);*/
|
|
if (isset($type))
|
|
{
|
|
$type = mb_strtolower($type);
|
|
$arr = compact("type","command","index");
|
|
}
|
|
}
|
|
else if (!(mb_substr($str, 0, 2) == "//") && mb_strlen($str))
|
|
{
|
|
//list($ident, $j
|
|
$type = "string";
|
|
$lBracket = mb_strpos($str, "[");
|
|
$rBracket = mb_strrpos($str, "]");
|
|
$sStart = $lBracket + 1;
|
|
$sEnd = $rBracket - ($sStart);
|
|
$identifier = trim(mb_substr($str, 0, $lBracket));
|
|
if (!$rBracket)
|
|
$sEnd = mb_strlen($str);
|
|
$string = mb_substr($str, $sStart, $sEnd);
|
|
$string = str_replace(
|
|
array('\\\\', '\[','\]'),
|
|
array('\\', '[',']'), // '
|
|
$string
|
|
);
|
|
$arr = compact("type", "identifier", "string");
|
|
}
|
|
/* echo "<pre>################################\n";
|
|
var_dump($str);
|
|
var_dump($arr);
|
|
echo "</pre>\n";*/
|
|
return $arr;
|
|
}
|
|
|
|
function parseFile($file)
|
|
{
|
|
$parsedEnt = array();
|
|
$newEnt = false;
|
|
$entities = array();
|
|
|
|
// $file = file_get_contents($this->pipeline_directory . $file);
|
|
// var_dump(mb_substr($file, 0,3));
|
|
// var_dump(substr($file, 0,3));
|
|
// var_dump($file);
|
|
$file = $this->removeBOM($file);
|
|
// var_dump($file);
|
|
$file = $this->removeComments($file);
|
|
// var_dump($file);
|
|
$lines = explode("\n", $file);
|
|
// echo "<pre>################################\n";
|
|
foreach ($lines as $line)
|
|
{
|
|
$line = rtrim($line);
|
|
$parsedLine = $this->parseLine($line);
|
|
|
|
if (!$parsedLine)
|
|
continue;
|
|
|
|
if ($parsedLine["type"] == "index")
|
|
$parsedEnt["index"] = $parsedLine["index"];
|
|
|
|
if ($parsedLine["type"] == "string")
|
|
{
|
|
/* echo "%%%% parsedEnt %%%%%\n";
|
|
var_dump($parsedEnt);
|
|
|
|
echo "%%%% parsedLine %%%%%\n";
|
|
var_dump($parsedLine);
|
|
*/
|
|
|
|
if (!$parsedLine['identifier'])
|
|
{
|
|
// echo "ZLACZENIE \n";
|
|
$parsedEnt['string'] .= "\n" . $parsedLine['string'];
|
|
}
|
|
else
|
|
{
|
|
// echo "DODANIE \n";
|
|
$parsedEnt += $parsedLine;
|
|
}
|
|
|
|
/* echo "%%%% parsedEnt after %%%%%\n";
|
|
var_dump($parsedEnt);*/
|
|
}
|
|
|
|
if ($parsedLine["type"] == "diff" && $parsedEnt)
|
|
{
|
|
$newEnt = true;
|
|
}
|
|
|
|
if ($newEnt)
|
|
{
|
|
// var_dump($parsedEnt);
|
|
$entities[] = $parsedEnt;
|
|
$parsedEnt =array();
|
|
$newEnt = false;
|
|
}
|
|
|
|
if ($parsedLine["type"] == "diff")
|
|
{
|
|
$parsedEnt["diff"] = $parsedLine["command"];
|
|
$parsedEnt["index"] = $parsedLine["index"];
|
|
}
|
|
}
|
|
if ($parsedEnt)
|
|
$entities[] = $parsedEnt;
|
|
/* var_dump($entities);
|
|
echo "</pre>\n";*/
|
|
return $entities;
|
|
}
|
|
}
|
|
?>
|