.: ShaolinTiger – Kung-Fu Geekery :.

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.

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Ping.fm Post to StumbleUpon

  

Tags: , ,

Follow @ShaolinTiger on Twitter Follow me on Twitter Follow @ShaolinTiger on Twitter Subscribe to RSS Subscribe to RSS Subscribe to RSS

Posted in Internet & Tech | 2 comments -

Related Posts:
- A small change to WordPress to make WHOIS International
- Torrent Sites Under Attack by MPAA
- Picture Published In The Star Today – Without Credit
- Checking out Hypp.TV – Local Video Content Finally!
- Natural Disaster Sweeps Across Asia
- I’ve Sold My First Print – Come to Me – Black and White

2 Comments so far

  1. PCGeek  on January 27th, 2006 at 8:35 pm
    Using Mozilla Firefox Mozilla Firefox 1.0.6 on Windows Windows XP 

    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  on January 27th, 2006 at 11:56 pm
    Using Mozilla Firefox Mozilla Firefox 1.5 on Windows Windows 2000 

    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.

Leave a reply


Stalk Me

  • Follow Me on Twitter!
  • Users Online

    Sponsor

    Recent Comments

    Top Commentators

    Topics

    Blog Stuff


  • Other Links

    Powered By

  • Linux
  • Apache
  • PHP
  • MySQL
  • Get Firefox!
  • WordPress is a state-of-the-art semantic personal publishing platform
  • FileZilla
  • Free Source Code Editor!
  • Photoshop CS2