From fb9f2dd4a49f87982ab22cd0f43dfc501f2705d2 Mon Sep 17 00:00:00 2001 From: haneul Date: Wed, 7 Oct 2009 17:08:47 +0000 Subject: [PATCH] #18353187 : add file object class git-svn-id: http://xe-core.googlecode.com/svn/sandbox@6825 201d5d3c-b55e-5fd7-737f-ddc643e51545 --- classes/file/FileHandler.class.php | 20 ++++- classes/file/FileObject.class.php | 128 +++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 classes/file/FileObject.class.php diff --git a/classes/file/FileHandler.class.php b/classes/file/FileHandler.class.php index 8f050bd95..da3808613 100644 --- a/classes/file/FileHandler.class.php +++ b/classes/file/FileHandler.class.php @@ -557,7 +557,7 @@ * @brief write array into ini file * @param[in] $filename target ini file name * @param[in] $arr array - * return if array contains nothing it returns false, otherwise true + * @return if array contains nothing it returns false, otherwise true **/ function writeIniFile($filename, $arr){ if(count($arr)==0) return false; @@ -581,5 +581,23 @@ } return $return; } + + /** + * @brief return file object + * @param[in] $file_name target file name + * @param[in] $mode file mode for fopen + * @remarks if the directory of the file does not exist, create it. + * @return file object + **/ + function openFile($file_name, $mode) + { + $pathinfo = pathinfo($file_name); + $path = $pathinfo['dirname']; + if(!is_dir($path)) FileHandler::makeDir($path); + + require_once("FileObject.class.php"); + $file_object = new FileObject($file_name, $mode); + return $file_object; + } } ?> diff --git a/classes/file/FileObject.class.php b/classes/file/FileObject.class.php new file mode 100644 index 000000000..a35c90bbe --- /dev/null +++ b/classes/file/FileObject.class.php @@ -0,0 +1,128 @@ +Open($path, $mode); + } + + /** + * @brief append target file's content to current file + * @param[in] $file_name path of target file + * @return none + **/ + function append($file_name) + { + $target = new FileObject($file_name, "r"); + while(!$target->feof()) + { + $readstr = $target->read(); + $this->write($readstr); + } + $target->close(); + } + + /** + * @brief check current file meets eof + * @return true: if eof. false: otherwise + **/ + function feof() + { + return feof($this->fp); + } + + /** + * @brief read from current file + * @param[in] $size size to read + * @return read bytes + **/ + function read($size = 1024) + { + return fread($this->fp, $size); + } + + + /** + * @brief write string to current file + * @param[in] $str string to write + * @return written bytes. if failed, it returns false + **/ + function write($str) + { + $len = strlen($str); + if(!$str || $len <= 0) return false; + if(!$this->fp) return false; + $written = fwrite($this->fp, $str); + return $written; + } + + /** + * @brief open a file + * @param[in] $path path of target file + * @param[in] $mode file open mode + * @remarks if file is opened, close it and open the new path + * @return true if succeed, false otherwise. + */ + function open($path, $mode) + { + if($this->fp != null) + { + $this->close(); + } + $this->fp = fopen($path, $mode); + if(! is_resource($this->fp) ) + { + $this->fp = null; + return false; + } + $this->path = $path; + return true; + } + + /** + * @brief return current file's path + * @return file path + **/ + function getPath() + { + if($this->fp != null) + { + return $this->path; + } + else + { + return null; + } + } + + /** + * @brief close file + * @return none + **/ + function close() + { + if($this->fp != null) + { + fclose($this->fp); + $this->fp = null; + } + } + + } +?>