file_reader.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Read text from a file. Reader is line oriented and not char oriented.
  4. * The default converter converts from the file encoding - auto-detected - to
  5. * system encoding.
  6. *
  7. * Usage:
  8. *
  9. * $file = FileReader::create('path');
  10. * foreach($file as $line)
  11. * {
  12. * ...
  13. * }
  14. *
  15. * @copyright (c) 2012 University of Geneva
  16. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  17. * @author Laurent Opprecht <laurent@opprecht.info>
  18. */
  19. class FileReader implements Iterator
  20. {
  21. const EOL = "\n";
  22. /**
  23. *
  24. * @param string $path
  25. * @return FileReader
  26. */
  27. static function create($path, $converter = null)
  28. {
  29. return new self($path, $converter);
  30. }
  31. /**
  32. * Returns the file encoding
  33. *
  34. * @return Encoding
  35. */
  36. static function detect_encoding($path)
  37. {
  38. $abstract = array();
  39. // We assume that 200 lines are enough for encoding detection.
  40. // here we must get at the raw data so we don't use other functions
  41. // it's not possible to read x chars as this would not be safe with utf
  42. // (chars may be split in the middle)
  43. $handle = fopen($path, 'r');
  44. $i = 0;
  45. while (($line = fgets($handle)) !== false && $i < 200) {
  46. $i++;
  47. $abstract[] = $line;
  48. }
  49. fclose($handle);
  50. $abstract = implode($abstract);
  51. return Encoding::detect_encoding($abstract);
  52. }
  53. protected $path = '';
  54. protected $handle = null;
  55. protected $current = false;
  56. protected $index = -1;
  57. protected $converter = null;
  58. function __construct($path, $converter = null)
  59. {
  60. if (empty($converter)) {
  61. $encoding = self::detect_encoding($path);
  62. $converter = $encoding->decoder();
  63. }
  64. $this->path = $path;
  65. $this->converter = $converter;
  66. }
  67. /**
  68. *
  69. * @return Converter
  70. */
  71. function get_converter()
  72. {
  73. return $this->converter;
  74. }
  75. function handle()
  76. {
  77. if (is_null($this->handle)) {
  78. $this->handle = fopen($this->path, 'r');
  79. }
  80. return $this->handle;
  81. }
  82. /**
  83. * Read at most $count lines.
  84. *
  85. * @param int $count
  86. * @return array
  87. */
  88. function read_lines($count)
  89. {
  90. $result;
  91. $i = 0;
  92. foreach ($this as $line) {
  93. if ($i >= $count) {
  94. return $result;
  95. }
  96. $i++;
  97. $result[] = $line;
  98. }
  99. return $result;
  100. }
  101. function read_line()
  102. {
  103. return $this->next();
  104. }
  105. function close()
  106. {
  107. if (is_resource($this->handle)) {
  108. fclose($this->handle);
  109. }
  110. $this->handle = null;
  111. }
  112. protected function convert($text)
  113. {
  114. return $this->converter->convert($text);
  115. }
  116. public function current()
  117. {
  118. return $this->current;
  119. }
  120. public function key()
  121. {
  122. return $this->index;
  123. }
  124. public function next()
  125. {
  126. $handle = $this->handle();
  127. if($handle === false)
  128. {
  129. $this->current = false;
  130. return false;
  131. }
  132. $line = fgets($handle);
  133. if ($line !== false) {
  134. $line = rtrim($line, "\r\n");
  135. $line = $this->convert($line);
  136. $this->index++;
  137. }
  138. $this->current = $line;
  139. return $this->current;
  140. }
  141. public function rewind()
  142. {
  143. $this->converter->reset();
  144. if ($handle = $this->handle()) {
  145. rewind($handle);
  146. }
  147. $this->current = false;
  148. $this->index = -1;
  149. $this->next();
  150. }
  151. public function valid()
  152. {
  153. return $this->current !== false;
  154. }
  155. function __clone()
  156. {
  157. $this->handle = null;
  158. $this->current = false;
  159. $this->index = -1;
  160. $this->converter->reset();
  161. }
  162. }