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 ] ( 2304 views )   |  permalink  |  related link
World's Simplest Source Code Formatter? 
Formatting code need not be tough. Indeed, when it came time to write a simple brace-based source code indentation-class for ClassIO, here is what we came up with:


package com.soft9000;

import java.util.Formatter;
import javax.xml.bind.DatatypeConverter;

/**
* A simple "brace based" code-formatter. Source code comments are supported.
*
* @author profnagy
*/
public class SourceCodeFormatter {

public static String FormatCode(String sText) {
SourceCodeFormatter ref = new SourceCodeFormatter();
return ref.format(sText, " ");
}

static class StringTokenizer {

private final String sBlockOn;
private final String codeStringReplacement;
private final String codeString;

public StringTokenizer(String sBlockOn, char chCode) {
this.sBlockOn = sBlockOn;
codeString = "" + chCode;
Formatter ref = new java.util.Formatter();
codeStringReplacement = ".:" + DatatypeConverter.printHexBinary(codeString.getBytes()) + ":.";
}

public String encode(String sSource) {
sSource = sSource.replace(codeString, codeStringReplacement);
return sSource.replace(sBlockOn, codeString);
}

public String decode(String sSource) {
sSource.replace(codeString, sBlockOn);
sSource = sSource.replace(codeStringReplacement, codeString);
return sSource;
}

public char getCode() {
return codeString.charAt(0);
}

public String getStirng() {
return sBlockOn;
}
}
StringTokenizer blockCommentOn;
StringTokenizer blockCommentOff;
StringTokenizer lineCommentOn;

public SourceCodeFormatter() {
blockCommentOn = new StringTokenizer("/*", '$');
blockCommentOff = new StringTokenizer("*/", '&');
lineCommentOn = new StringTokenizer("//", '#');
}

public String format(String sText, String sLevelPrefix) {
if (sText == null || sLevelPrefix == null) {
return "";
}
sText = this.lineCommentOn.encode(sText);
sText = this.blockCommentOff.encode(sText);
sText = this.blockCommentOn.encode(sText);

int pwComment = 0;

char chStart = '{';
char chStop = '}';
StringBuilder sb = new StringBuilder();
int iLevel = 0;
char[] chars = sText.toCharArray();
boolean bIgnoreWS = false;
for (char ch : chars) {
switch (pwComment) {
case 0: {
if (ch == blockCommentOn.getCode()) {
pwComment = 2; // flag block comment
sb.append(blockCommentOn.getStirng());
bIgnoreWS = false;
continue;
}
if (ch == lineCommentOn.getCode()) {
pwComment = 1; // flag linear comment
sb.append(lineCommentOn.getStirng());
bIgnoreWS = false;
continue;
}
}
case 1: {
if (ch == '\n') {
pwComment = 0; // passthru of single-line comment ends
bIgnoreWS = true;
sb.append(ch);
continue;
}
}
break;
case 2: {
if (ch == blockCommentOff.getCode()) {
pwComment = 0; // passthru of block comment ends
bIgnoreWS = true;
sb.append(blockCommentOff.getStirng());
continue;
}
}
break;
}
if (pwComment == 0) {
if (ch == '\n') {
bIgnoreWS = true;
sb.append(ch);
continue;
}
if (ch == chStart) {
iLevel++;
}
if (ch == chStop) {
iLevel--;
}
if (bIgnoreWS) {
boolean isWS = Character.isWhitespace(ch);
if (isWS) {
continue;
}
for (int ss = 0; ss < iLevel; ss++) {
sb.append(sLevelPrefix);
}
}
}
sb.append(ch);
bIgnoreWS = false;
}
sText = this.lineCommentOn.decode(sb.toString());
sText = this.blockCommentOff.decode(sText);
sText = this.blockCommentOn.decode(sText);
return sText;
}
}


Notice that we did not have to create an entire project, include a jar, or add hundreds of lines to perform such a simple task. -A nice little class to use when we need to keep things simple, feel free to use SourceCodeFormatter to indent-format your C/C++, Java, and C# project(s), as well.

[ view entry ] ( 2514 views )   |  permalink  |  related link
Reading, Writing, & Converting between XML, TDF, & CSV 
Just a quick note to let everyone know that a new release of ClassIO is out:

https://sourceforge.net/p/classio/home/Home/

ClassIO is a command-line tool (executable JAR) that allows us to quickly create POJOs. Java Source Code that can create, read, update, and save arrays of objects via file.

Because support for every format is automatically provided, ClassIO objects can save / convert their data to / from Extended Markup Language (DOM/XML), Comma-Separated Value (CSV), and / or Tab-Delimited Format (TDF.)

Enjoy,

-Rn

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

<<First <Back | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | Next> Last>>