PageRenderTime 63ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/code/ryzom/tools/server/www/webtt/app/vendors/StringParser.php

https://bitbucket.org/mattraykowski/ryzomcore_demoshard
PHP | 316 lines | 223 code | 26 blank | 67 comment | 59 complexity | 02f5f02682ddd74ce301e5d720ddf009 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. Ryzom Core Web-Based Translation Tool
  4. Copyright (C) 2011 Piotr Kaczmarek <p.kaczmarek@openlink.pl>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. ?>
  17. <?php
  18. class StringParser
  19. {
  20. var $pipeline_directory = "/home/kaczorek/projects/webtt/distfiles/translation/";
  21. var $debug = false;
  22. function removeComments($str)
  23. {
  24. /* while (($cstart = mb_strpos($str, "/*", $offset)) !== false)
  25. {
  26. $cend = mb_strpos();
  27. }*/
  28. // var_dump($str);
  29. //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.
  30. //http://pl.php.net/manual/en/function.preg-replace.php#98843
  31. ini_set('pcre.backtrack_limit', 10000000);
  32. //$returnString = preg_replace('!/\*.*?\*/!s', '', $str); // /* .*? */ s
  33. // added [^/] because there was //******* in translation file
  34. $returnString = preg_replace('![^/]/\*.*?\*/!s', '', $str); // /* .*? */ s
  35. // PHP 5.2.0
  36. // if (PREG_NO_ERROR !== preg_last_error())
  37. if ($returnString === null)
  38. {
  39. $returnStr = $str;
  40. var_dump("PREG ERROR");
  41. // exception
  42. }
  43. return $returnString;
  44. }
  45. function removeBOM($str)
  46. {
  47. // mb_internal_encoding("ISO-8859-2");
  48. // var_dump(substr($str, 0, 3));
  49. if(($bom = substr($str, 0,3)) == pack("CCC",0xef,0xbb,0xbf))
  50. {
  51. // var_dump("jest bom");
  52. $bla = substr($str, 3);
  53. // var_dump($bla);
  54. return $bla;
  55. }
  56. else
  57. {
  58. // var_dump($bom);
  59. return $str;
  60. }
  61. }
  62. function addBOM($str)
  63. {
  64. if(($bom = substr($str, 0,3)) != pack("CCC",0xef,0xbb,0xbf))
  65. return pack("CCC",0xef,0xbb,0xbf) . $str;
  66. else
  67. return $str;
  68. }
  69. function parseLine($str)
  70. {
  71. $arr = array();
  72. // var_dump(mb_internal_encoding());
  73. // mb_internal_encoding("ISO-8859-2");
  74. if (mb_substr($str, 0, 2) == "//")
  75. {
  76. if (mb_substr($str, 0, 7) == "// DIFF")
  77. {
  78. list($j, $type, $command, $args) = explode(" ", $str);
  79. $command = mb_strtolower($command);
  80. if ($command == "add" || $command == "changed")
  81. {
  82. $index = intval($args);
  83. $command = mb_substr($str, 3);
  84. }
  85. else
  86. unset($type);
  87. }
  88. else if (mb_substr($str, 0, 8) == "// INDEX")
  89. {
  90. list($j, $type, $index) = explode(" ", $str);
  91. $type = "internal_index";
  92. // $arr = explode(" ", $str);
  93. }
  94. else if (mb_substr($str, 0, 13) == "// HASH_VALUE")
  95. {
  96. list($j, $type, $hash_value) = explode(" ", $str);
  97. $type = "hash_value";
  98. }
  99. /* if (!isset($type))
  100. {
  101. var_dump(isset($type));
  102. debug_print_backtrace();
  103. }
  104. var_dump($type);*/
  105. if (isset($type))
  106. {
  107. $type = mb_strtolower($type);
  108. $arr = compact("type","command","index","hash_value");
  109. }
  110. }
  111. else if (!(mb_substr($str, 0, 2) == "//") && mb_strlen($str))
  112. {
  113. //list($ident, $j
  114. $type = "string";
  115. $lBracket = mb_strpos($str, "[");
  116. $rBracket = mb_strrpos($str, "]");
  117. $sStart = $lBracket + 1;
  118. $sEnd = $rBracket - ($sStart);
  119. $identifier = trim(mb_substr($str, 0, $lBracket));
  120. if (!$rBracket)
  121. $sEnd = mb_strlen($str);
  122. $string = mb_substr($str, $sStart, $sEnd);
  123. $string = str_replace(
  124. array('\\\\', '\[','\]'),
  125. array('\\', '[',']'), // '
  126. $string
  127. );
  128. $arr = compact("type", "identifier", "string");
  129. }
  130. /* echo "<pre>################################\n";
  131. var_dump($str);
  132. var_dump($arr);
  133. echo "</pre>\n";*/
  134. return $arr;
  135. }
  136. function parseFile($file)
  137. {
  138. $parsedEnt = array();
  139. $newEnt = false;
  140. $prevStringLine = false;
  141. $entities = array();
  142. // $file = file_get_contents($this->pipeline_directory . $file);
  143. // var_dump(mb_substr($file, 0,3));
  144. // var_dump(substr($file, 0,3));
  145. // var_dump($file);
  146. $file = $this->removeBOM($file);
  147. // var_dump($file);
  148. $file = $this->removeComments($file);
  149. // var_dump($file);
  150. $lines = explode("\n", $file);
  151. if ($this->debug)
  152. {
  153. echo "<pre>\n\n";
  154. }
  155. $line_no=1;
  156. foreach ($lines as $line)
  157. {
  158. if ($this->debug)
  159. {
  160. echo "\n\t#################### LINE NUMBER " . $line_no++ . "\n\n";
  161. }
  162. // var_dump($line);
  163. // $line = rtrim($line);
  164. // $line = str_replace(array("\r\n","\n","\r"), '', $line);
  165. $line = rtrim($line, "\r\n");
  166. $parsedLine = $this->parseLine($line);
  167. if ($this->debug)
  168. {
  169. echo "%%%% parsedLine\n";
  170. var_dump($parsedLine);
  171. echo "\n";
  172. echo "%%%% prevStringLine\n";
  173. var_dump($prevStringLine);
  174. echo "\n";
  175. }
  176. if (!$parsedLine)
  177. continue;
  178. // if line start with diff (diff files) or hash_value (translated files) and before was line with translation, then we start new ent
  179. if ($prevStringLine && (
  180. ($parsedLine["type"] == "diff" && $parsedEnt) || ($parsedLine["type"] == "hash_value" && $parsedEnt)
  181. ))
  182. {
  183. /* echo "%%%% prevStringLine %%%%%\n";
  184. var_dump($parsedEnt);*/
  185. $newEnt = true;
  186. }
  187. if ($newEnt)
  188. {
  189. if ($this->debug)
  190. {
  191. echo "\t%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  192. echo "\t%%%% newEnt %%%%%%%%% newEnt %%%%%%%%% newEnt %%%%%%%%% newEnt %%%%%\n";
  193. echo "\t%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n";
  194. var_dump($parsedEnt);
  195. }
  196. if (!isset($parsedEnt["diff"]) && !isset($parsedEnt["index"]))
  197. $parsedEnt["index"] = $parsedEnt["internal_index"];
  198. $entities[] = $parsedEnt;
  199. $parsedEnt =array();
  200. $newEnt = false;
  201. }
  202. if ($parsedLine["type"] == "internal_index")
  203. $parsedEnt["internal_index"] = $parsedLine["index"];
  204. if ($parsedLine["type"] == "string")
  205. {
  206. $prevStringLine = true;
  207. if ($this->debug)
  208. {
  209. echo "%%%% parsedEnt %%%%%\n";
  210. var_dump($parsedEnt);
  211. // echo "%%%% parsedLine %%%%%\n";
  212. // var_dump($parsedLine);
  213. }
  214. if (!$parsedLine['identifier'])
  215. {
  216. if ($this->debug) echo "ZLACZENIE \n";
  217. if ($this->debug && !isset($parsedEnt['string']))
  218. {
  219. echo "!isset parsedEnt['string']\n";
  220. var_dump($line);
  221. var_dump($parsedEnt);
  222. var_dump($parsedLine);
  223. }
  224. // $parsedEnt['string'] .= $parsedLine['string'] . "\n";
  225. $parsedEnt['string'] .= "\n" . $parsedLine['string'];
  226. }
  227. else
  228. {
  229. if ($this->debug) echo "DODANIE \n";
  230. $parsedEnt += $parsedLine;
  231. // $parsedEnt['string'] .= "\n";
  232. }
  233. if ($this->debug)
  234. {
  235. echo "%%%% parsedEnt after %%%%%\n";
  236. var_dump($parsedEnt);
  237. }
  238. }
  239. else
  240. $prevStringLine = false;
  241. if ($parsedLine["type"] == "diff")
  242. {
  243. $parsedEnt["diff"] = $parsedEnt["command"] = $parsedLine["command"];
  244. $parsedEnt["index"] = $parsedLine["index"];
  245. }
  246. }
  247. if ($parsedEnt)
  248. {
  249. if (!isset($parsedEnt["diff"]) && !isset($parsedEnt["index"]))
  250. $parsedEnt["index"] = $parsedEnt["internal_index"];
  251. $entities[] = $parsedEnt;
  252. }
  253. if ($this->debug)
  254. {
  255. echo "<pre>";
  256. var_dump($entities);
  257. echo "</pre>\n";
  258. }
  259. return $entities;
  260. }
  261. function CRLF($s)
  262. {
  263. $s = str_replace("\r\n", "\n", $s);
  264. $s = str_replace("\n", "\r\n", $s);
  265. return $s;
  266. }
  267. function buildFile($entities)
  268. {
  269. $content = '';
  270. foreach ($entities as $ent)
  271. {
  272. if (isset($ent['command']))
  273. $content .= '// ' . $ent['command'] . "\n";
  274. if (isset($ent['hash_value']))
  275. $content .= '// HASH_VALUE ' . $ent['hash_value'];
  276. /* if (isset($ent['command']))
  277. $content .= '// INDEX ' . $ent['internal_index'] . "\n";
  278. else*/
  279. if (!isset($ent['command']))
  280. $content .= '// INDEX ' . $ent['index'] . "\n";
  281. $content .= $ent['identifier'] . "\t" . '[' . $ent['string'] . ']' . "\n";
  282. $content .= "\n";
  283. }
  284. return $this->addBOM($this->CRLF($content));
  285. }
  286. }
  287. ?>