<?php
/*
* ---------------------------------------------------
* @author raenkrus nadir
* @version 0.4.5
*
* tested with PHP 5.2.6 (cli)
* ---------------------------------------------------
*
* Copyright (c) 2009 raenkrus nadir - hide@address.com
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated
* documentation files (the "Software"),
* to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
class CheckCurlyBraces {
public $theFile;
public $TotOpen;
public $TotClose;
private $LineLength;
private $subLineLength;
private $fileP;
private $lin;
private $parz;
private $numLine;
private $o;
private $c;
public function CheckCurlyBraces() {
$this->o = chr(123);
$this->c = chr(125);
$this->theFile = file_exists($_SERVER['argv'][1]) ? realpath($_SERVER['argv'][1]) : FALSE;
if (FALSE === $this->theFile) {
$this->Errors("Usage:\n\t$ php ".$_SERVER['argv'][0]." filename");
}
if (FALSE === ($this->fileP = file($this->theFile))) {
$this->Errors("Cannot open file ".$this->theFile);
}
$this->TotOpen = 0;
$this->TotClose = 0;
}
public function CountBraces() {
foreach($this->fileP as $k=>$v) {
$this->LineLength = strlen($v);
for ($i=0;$i<$this->LineLength;$i++) {
if (FALSE !== strpos($v{$i},$this->o)) {
$this->TotOpen++;
}
if (FALSE !== strpos($v{$i},$this->c)) {
$this->TotClose++;
}
}
}
reset($this->fileP);
echo "\n\tFile: ".$this->theFile."\n\tTotals: { ".$this->TotOpen." - } ".$this->TotClose."\n\n";
}
private function PrepareLine($line) {
// we assume a "tab" character as 4 "space" characters
$tmpL = str_replace(chr(9), chr(32).chr(32).chr(32).chr(32), $line, $matches);
//if ($matches) $this->WriteLog("replaced tabs: ".$matches);
$tmpL = rtrim($tmpL, " \r\n\t");
return $tmpL;
}
public function SeekOpenClose () {
foreach ($this->fileP as $k=>$v) {
$this->lin = $this->PrepareLine($v);
$this->LineLength = strlen($this->lin);
for ($i=0;$i<$this->LineLength;$i++) {
if (FALSE !== strpos($this->lin{$i},$this->o)) {
$this->parz = 0;
echo "Opening : L ".($k+1).", C ".($i+1);
$this->numLine = $k;
while ($this->fileP[$this->numLine]) {
$this->fileP[$this->numLine] = (!$w) ? $this->fileP[$this->numLine] : $this->PrepareLine($this->fileP[$this->numLine]);
$this->subLineLength = strlen($this->fileP[$this->numLine]);
$ch_start = (!$w)?$i:0;
for ($j=$ch_start;$j<$this->subLineLength;$j++) {
$w=true;
if (FALSE !== strpos($this->fileP[$this->numLine]{$j},$this->o)) {
$this->parz++;
}
if (FALSE !== strpos($this->fileP[$this->numLine]{$j},$this->c)) {
$this->parz--;
if ($this->parz==0) {
break;
}
}
} // end for
if ($this->parz==0) {
echo " - Closing : L ".($this->numLine+1).", C ".($j+1)."\n";
//$cl = false;
break 1;
}
$this->numLine++;
} // end while
$w=false;
} // end if
} // end for
} // end foreach
}
private function Errors($mess) {
die("\n\t".$mess."\n\n");
}
private function Warnings($warn) {
echo "\n\t[WARNING] ".$warn."\n";
}
public function GenTime() {
static $a;
if ($a == 0) {
$a = microtime(true);
return true;
} else {
$diff = number_format((microtime(true)-$a), 3);
$timeEnd = (string) $diff;
return "\n\n\tDone in ".$timeEnd." seconds\n\n";
}
}
private function WriteLog($Log) {
$fp = fopen('ccb.log','a');
$data = date('Ymd His|');
fwrite($fp,$data.$Log."\n");
fclose($fp);
}
} // end class
$Obj = new CheckCurlyBraces();
$Obj->gentime();
$Obj->CountBraces();
$Obj->SeekOpenClose();
$te = $Obj->GenTime();
echo $te;
?>