Databug

Tuesday, July 11, 2006

Method: _databug_thresh_file($fpath, $ratio_denominator=3)

I'm trying to make the class workable without a database because I'm lazy and find integrating them in my PHP scripts a bit of a nuisance and, well, I just find it tidier not to get MySQL involved sometimes.

There are a couple methods in the class for opening files and pulling data into an array. Most involve some randomization of the result. So I wrote this method to lighten the load. The $ratio_denominator argument indicates the ratio of lines (as in 1 in every $ratio_denominator lines) that will be pulled into the array (i.e. the "threshing"). The same concept is used with set_zip_data_randomly_by_range function.

/**
* @access private
* @return array
*/
function _databug_thresh_file($fpath, $ratio_denominator=3)
{
// *** DATA

# internal
$skip_token = '%';
$_d = $ratio_denominator;
$_i = 0;

# return
$LINES = array();


// *** MANIPULATE

# sanity check
if ( !is_file($source_path) )
{
trigger_error("file [$source_path] not found", E_USER_WARNING);
return 0;
}

# open file (for reading)
$_handle = @fopen($source_path, "r");

# Set Mod Offset
$_mod_offset = mt_rand(0, $_d-1);

# fetch file lines
while ( !feof($_handle) )
{
$_i++;
if ( $_i % $_d <> $_mod_offset )
{
continue;
}

$_buffer = fgets($_handle, 4096);
$_line = trim($_buffer);

# check for skip token
if ( substr($_line,0,1) <> $skip_token && !empty($_line) )
{
$LINES[] = $_line;
}
}

# close file
fclose($_handle);

# DEBUG
#print_r($LINES);

# catch
if ( !count($LINES) )
{
trigger_error('no lines found', E_USER_WARNING);
}
elseif ( count($LINES) == 1 )
{
trigger_error('only 1 line fetched -> check EOL delimiter', E_USER_NOTICE);
}

// *** RETURN

return $LINES;
}

 

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home