Site Sponsors:
New Pascal Strings for PHP 
New Pascal: We use it in Java, .Net, and C/C++ - I even used them in the 118,000 recipes we have on this site.

Now is the time to share!

New Pascal Strings


Perfect for when we need to quickly index / save large "heaps" of textual data - or when we need to communicate between just about any other programming languages - we believe that the New Pascal String convention is both obvious, & simple. -So simple that we wrapped the convention up into a PHP class:


<?php
/**
* An opportunity to encapsulate the reading & writing of New Pascal Strings.
* Designed to work with my other "New Pascal Strings" readers and writers
* for Java, C/C++, and .NET.
*/
class IOPascal {

/**
* Properly open a file for cross-platform string-reading.
* @param type $file The file to read.
* @return type The file handle will be FALSE on error.
*/
public static function OpenRead($file) {
$hfile = fopen($file, 'rb');
return $hfile;
}

/**
* Properly open a file for cross-platform string-writing.
* @param type $file The file to write.
* @return type The file handle will be FALSE on error.
*/
public static function OpenAppend($file) {
$hfile = fopen($file, 'a+b');
return $hfile;
}

/**
* Read a Pascal String, as written by this, or a legacy, Pascal String writer.
* @param type $handle The handle of a file to read-from. (see <i>OpenRead</i>)
* @return type The string, as read from the file.
*/
public static function ReadString($handle) {
$char = '-';
while ($char != ' ') {
if (feof($handle))
return null;
$char = fgetc($handle);
if ($char == ' ')
continue;
$len = $len . $char;
}
if ($len)
$string = fread($handle, $len);
return $string;
}

/**
* Write a string to the file for reading by this, or a legacy. Pascal String reader.
* @param type $handle The handle of a file to write-to. (see <i>OpenAppend</i>)
* @param type $string The stirng, as destined to write-to a file.
* @return type Returns the size of the string as written to the file, else FALSE on error / partial write.
*/
public static function WriteString($handle, $string) {
$len = strlen($string);
$len = fwrite($handle, $len);
if($len == false)
return $len;
$len = fwrite($handle, ' ');
if($len == false)
return $len;
return fwrite($handle, $string);
}

}
?>


So when you feel the need to put a simple cross-tech count in front of a string - and when you do not want the newlines to stomp all over your activities - free free to use IOPascal.

Here is the test case:

<?php

include_once 'IOPascal.php';

echo '<h1>IOPascalTest.php</h1>';

$file = 'IOPascal.tmp';
$tstx = new IOPascal;

$test = 'If you can read this, then all is well';

unlink($file);
$hand = $tstx->OpenAppend($file);
if ($hand == false) {
echo 'Error 0: Access Permission Failure.';
exit(1);
}
$pos = $tstx->WriteString($hand, $test);
fclose($hand);

if ($pos != strlen($test)) {
echo 'Error 100: Basic read error.';
exit(1);
} else {
$hand = $tstx->OpenRead($file);
if ($hand == false) {
echo 'Error 101: Open Error.';
exit(1);
}
$result = $tstx->ReadString($hand);
fclose($hand);

if (strcmp($result, $test) != 0) {
echo 'Error 102: IO result mis-match?';
exit(1);
} else {
unlink($file);
$hand = $tstx->OpenAppend($file);
if ($hand == false)
echo 'Error 103<br>\n';
else {
$stuff = array('One', 'Two', '', "F\rour", "\nFive\r\n", 'Six');
foreach ($stuff as $str) {
$pos = $tstx->WriteString($hand, $str);
}
fclose($hand);
$hand = $tstx->OpenRead($file);
foreach ($stuff as $str) {
$input = $tstx->ReadString($hand);
if (strcmp($str, $input) != 0) {
echo "Error 104: Unable to read [$str]<br>";
exit(1);
} else {
echo "Okay [$str]<br>";
}
}
fclose($hand);
unlink($file);
echo 'Success.<br>';
exit(0);
}
}
}
?>


Enjoy,

-Rn


[ view entry ] ( 2525 views )   |  permalink  |  related link

<<First <Back | 1 | 2 |