PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/libs/fshl/fshl-helper.php

https://github.com/OndrejBrejla/apigen
PHP | 107 lines | 68 code | 11 blank | 28 comment | 6 complexity | abf48acc213ac09a6721055ad688e26a MD5 | raw file
  1. <?php
  2. /*
  3. * FastSHL | Universal Syntax HighLighter |
  4. * ---------------------------------------------------------------------
  5. Copyright (C) 2002-2005 Juraj 'hvge' Durech
  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 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. * ---------------------------------------------------------------------
  18. */
  19. // ---------------------------------------------------------------------
  20. // new experimental code grabbed from FSHL 0.V.x (0.5.0)
  21. // ---------------------------------------------------------------------
  22. class fshlHelper {
  23. function getStringSource($var) {
  24. if(!is_string($var)) {
  25. return fshlHelper::getVarContentSource($var);
  26. }
  27. return '"' . strtr($var, array('$'=>'\$','"'=>'\"','\\'=>'\\\\',"\x00"=>'\x00',"\x01"=>'\x01',"\x02"=>'\x02',"\x03"=>'\x03',"\x04"=>'\x04',"\x05"=>'\x05',"\x06"=>'\x06',"\x07"=>'\x07',"\x08"=>'\x08',"\x09"=>'\x09',"\x0a"=>'\x0a',"\x0b"=>'\x0b',"\x0c"=>'\x0c',"\x0d"=>'\x0d',"\x0e"=>'\x0e',"\x0f"=>'\x0f',"\x10"=>'\x10',"\x11"=>'\x11',"\x12"=>'\x12',"\x13"=>'\x13',"\x14"=>'\x14',"\x15"=>'\x15',"\x16"=>'\x16',"\x17"=>'\x17',"\x18"=>'\x18',"\x19"=>'\x19',"\x1a"=>'\x1a',"\x1b"=>'\x1b',"\x1c"=>'\x1c',"\x1d"=>'\x1d',"\x1e"=>'\x1e',"\x1f"=>'\x1f',"\x7f"=>'\x7f',"\x80"=>'\x80',"\x81"=>'\x81',"\x82"=>'\x82',"\x83"=>'\x83',"\x84"=>'\x84',"\x85"=>'\x85',"\x86"=>'\x86',"\x87"=>'\x87',"\x88"=>'\x88',"\x89"=>'\x89',"\x8a"=>'\x8a',"\x8b"=>'\x8b',"\x8c"=>'\x8c',"\x8d"=>'\x8d',"\x8e"=>'\x8e',"\x8f"=>'\x8f',"\x90"=>'\x90',"\x91"=>'\x91',"\x92"=>'\x92',"\x93"=>'\x93',"\x94"=>'\x94',"\x95"=>'\x95',"\x96"=>'\x96',"\x97"=>'\x97',"\x98"=>'\x98',"\x99"=>'\x99',"\x9a"=>'\x9a',"\x9b"=>'\x9b',"\x9c"=>'\x9c',"\x9d"=>'\x9d',"\x9e"=>'\x9e',"\x9f"=>'\x9f',"\xa0"=>'\xa0',"\xa1"=>'\xa1',"\xa2"=>'\xa2',"\xa3"=>'\xa3',"\xa4"=>'\xa4',"\xa5"=>'\xa5',"\xa6"=>'\xa6',"\xa7"=>'\xa7',"\xa8"=>'\xa8',"\xa9"=>'\xa9',"\xaa"=>'\xaa',"\xab"=>'\xab',"\xac"=>'\xac',"\xad"=>'\xad',"\xae"=>'\xae',"\xaf"=>'\xaf',"\xb0"=>'\xb0',"\xb1"=>'\xb1',"\xb2"=>'\xb2',"\xb3"=>'\xb3',"\xb4"=>'\xb4',"\xb5"=>'\xb5',"\xb6"=>'\xb6',"\xb7"=>'\xb7',"\xb8"=>'\xb8',"\xb9"=>'\xb9',"\xba"=>'\xba',"\xbb"=>'\xbb',"\xbc"=>'\xbc',"\xbd"=>'\xbd',"\xbe"=>'\xbe',"\xbf"=>'\xbf',"\xc0"=>'\xc0',"\xc1"=>'\xc1',"\xc2"=>'\xc2',"\xc3"=>'\xc3',"\xc4"=>'\xc4',"\xc5"=>'\xc5',"\xc6"=>'\xc6',"\xc7"=>'\xc7',"\xc8"=>'\xc8',"\xc9"=>'\xc9',"\xca"=>'\xca',"\xcb"=>'\xcb',"\xcc"=>'\xcc',"\xcd"=>'\xcd',"\xce"=>'\xce',"\xcf"=>'\xcf',"\xd0"=>'\xd0',"\xd1"=>'\xd1',"\xd2"=>'\xd2',"\xd3"=>'\xd3',"\xd4"=>'\xd4',"\xd5"=>'\xd5',"\xd6"=>'\xd6',"\xd7"=>'\xd7',"\xd8"=>'\xd8',"\xd9"=>'\xd9',"\xda"=>'\xda',"\xdb"=>'\xdb',"\xdc"=>'\xdc',"\xdd"=>'\xdd',"\xde"=>'\xde',"\xdf"=>'\xdf',"\xe0"=>'\xe0',"\xe1"=>'\xe1',"\xe2"=>'\xe2',"\xe3"=>'\xe3',"\xe4"=>'\xe4',"\xe5"=>'\xe5',"\xe6"=>'\xe6',"\xe7"=>'\xe7',"\xe8"=>'\xe8',"\xe9"=>'\xe9',"\xea"=>'\xea',"\xeb"=>'\xeb',"\xec"=>'\xec',"\xed"=>'\xed',"\xee"=>'\xee',"\xef"=>'\xef',"\xf0"=>'\xf0',"\xf1"=>'\xf1',"\xf2"=>'\xf2',"\xf3"=>'\xf3',"\xf4"=>'\xf4',"\xf5"=>'\xf5',"\xf6"=>'\xf6',"\xf7"=>'\xf7',"\xf8"=>'\xf8',"\xf9"=>'\xf9',"\xfa"=>'\xfa',"\xfb"=>'\xfb',"\xfc"=>'\xfc',"\xfd"=>'\xfd',"\xfe"=>'\xfe',"\xff"=>'\xff'))
  28. . '"';
  29. }
  30. function getVarContentSource($var)
  31. {
  32. if(is_numeric($var)) {
  33. return $var;
  34. }
  35. if(is_null($var)) {
  36. return 'null';
  37. }
  38. if(is_bool($var)) {
  39. return $var ? 'true' : 'false';
  40. }
  41. if(is_string($var)) {
  42. return fshlHelper::getStringSource($var);
  43. }
  44. if(is_array($var)) {
  45. $array = 'array(';
  46. $tmp = ""; $cnt = 0;
  47. foreach($var as $key => $value) {
  48. $tmp .= fshlHelper::getVarContentSource($key).'=>'.fshlHelper::getVarContentSource($value);
  49. $tmp .= ++$cnt < count($var) ? ',' : '';
  50. }
  51. $tmp.=')';
  52. return $array.$tmp;
  53. }
  54. assert(0);
  55. return 'fshlHelper::getVarContentSource error';
  56. }
  57. function getVarSource($varname, $mixed_var) {
  58. return '$'.$varname."=".fshlHelper::getVarContentSource($mixed_var).";\n";
  59. }
  60. function getFncSource($fncname,$param=null) {
  61. return "function $fncname ($param) {\n";
  62. }
  63. function blockIndent($string, $level) {
  64. $tab = str_repeat(" ", $level);
  65. $lines = explode("\n", $string);
  66. $out = null;
  67. foreach($lines as $line) {
  68. $out .= $tab.$line."\n";
  69. }
  70. return $out;
  71. }
  72. } //class fshlHelper
  73. // ---------------------------------------------------------------------
  74. // old style wrappers for generator version < 0.5.0 compatibility
  75. // ---------------------------------------------------------------------
  76. function get_string_source($var, $human_readable = false)
  77. {
  78. return fshlHelper::getStringSource($var, $human_readable);
  79. }
  80. function get_array_source($var)
  81. {
  82. return fshlHelper::getVarContentSource($var);
  83. }
  84. function get_var_source($varname,$var)
  85. {
  86. return fshlHelper::getVarSource($varname, $var);
  87. }
  88. function get_fnc_source($fncname,$param=null)
  89. {
  90. return fshlHelper::getFncSource($fncname,$param);
  91. }