Wednesday, May 14, 2008

CLASS101- Artikel 3


Ini adalah artikel terakhir dari serangkaian class101. Artikel ini akan membahasa teknik yg lebih lanjut antara lain: menempatkan code class di file yg terpisah dari code main... Dan jangan kaget karena artikel ini juga lsg memperlihatkan bagaimana "class-in-action", utk aplikasi yg saya lampirkan adalah project saya... searchEngine :D, project "search4Gold" walau masih primitif.

Aplikasi yg dibahas akan dimulain dari tahap primitif sampai dengan penyempurnaan dan disini akan terlihat bgaimana class membantu dlm pengambangan project. Jika ada masukan silahkan ato mungkin code di copy dan dikembang saya lebih senang... berikan feedback elo kalo perlu lo bisa kasih hasil pengembangan elo dan diposting disini. Langsung aje...

Buat dolo file class nya... Inget utk mempermudah taruh ke-2 file pada directory yg sama kecuali anda sudah mahir dlm pemberian "../../../namaFileMu"

file: classSearchEngine.php
/**
* A primitive search engine
* it searches for needle from a string by comparing char by char
*
* @author Adesanto Asman
* @version 1.0
* @license freeware
* @package search4Gold
*/

/**
* Variables of searchEngine
* @package varsSearchEngine
*/
class varsSearchEngine{

/**
* @var boolean TRUE = Case sensitive, FALSE = no case :D
*/
var $case;

/**
* @var string string we tried to search for
*/
var $needle;

/**
* @var integer how many file?
*/
var $nFiles;

/**
* @var integer search results
*/
var $schFound;

/**
* @var array filenames
*/
var $filename = array();

/**
* @var array the results are store here
*/
var $schResult = array("FILE_NAME" => array(),
"FILE_LINE" => array(),
"FILE_STRING" => array());

/**
* @var boolean flag of class
*/
var $status;
}

/**
* Methods of searchEngine
* @package mthSeacrhEngine
* @see varsSearchEngine
*/
class mthSearchEngine extends varsSearchEngine{

/**
* it searches char-by-char
* @see $needle, $case
* @param string $needle the string we tried to search
* @param string $string the file string we search from
* @param boolean $case is it case sensitive?
* @return boolean
*/
function searchEngine($needle, $string, $case){

$maxLenNeedle = strlen($needle); // Get needle length. It is important to ease the search
$maxLenString = strlen($string); // Get String length. It is important to set the max search loop
$match = FALSE;
if($case == 0){ //not case sensitive

$needle = strtolower($needle);
$string = strtolower($string);
}
for($i = 0; $i < $maxLenString; $i++){ if($needle[0] == $string[$i]){ //if the first char of needle match with the string then loop as much as the needle' length if(($maxLenString - $i) >= $maxLenNeedle){
//before we continue check if the remainded chars not lower than
//the needle length

$match = TRUE;
for($ix = 1; $ix < $maxLenNeedle; $ix++){ if($needle[$ix] != $string[($i+$ix)]){ $match = FALSE; } } if($match == TRUE){ //echo "We found the string you looking for ".$needle." --- ".$string." //\n"; return TRUE; } }else{ //echo "No Luck!\n"; return FALSE; } } } //echo "No Luck!\n"; return FALSE; } /** * init the filename, keyword, search string and how many files from... * @see $filename, $needle, $case, $nFiles * @param string $filename * @param string $needle * @param string $string * @param boolean $case * @param integer $nFiles */ function initSearchEngine( $filename, $needle, $case, $nFiles){ for($i = 0; $i < $nFiles; $i++){ $this->filename[$i] = $filename[$i];
}
$this->nFiles = $nFiles;
$this->needle = $needle;
$this->case = $case;
}

/**
* start the search
* it copies all fetched info into array $schResult and integer $schFound
* @see $schResult, $schFound
* @param void
*/
function searchNow(){

$schFound = 0;
for($i = 0; $i < $this->nFiles; $i++){
//foreach file name do search string perline of file

$lnFile = 1;
$fh = fopen($this->filename[$i],'r') or
die("can't open ".$this->filename[$i]." : ".$php_errormsg." ");
while($strFile = fgets($fh)) {

if(mthSeacrhEngine::searchEngine($this->needle, $strFile, $this->case) == TRUE){
//if we found one then copy the info to schResult

$this->schResult["FILE_NAME"][$schFound] = $this->filename[$i]; //copy file name
$this->schResult["FILE_LINE"][$schFound] = $lnFile; //copy file line
$this->schResult["FILE_STRING"][$schFound] = $strFile; //copy file string
$schFound++;
}
$lnFile++;
}
}
$this->schFound = $schFound;
}

/**
* Print the results
* @param void
*/
function searchPrint(){

for($i = 0; $i < $this->schFound; $i++){

echo "We found needle: \"".$this->needle."\" at ".
"(".$this->schResult["FILE_NAME"][$i]." : ".
$this->schResult["FILE_LINE"][$i]."
) ".
$this->schResult["FILE_STRING"][$i]." \n";
}
echo "\n".
"We have ".$this->schFound." results for \"".$this->needle."\" keywords\n";
}
}

$filename = array("../../tocmenu_821/upentrytodb.php",
"../../tocmenu_821/stock_process.php",
"../../tocmenu_821/trans_process.php");
$needle = "STK_NUM";
$case = FALSE;
$nFiles = sizeof($filename);

$schMe = new mthSearchEngine;
$schMe->initSearchEngine( $filename, $needle, $case, $nFiles);
$schMe->searchNow();
$schMe->searchPrint();
Pembahasan:

Kita mulai dari mainSearchEngine.php dolo dari yg sedikit dolo baru masuk ke yg sedikit lebih byk parameter nya...
String $filename = array("test.txt"); <-- disini lo masukin nama2 file yg mau lo search... Loh KOK!!! primitif amat, lahh khan gw udah bialng ini masih primitif! String $needle <-- string yg mau lo cari Boolean $case TRUE|FALSE <-- case sensitif ga?? Oh nooo positif hamil dehh :D Integer $nFiles <-- byk nya file yg lo definisikan di $filename. Yup... ok kita udah bisa search file... :D Artikel selanjutnya dah bukan class-101 tetapi adalah pengembangan modul ini... Kita buat search engine ini makin aduhai... Suggestions and Critics are very welcome...

Download file lengkap disini

No comments: