Code cleanup and fixed bug in Regex

This commit is contained in:
DHTMLGoodies
2013-02-07 17:21:39 +01:00
parent 5e0f62c7b6
commit 929c109bb9

View File

@@ -19,16 +19,10 @@ class PgnParser {
$this->pgnContent = $content; $this->pgnContent = $content;
} }
private function readPgn(){
$fh = fopen($this->pgnFile, 'r');
$this->pgnContent = fread($fh, filesize($this->pgnFile));
fclose($fh);
}
private function cleanPgn(){ private function cleanPgn(){
$c = $this->pgnContent; $c = $this->pgnContent;
$c = preg_replace("/\\$[0-9]+/s", "", $c); $c = preg_replace("/\\$[0-9]+/s", "", $c);
$c = preg_replace("/{(.*?)\[(.*?)}/s", '{$1-SB-$2}', $c); $c = preg_replace("/{([^\[]*?)\[([^}]?)}/s", '{$1-SB-$2}', $c);
$c = preg_replace("/\r/s", "", $c); $c = preg_replace("/\r/s", "", $c);
$c = preg_replace("/\t/s", "", $c); $c = preg_replace("/\t/s", "", $c);
$c = preg_replace("/\]\s+\[/s", "]\n[", $c); $c = preg_replace("/\]\s+\[/s", "]\n[", $c);
@@ -65,11 +59,13 @@ class PgnParser {
} }
public function getUnparsedGames() { public function getUnparsedGames() {
if($this->pgnFile){ if(!isset($this->pgnGames)){
$this->readPgn(); if($this->pgnFile && !isset($this->pgnContent)){
$this->pgnContent = file_get_contents($this->pgnFile);
}
$this->cleanPgn();
$this->splitPgnIntoGames();
} }
$this->cleanPgn();
$this->splitPgnIntoGames();
return $this->pgnGames; return $this->pgnGames;
} }
@@ -82,17 +78,13 @@ class PgnParser {
} }
public function getGames(){ public function getGames(){
if($this->pgnFile){
$this->readPgn();
}
$this->cleanPgn();
$this->splitPgnIntoGames();
for($i=0, $count = count($this->pgnGames);$i<$count; $i++){ $games = $this->getUnparsedGames();
$gameParser = new PgnGameParser($this->pgnGames[$i]); $this->games = array();
for($i=0, $count = count($games);$i<$count; $i++){
$gameParser = new PgnGameParser($games[$i]);
$this->games[$i] = $gameParser->getParsedData(); $this->games[$i] = $gameParser->getParsedData();
if(!$this->isLazy()){ if(!$this->isLazy()){
$this->games[$i] = $this->gameParser->getParsedGame($this->games[$i]); $this->games[$i] = $this->gameParser->getParsedGame($this->games[$i]);
} }