PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/kerphi/contrib/pfcInstaller2/File/Archive/Reader/ChangeName.php

https://bitbucket.org/shashwat_dinasource/bitscentral
PHP | 212 lines | 124 code | 13 blank | 75 comment | 23 complexity | 7d3e20216b90964904ab664d122fe307 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Add a directory to the public name of all the files of a reader
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA
  21. *
  22. * @category File Formats
  23. * @package File_Archive
  24. * @author Vincent Lascaux <vincentlascaux@php.net>
  25. * @copyright 1997-2005 The PHP Group
  26. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  27. * @version CVS: $Id: ChangeName.php,v 1.19 2005/07/09 12:54:35 vincentlascaux Exp $
  28. * @link http://pear.php.net/package/File_Archive
  29. */
  30. require_once "File/Archive/Reader/Relay.php";
  31. /**
  32. * Add a directory to the public name of all the files of a reader
  33. *
  34. * Example:
  35. * If archive.tar is a file archive containing files a.txt and foo/b.txt
  36. * new File_Archive_Reader_AddBaseName('bar',
  37. * new File_Archive_Reader_Tar(
  38. * new File_Archive_Reader_File('archive.tar')
  39. * )
  40. * ) is a reader containing files bar/a.txt and bar/foo/b.txt
  41. */
  42. class File_Archive_Reader_AddBaseName extends File_Archive_Reader_Relay
  43. {
  44. var $baseName;
  45. function File_Archive_Reader_AddBaseName($baseName, &$source)
  46. {
  47. parent::File_Archive_Reader_Relay($source);
  48. $this->baseName = $this->getStandardURL($baseName);
  49. }
  50. /**
  51. * Modify the name by adding baseName to it
  52. */
  53. function modifyName($name)
  54. {
  55. return $this->baseName.
  56. (empty($this->baseName) || empty($name) ? '': '/').
  57. $name;
  58. }
  59. /**
  60. * Remove baseName from the name
  61. * Return false if the name doesn't start with baseName
  62. */
  63. function unmodifyName($name)
  64. {
  65. if (strncmp($name, $this->baseName.'/', strlen($this->baseName)+1) == 0) {
  66. $res = substr($name, strlen($this->baseName)+1);
  67. if ($res === false) {
  68. return '';
  69. } else {
  70. return $res;
  71. }
  72. } else if (empty($this->baseName)) {
  73. return $name;
  74. } else if ($name == $this->baseName) {
  75. return '';
  76. } else {
  77. return false;
  78. }
  79. }
  80. /**
  81. * @see File_Archive_Reader::getFilename()
  82. */
  83. function getFilename()
  84. {
  85. return $this->modifyName(parent::getFilename());
  86. }
  87. /**
  88. * @see File_Archive_Reader::getFileList()
  89. */
  90. function getFileList()
  91. {
  92. $list = parent::getFileList();
  93. $result = array();
  94. foreach ($list as $name) {
  95. $result[] = $this->modifyName($name);
  96. }
  97. return $result;
  98. }
  99. /**
  100. * @see File_Archive_Reader::select()
  101. */
  102. function select($filename, $close = true)
  103. {
  104. $name = $this->unmodifyName($filename);
  105. if ($name === false) {
  106. return false;
  107. } else {
  108. return $this->source->select($name, $close);
  109. }
  110. }
  111. }
  112. /**
  113. * Change a directory name to another
  114. *
  115. * Example:
  116. * If archive.tar is a file archive containing files a.txt and foo/b.txt
  117. * new File_Archive_Reader_ChangeBaseName('foo', 'bar'
  118. * new File_Archive_Reader_Tar(
  119. * new File_Archive_Reader_File('archive.tar')
  120. * )
  121. * ) is a reader containing files a.txt and bar/b.txt
  122. */
  123. class File_Archive_Reader_ChangeBaseName extends File_Archive_Reader_Relay
  124. {
  125. var $oldBaseName;
  126. var $newBaseName;
  127. function File_Archive_Reader_ChangeBaseName
  128. ($oldBaseName, $newBaseName, &$source)
  129. {
  130. parent::File_Archive_Reader_Relay($source);
  131. $this->oldBaseName = $this->getStandardURL($oldBaseName);
  132. if (substr($this->oldBaseName, -1) == '/') {
  133. $this->oldBaseName = substr($this->oldBaseName, 0, -1);
  134. }
  135. $this->newBaseName = $this->getStandardURL($newBaseName);
  136. if (substr($this->newBaseName, -1) == '/') {
  137. $this->newBaseName = substr($this->newBaseName, 0, -1);
  138. }
  139. }
  140. function modifyName($name)
  141. {
  142. if (empty($this->oldBaseName) ||
  143. !strncmp($name, $this->oldBaseName.'/', strlen($this->oldBaseName)+1) ||
  144. strcmp($name, $this->oldBaseName) == 0) {
  145. return $this->newBaseName.
  146. (
  147. empty($this->newBaseName) ||
  148. strlen($name)<=strlen($this->oldBaseName)+1 ?
  149. '' : '/'
  150. ).
  151. substr($name, strlen($this->oldBaseName)+1);
  152. } else {
  153. return $name;
  154. }
  155. }
  156. function unmodifyName($name)
  157. {
  158. if (empty($this->newBaseName) ||
  159. !strncmp($name, $this->newBaseName.'/', strlen($this->newBaseName)+1) ||
  160. strcmp($name, $this->newBaseName) == 0) {
  161. return $this->oldBaseName.
  162. (
  163. empty($this->oldBaseName) ||
  164. strlen($name)<=strlen($this->newBaseName)+1 ?
  165. '' : '/'
  166. ).
  167. substr($name, strlen($this->newBaseName)+1);
  168. } else {
  169. return $name;
  170. }
  171. }
  172. /**
  173. * @see File_Archive_Reader::getFilename()
  174. */
  175. function getFilename()
  176. {
  177. return $this->modifyName(parent::getFilename());
  178. }
  179. /**
  180. * @see File_Archive_Reader::getFileList()
  181. */
  182. function getFileList()
  183. {
  184. $list = parent::getFileList();
  185. $result = array();
  186. foreach ($list as $name) {
  187. $result[] = $this->modifyName($name);
  188. }
  189. return $result;
  190. }
  191. /**
  192. * @see File_Archive_Reader::select()
  193. */
  194. function select($filename, $close = true)
  195. {
  196. return $this->source->select($this->unmodifyName($filename));
  197. }
  198. }
  199. ?>