- 论坛徽章:
- 0
|
XEAM有望在PHP编程领域填补一项国内空白
贴一个XEAM处理文件流的子类的代码,自己看吧
文件:xmTextStream.php
作者:Bighan
更新:2003-12-31
- <?php
- require_once (dirname(__FILE__). "/xmFsoCommon.php");
- class xmTextStream extends xmFsoCommon
- {
- var $AtEndOfLine = false;
- var $AtEndOfStream = false;
- var $Column = fsoPosUnknown;
- var $Line = fsoPosUnknown;
- var $Pointer = fsoPosUnknown;
- var $EOF = fsoPosUnknown;
- var $EOL = fsoPosUnknown;
- var $Source = "";
- var $IsCreate = true;
- var $IsOverWrite = false;
- var $OpenMode = fsoForreading;
- var $_handler = false;
- var $_IsOpen = false;
- var $_IsLock = false;
- var $Unicode = false;
- function __construct($filename = "", $mode = "rb")
- {
- parent::__construct();
- if (!empty($filename) && !empty($mode)) {
- $this->;OpenFile($filename, $mode);
- }
- }
- function __destruct()
- {
- //$this->;Close();
- }
- function OpenFile($filename = "", $mode = "")
- {
- if ($this->;_IsOpen) return;
- if (!empty($filename)) $this->;Source = $filename;
- if (!empty($mode)) $this->;OpenMode = $mode;
- if ($this->;_handler = &$this->;_doOpenFile()) {
- $this->;Pointer = 0;
- $this->;Line = 0;
- $this->;Column = 0;
- $this->;_CheckEOF();
- $this->;_IsOpen = true;
- } else {
- $this->;Source = "";
- $this->;OpenMode = fsoForreading;
- }
- return $this->;_IsOpen;
- }
- function &_doOpenFile()
- {
- if (!$this->;Source || !$this->;OpenMode) return false;
- if ($this->;_IsRemote($this->;Source)) {
- if (!ini_get('allow_url_fopen')) ini_set('allow_url_fopen', 1);
- }
- $fp = false;
- $this->;_doOpenMode();
- if ($this->;_doCheckFile()) {
- if ($this->;OpenMode != 'wb' || $this->;OpenMode != 'wb+') {
- $fp = fopen($this->;Source, $this->;OpenMode);
- } else if ($this->;IsOverWrite) {
- $fp = fopen($this->;Source, $this->;OpenMode);
- }
- } elseif (($this->;OpenMode != 'rb' || $this->;OpenMode != 'rb+') && $this->;IsCreate) {
- $fp = fopen($this->;Source, $this->;OpenMode);
- }
- return is_resource($fp) ? $fp : false;
- }
- function _doOpenMode()
- {
- $this->;OpenMode = strtolower(trim($this->;OpenMode));
- $this->;OpenMode = preg_replace("/^([r|w|a|x])+\s*(b)?\s*(\+)?/i", "\\1b\\3", $this->;OpenMode);
- }
- function _doCheckFile()
- {
- //$this->;doClearstatcache();
- $mode = preg_replace("/^([r|w|a|x])+\s*(b)?\s*(\+)?/i", "\\1\\2", $this->;OpenMode);
- if ($mode == fsoForreading && !$this->;_IsRemote($this->;Source) && !file_exists($this->;Source)) {
- return false;
- } elseif (($mode == fsoForwritting || $mode == fsoForappending)
- && !file_exists($this->;Source)
- && !is_writable(dirname($this->;Source))) {
- return false;
- } elseif (($mode == fsoForwritting || $mode == fsoForappending)
- && !is_writable(dirname($this->;Source))) {
- return false;
- }
- return true;
- }
- function _Lock($operation)
- {
- if (!$this->;_IsOpen) return false;
- return flock($this->;_handler, $operation+LOCK_NB, true);
- }
- function shaLock()
- {
- $this->;_IsLock = true;
- return $this->;_Lock(fsoLockShared);
- }
- function excLock()
- {
- $this->;_IsLock = true;
- return $this->;_Lock(fsoLockExclusive);
- }
- function unLock()
- {
- if ($this->;_IsLock) {
- $this->;_IsLock = false;
- return $this->;_Lock(fsoLockRelease);
- }
- }
- function Close()
- {
- if (!$this->;_IsOpen) return;
- if ($this->;_IsLock) $this->;unLock();
- if (is_resource($this->;_handler)) fclose($this->;_handler);
- $this->;_IsOpen = false;
- }
- function _CheckMode($state)
- {
- if (ereg('\+', $this->;OpenMode)) return true;
- $result = false;
- switch ($state) {
- case 'r':
- if ($this->;OpenMode == fsoForreading) $result = true;
- break;
- case 'w':
- if ($this->;OpenMode == fsoForwritting) $result = true;
- break;
- case 'a':
- if ($this->;OpenMode == fsoForappending) $result = true;
- break;
- }
- return $result;
- }
- function _CheckEOF()
- {
- $this->;EOF = feof($this->;_handler);
- $this->;AtEndOfStream =& $this->;EOF;
- return $this->;EOF;
- }
- function _CheckEOL($char)
- {
- if ($char === "\n" || $char === "\r\n") {
- $this->;EOL = true;
- $this->;Line++;
- $this->;Column = fsoPosEOF;
- } else {
- $this->;EOL = false;
- if ($this->;Column == fsoPosEOF || $this->;Column == fsoPosBOF)
- {
- $this->;Column = 0;
- }
- if (ord($char) >; 0x2f) $this->;Column++;
- }
- $this->;AtEndOfLine =& $this->;EOL;
- return $this->;EOL;
- }
- function _CheckPointer()
- {
- $this->;Pointer = ftell($this->;_handler);
- return $this->;Pointer;
- }
- function Read($characters)
- {
- $fileChar = '';
- if ($characters == 0) return $this->;ReadAll();
- for ($i = 0; $i < $characters; $i++) {
- $fileChar .= $this->;ReadChar();
- if($this->;EOF) break;
- }
- return !empty($fileChar) ? $fileChar : false;
- }
- function ReadChar()
- {
- if (!$this->;_IsOpen || !$this->;_CheckMode('r') || $this->;EOF) return false;
- $char = '';
- if ($char = fgetc($this->;_handler)) {
- if ($this->;Unicode) {
- if (ord($char) >; 0x7f) $char .= fgetc($this->;_handler);
- }
- }
- $this->;_CheckPointer();
- $this->;_CheckEOF();
- $this->;_CheckEOL($char);
- return strlen($char) ? $char : false;
- }
- function ReadLine()
- {
- if (!$this->;_IsOpen || !$this->;_CheckMode('r')) return false;
- $fileString = '';
- while (($fileChar = $this->;ReadChar()) != "\n" && !$this->;EOF) {
- $fileString .= $fileChar;
- }
- return substr($fileString, -1) == "\r" ? substr($fileString, 0, -1) : $fileString;
- }
- function ReadAll()
- {
- $file = '';
- while (!$this->;_CheckEOF()) {
- $file .= fread($this->;_handler, fsoReadDefaultSize);
- }
- $this->;Line = fsoPosEOF;
- $this->;Column = fsoPosEOF;
- $this->;_CheckPointer();
- return $file;
- }
- function Skip($characters)
- {
- $this->;Move($characters);
- }
- function SkipLine()
- {
- $this->;ReadLine();
- }
- function Write($string, $length = false)
- {
- if ($this->;_IsOpen && ($this->;_CheckMode('w') || $this->;_CheckMode('a'))) {
- if (0 >; $length) return false;
- if (!$length) $length = strlen($string);
- if ($bytes = fwrite($this->;_handler, $string, $length)) {
- return $bytes;
- } else {
- return false;
- }
- }
- return false;
- }
- function WriteChar($char)
- {
- if ($this->;_IsOpen && ($this->;_CheckMode('w') || $this->;_CheckMode('a'))) {
- if ($this->;Unicode) {
- $length = (ord($char) < 0x7f) ? 1 : 2;
- }
- return $this->;Write($char, $length);
- }
- return false;
- }
- function WriteLine($line)
- {
- $line .= $this->;_LineSeparator;
- return $this->;Write($line);
- }
- /*
- function BufferToStream()
- {
- return fflush($this->;_handler);
- }
- function StreamToBuffer()
- {
- return fpassthru($this->;_handler);
- }
- */
- function WriteBlankLines($lines)
- {
- }
- function MoveFirst()
- {
- if (!$this->;_IsOpen || $this->;Pointer == fsoPosBOF) return false;
- if(rewind($this->;_handler)) {
- $this->;Line = 0;
- $this->;Pointer = 0;
- }
- }
- function MoveLast()
- {
- return $this->;Move(-1, SEEK_END);
- }
- function Move($offset, $whence = SEEK_SET)
- {
- if (!$this->;_IsOpen || $offset == 0) return false;
- if (($result = fseek($this->;_handler, $offset, $whence)) != -1) {
- /*
- if ($this->;Unicode) {
- if (ord(fgetc($this->;_handler)) >; 0x7f) {
- if ($offset < 0 ) {
- fseek($this->;_handler, -2, $whence);
- }
- } else {
- fseek($this->;_handler, -1, $whence);
- }
- }
- */
- $this->;AtEndOfStream = feof($this->;_handler);
- $this->;Pointer = ftell($this->;_handler);
- }
- return ($result === 0) ? true: false;
- }
- function MoveNext()
- {
- if (!$this->;_IsOpen && $this->;AtEndOfStream) return false;
- return $this->;Move(1, SEEK_CUR);
- }
- function MovePrevious()
- {
- if (!$this->;_IsOpen && $this->;AtEndOfStream) return false;
- return $this->;Move(-1, SEEK_CUR);
- }
- }
- ?>;
复制代码 |
|