XSUtil.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * XSUtil 类定义文件
  4. *
  5. * @author hightman
  6. * @link http://www.xunsearch.com/
  7. * @copyright Copyright &copy; 2011 HangZhou YunSheng Network Technology Co., Ltd.
  8. * @license http://www.xunsearch.com/license/
  9. * @version $Id$
  10. */
  11. /**
  12. * XSUtil 工具程序通用代码
  13. *
  14. * @author hightman <hightman@twomice.net>
  15. * @version 1.0.0
  16. * @package XS.util
  17. */
  18. class XSUtil
  19. {
  20. private static $optind, $options = null;
  21. private static $charset = null;
  22. /**
  23. * 修正字符串至固定宽度
  24. * 其中一个全角符号、汉字的宽度为半角字符的 2 倍。
  25. * @param string $text 要修正的字符串
  26. * @param int $size 修正的目标宽度
  27. * @param string $pad 用于填充补足的字符
  28. * @return type
  29. */
  30. public static function fixWidth($text, $size, $pad = ' ')
  31. {
  32. for ($i = $j = 0; $i < strlen($text) && $j < $size; $i++, $j++) {
  33. if ((ord($text[$i]) & 0xe0) === 0xe0) {
  34. if (($size - $j) == 1) {
  35. break;
  36. }
  37. $j++;
  38. $i += 2;
  39. }
  40. }
  41. return substr($text, 0, $i) . str_repeat($pad, $size - $j);
  42. }
  43. /**
  44. * 设置输出、输入编码
  45. * 默认输出的中文编码均为 UTF-8
  46. * @param string $charset 期望得到的字符集
  47. */
  48. public static function setCharset($charset)
  49. {
  50. if ($charset !== null && strcasecmp($charset, 'utf8') && strcasecmp($charset, 'utf-8')) {
  51. self::$charset = $charset;
  52. ob_start(array(__CLASS__, 'convertOut'));
  53. }
  54. }
  55. /**
  56. * 把 UTF-8 字符串转换为用户编码
  57. * @param string $buf 要转换字符串
  58. * @return string 转换后的字符串
  59. */
  60. public static function convertOut($buf)
  61. {
  62. if (self::$charset !== null) {
  63. return XS::convert($buf, self::$charset, 'UTF-8');
  64. }
  65. return $buf;
  66. }
  67. /**
  68. * 把用户输入的字符串转换为 UTF-8 编码
  69. * @param string $buf 要转换字符串
  70. * @return string 转换后的字符串
  71. */
  72. public static function convertIn($buf)
  73. {
  74. if (self::$charset !== null) {
  75. return XS::convert($buf, 'UTF-8', self::$charset);
  76. }
  77. return $buf;
  78. }
  79. /**
  80. * 解析命令行参数
  81. * @param array $valued 需要附加值的参数列表
  82. * @return array 解析完的参数数组,未指定 - 开头的选项统一放入 '-' 的子数组
  83. */
  84. public static function parseOpt($valued = array())
  85. {
  86. $result = array('-' => array());
  87. $params = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
  88. for ($i = 0; $i < count($params); $i++) {
  89. if ($params[$i] === '--') {
  90. for ($i = $i + 1; $i < count($params); $i++) {
  91. $result['-'][] = $params[$i];
  92. }
  93. break;
  94. } elseif ($params[$i][0] === '-') {
  95. $value = true;
  96. $pname = substr($params[$i], 1);
  97. if ($pname[0] === '-') {
  98. $pname = substr($pname, 1);
  99. if (($pos = strpos($pname, '=')) !== false) {
  100. $value = substr($pname, $pos + 1);
  101. $pname = substr($pname, 0, $pos);
  102. }
  103. } elseif (strlen($pname) > 1) {
  104. for ($j = 1; $j < strlen($params[$i]); $j++) {
  105. $pname = substr($params[$i], $j, 1);
  106. if (in_array($pname, $valued)) {
  107. $value = substr($params[$i], $j + 1);
  108. break;
  109. } elseif (($j + 1) != strlen($params[$i])) {
  110. $result[$pname] = true;
  111. }
  112. }
  113. }
  114. if ($value === true && in_array($pname, $valued) && isset($params[$i + 1])) {
  115. $value = $params[$i + 1];
  116. $i++;
  117. }
  118. $result[$pname] = $value;
  119. } else {
  120. $result['-'][] = $params[$i];
  121. }
  122. }
  123. self::$options = $result;
  124. self::$optind = 1;
  125. return $result;
  126. }
  127. /**
  128. * 取得命令行参数
  129. * 要求事先调用 parseOpt, 否则会自动以默认参数调用它。
  130. * @param string $short 短参数名
  131. * @param string $long 长参数名
  132. * @param bool $extra 是否补用默认顺序的参数
  133. * @return string 返回可用的参数值,若不存在则返回 null
  134. * @see parseOpt
  135. */
  136. public static function getOpt($short, $long = null, $extra = false)
  137. {
  138. if (self::$options === null) {
  139. self::parseOpt();
  140. }
  141. $value = null;
  142. $options = self::$options;
  143. if ($long !== null && isset($options[$long])) {
  144. $value = $options[$long];
  145. } elseif ($short !== null && isset($options[$short])) {
  146. $value = $options[$short];
  147. } elseif ($extra === true && isset($options['-'][self::$optind])) {
  148. $value = $options['-'][self::$optind];
  149. self::$optind++;
  150. }
  151. return $value;
  152. }
  153. /**
  154. * 刷新标准输出缓冲区
  155. */
  156. public static function flush()
  157. {
  158. flush();
  159. if (ob_get_level() > 0) {
  160. ob_flush();
  161. }
  162. }
  163. /**
  164. * 拷贝一个目录及其子目录文件
  165. */
  166. public static function copyDir($src, $dst)
  167. {
  168. if (!($dir = @dir($src)) || (!is_dir($dst) && !@mkdir($dst, 0755, true))) {
  169. return false;
  170. }
  171. while (($entry = $dir->read()) !== false) {
  172. if ($entry === '.' || $entry === '..') {
  173. continue;
  174. }
  175. $psrc = $src . DIRECTORY_SEPARATOR . $entry;
  176. $pdst = $dst . DIRECTORY_SEPARATOR . $entry;
  177. if (is_dir($pdst)) {
  178. self::copyDir($psrc, $pdst);
  179. } else {
  180. @copy($psrc, $pdst);
  181. }
  182. }
  183. $dir->close();
  184. return true;
  185. }
  186. }