PHP File buffer

Ok, so it has been said that normal posts here are “techy DIY” so I’ll give that a go. Recently I have been working on a template engine, its not just varable replacement, its more for the back end and controls the way the site is built, not just the way it looks.

There are some quirks with the PHP 4 object model that make the job a little bit ugly, mainly the missing property access methods. But to parse the template file we can basicly use a cascading object tree, passing a file buffer down a tree as its built.

The reason for using the buffer is speed, its quicker to just read the whole file in to memory, and then just pop along it. Using an object means we don’t also have to pass the index with the buffer as well as being just hand for adding short functions.

Any way, as I am still working on the main parser part, or at least the latest version there of. I’ll go with the file buffer. It a fairly straight forward object.

<?  // filebuffer.class.php

require_once('base.class.php') ;

//error codes ;
define("_FILEBUFFER_ERROR_MISSING_FILE_", -10001) ;


class baseBufferClass extends baseClass {
  var $buffer ;
  var $bufferSize ;
  var $bufferPointer ;

  function baseBufferClass() {
    parent::baseClass() ;
    $this->buffer = "" ;
    $this->bufferSize = 0 ;
    $this->bufferPointer = 0 ;
  }

  function readChar() {
    $result = false ;
    $this->bufferSize = strLen($this->buffer) ;

    if ($this->bufferSize > 0) {
      if ($this->bufferPointer < $this->bufferSize) {
        $result = $this->buffer[$this->bufferPointer] ;
        $this->bufferPointer = $this->bufferPointer + 1 ;
      }
    }

    return $result ;
  }

}

class fileBuffer extends baseBufferClass {
  var $fileName ;
  var $fileOpen ;
  var $error ;
  var $fileHandle ;

  function fileBuffer($fileName="") {
    parent::baseBufferClass() ;
    $this->fileOpen = false ;
    $this->fileName = "" ;
    $this->fileBuffer = "" ;
    $this->bufferSize = 0 ;
    $this->filePointer = 0 ;

    if (strLen($fileName) >0) {
      $this->fileName = $fileName ;
      $this->openFile() ;
    }
  }

  function openFile($fileName="") {
    if ($this->fileOpen) {
      $this->closeFile() ;
    }

    if (strLen($fileName) >0) {
      $this->fileName = $fileName ;
    }

    if (file_Exists($this->fileName)) {
      $this->buffer = file_get_contents($this->fileName) ;
      $this->bufferSize = strLen($this->buffer) ;
      $this->fileOpen = true ;
    }
    else {
      $this->error = _FILEBUFFER_ERROR_MISSING_FILE_ ;
      $result = false ;
    }
  }

  function closeFile() {
    $this->fileBuffer = "" ;
    $this->bufferSize = 0 ;
    $this->filePointer = 0 ;

    $this->fileOpen = false ;
  }

}

?>

The base class in this case doesn’t do any thing, I just use it as good style.

BaseBufferClass is used as a basic buffer, this can then be passed around as a slightly lighter buffer in the parser objects. It maintains the storage and pointer.

The file buffer class then extends the baseBufferClass to add file access. This is mainly done by the use of the constructor and openFile methods. When a file name is passed in the constructor openFile is then called to load the data from the file. If the file name is not passed, then openFile can be called directly.

Its not the worlds most complicated bit of code, but it fairly handy.

Comments

comments

Subscribe

You can subscribe via e-mail to get my posts in your Inbox, or stalk me on numerous other platforms.

Follow my Dayre!

, ,


3 Responses to PHP File buffer

  1. PCGeek January 27, 2006 at 8:35 pm #

    APPLE ][ CODE

    10 CLS
    20 ? “HI I AM THE HAL9000 COMPUTER. NO I AM NOT AN IBM”
    30 PRINT CHR$(42)
    40 GOTO 20
    50 END

  2. Haydies January 27, 2006 at 11:56 pm #

    ewwww. a goto… any one that uses a go to by choice needs to have their balls removed with a blunt spoon or simular for people with out balls.

  3. marco September 8, 2010 at 10:10 pm #

    how to use this script? Do you make direct call or do you create new instance to read a file?