Linux multi-monitor fullscreen support
[ryzomcore.git] / web / public_php / webtt / app / vendors / StringParser.php
blobf2b4bc1840a32c349f041bae1c75e472efcb009c
1 <?php
2 /*
3 Ryzom Core Web-Based Translation Tool
4 Copyright (C) 2011 Piotr Kaczmarek <p.kaczmarek@openlink.pl>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 <?php
21 class StringParser
23 var $pipeline_directory = "/home/kaczorek/projects/webtt/distfiles/translation/";
24 var $debug = false;
26 function removeComments($str)
28 /* while (($cstart = mb_strpos($str, "/*", $offset)) !== false)
30 $cend = mb_strpos();
31 }*/
32 // var_dump($str);
33 //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.
34 //http://pl.php.net/manual/en/function.preg-replace.php#98843
35 ini_set('pcre.backtrack_limit', 10000000);
36 //$returnString = preg_replace('!/\*.*?\*/!s', '', $str); // /* .*? */ s
37 // added [^/] because there was //******* in translation file
38 $returnString = preg_replace('![^/]/\*.*?\*/!s', '', $str); // /* .*? */ s
39 // PHP 5.2.0
40 // if (PREG_NO_ERROR !== preg_last_error())
41 if ($returnString === null)
43 $returnStr = $str;
44 var_dump("PREG ERROR");
45 // exception
47 return $returnString;
50 function removeBOM($str)
52 // mb_internal_encoding("ISO-8859-2");
53 // var_dump(substr($str, 0, 3));
54 if(($bom = substr($str, 0,3)) == pack("CCC",0xef,0xbb,0xbf))
56 // var_dump("jest bom");
57 $bla = substr($str, 3);
58 // var_dump($bla);
59 return $bla;
61 else
63 // var_dump($bom);
64 return $str;
68 function addBOM($str)
70 if(($bom = substr($str, 0,3)) != pack("CCC",0xef,0xbb,0xbf))
71 return pack("CCC",0xef,0xbb,0xbf) . $str;
72 else
73 return $str;
76 function parseLine($str)
78 $arr = array();
79 // var_dump(mb_internal_encoding());
80 // mb_internal_encoding("ISO-8859-2");
81 if (mb_substr($str, 0, 2) == "//")
83 if (mb_substr($str, 0, 7) == "// DIFF")
85 list($j, $type, $command, $args) = explode(" ", $str);
86 $command = mb_strtolower($command);
87 if ($command == "add" || $command == "changed")
89 $index = intval($args);
90 $command = mb_substr($str, 3);
92 else
93 unset($type);
95 else if (mb_substr($str, 0, 8) == "// INDEX")
97 list($j, $type, $index) = explode(" ", $str);
98 $type = "internal_index";
99 // $arr = explode(" ", $str);
101 else if (mb_substr($str, 0, 13) == "// HASH_VALUE")
103 list($j, $type, $hash_value) = explode(" ", $str);
104 $type = "hash_value";
106 /* if (!isset($type))
108 var_dump(isset($type));
109 debug_print_backtrace();
111 var_dump($type);*/
112 if (isset($type))
114 $type = mb_strtolower($type);
115 $arr = compact("type","command","index","hash_value");
118 else if (!(mb_substr($str, 0, 2) == "//") && mb_strlen($str))
120 //list($ident, $j
121 $type = "string";
122 $lBracket = mb_strpos($str, "[");
123 $rBracket = mb_strrpos($str, "]");
124 $sStart = $lBracket + 1;
125 $sEnd = $rBracket - ($sStart);
126 $identifier = trim(mb_substr($str, 0, $lBracket));
127 if (!$rBracket)
128 $sEnd = mb_strlen($str);
129 $string = mb_substr($str, $sStart, $sEnd);
130 $string = str_replace(
131 array('\\\\', '\[','\]'),
132 array('\\', '[',']'), // '
133 $string
135 $arr = compact("type", "identifier", "string");
137 /* echo "<pre>################################\n";
138 var_dump($str);
139 var_dump($arr);
140 echo "</pre>\n";*/
141 return $arr;
144 function parseFile($file)
146 $parsedEnt = array();
147 $newEnt = false;
148 $prevStringLine = false;
149 $entities = array();
151 // $file = file_get_contents($this->pipeline_directory . $file);
152 // var_dump(mb_substr($file, 0,3));
153 // var_dump(substr($file, 0,3));
154 // var_dump($file);
155 $file = $this->removeBOM($file);
156 // var_dump($file);
157 $file = $this->removeComments($file);
158 // var_dump($file);
159 $lines = explode("\n", $file);
160 if ($this->debug)
162 echo "<pre>\n\n";
164 $line_no=1;
165 foreach ($lines as $line)
167 if ($this->debug)
169 echo "\n\t#################### LINE NUMBER " . $line_no++ . "\n\n";
172 // var_dump($line);
173 // $line = rtrim($line);
174 // $line = str_replace(array("\r\n","\n","\r"), '', $line);
175 $line = rtrim($line, "\r\n");
176 $parsedLine = $this->parseLine($line);
178 if ($this->debug)
180 echo "%%%% parsedLine\n";
181 var_dump($parsedLine);
182 echo "\n";
184 echo "%%%% prevStringLine\n";
185 var_dump($prevStringLine);
186 echo "\n";
189 if (!$parsedLine)
190 continue;
192 // if line start with diff (diff files) or hash_value (translated files) and before was line with translation, then we start new ent
194 if ($prevStringLine && (
195 ($parsedLine["type"] == "diff" && $parsedEnt) || ($parsedLine["type"] == "hash_value" && $parsedEnt)
198 /* echo "%%%% prevStringLine %%%%%\n";
199 var_dump($parsedEnt);*/
200 $newEnt = true;
203 if ($newEnt)
205 if ($this->debug)
207 echo "\t%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
208 echo "\t%%%% newEnt %%%%%%%%% newEnt %%%%%%%%% newEnt %%%%%%%%% newEnt %%%%%\n";
209 echo "\t%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n";
210 var_dump($parsedEnt);
212 if (!isset($parsedEnt["diff"]) && !isset($parsedEnt["index"]))
213 $parsedEnt["index"] = $parsedEnt["internal_index"];
215 $entities[] = $parsedEnt;
216 $parsedEnt =array();
217 $newEnt = false;
220 if ($parsedLine["type"] == "internal_index")
221 $parsedEnt["internal_index"] = $parsedLine["index"];
223 if ($parsedLine["type"] == "string")
225 $prevStringLine = true;
227 if ($this->debug)
229 echo "%%%% parsedEnt %%%%%\n";
230 var_dump($parsedEnt);
232 // echo "%%%% parsedLine %%%%%\n";
233 // var_dump($parsedLine);
236 if (!$parsedLine['identifier'])
238 if ($this->debug) echo "ZLACZENIE \n";
239 if ($this->debug && !isset($parsedEnt['string']))
241 echo "!isset parsedEnt['string']\n";
242 var_dump($line);
243 var_dump($parsedEnt);
244 var_dump($parsedLine);
246 // $parsedEnt['string'] .= $parsedLine['string'] . "\n";
247 $parsedEnt['string'] .= "\n" . $parsedLine['string'];
249 else
251 if ($this->debug) echo "DODANIE \n";
252 $parsedEnt += $parsedLine;
253 // $parsedEnt['string'] .= "\n";
256 if ($this->debug)
258 echo "%%%% parsedEnt after %%%%%\n";
259 var_dump($parsedEnt);
262 else
263 $prevStringLine = false;
265 if ($parsedLine["type"] == "diff")
267 $parsedEnt["diff"] = $parsedEnt["command"] = $parsedLine["command"];
268 $parsedEnt["index"] = $parsedLine["index"];
271 if ($parsedEnt)
273 if (!isset($parsedEnt["diff"]) && !isset($parsedEnt["index"]))
274 $parsedEnt["index"] = $parsedEnt["internal_index"];
276 $entities[] = $parsedEnt;
279 if ($this->debug)
281 echo "<pre>";
282 var_dump($entities);
283 echo "</pre>\n";
285 return $entities;
288 function CRLF($s)
290 $s = str_replace("\r\n", "\n", $s);
291 $s = str_replace("\n", "\r\n", $s);
292 return $s;
296 function buildFile($entities)
298 $content = '';
299 foreach ($entities as $ent)
301 if (isset($ent['command']))
302 $content .= '// ' . $ent['command'] . "\n";
303 if (isset($ent['hash_value']))
304 $content .= '// HASH_VALUE ' . $ent['hash_value'];
305 /* if (isset($ent['command']))
306 $content .= '// INDEX ' . $ent['internal_index'] . "\n";
307 else*/
308 if (!isset($ent['command']))
309 $content .= '// INDEX ' . $ent['index'] . "\n";
310 $content .= $ent['identifier'] . "\t" . '[' . $ent['string'] . ']' . "\n";
311 $content .= "\n";
313 return $this->addBOM($this->CRLF($content));