Site Sponsors:
Easy Backgrounds for Android 
I have developed a great respect for the developers of the Android Graphics Framework. Why? Because it is obvious that the developers like to use their classes, themselves.

It's Ragu!


Indeed, from doing complex things like affine transforms, to even simple things like measuring a Canvas or a Bitmap, Android has the data we need, when-we-need-it. -From splits to splines, it all seems to be "in there"! (The Company that makes Ragu Spaghetti Sauce had a commercial a few years back - many take the expression it's ragu to mean ''it's in there''!)

Vista


Such was thee case of having what we needed "in there" when we set our paws to spruce-up a little ditty we wrote for the Android Marketplace. When it came time to spread a tile image all over a background, we had what we needed. -So much so that we feel confident that what we wrote will be able to work across hand-held devices of infinite screen size. All because members are ragu, where & when they should be... and more:

public void draw(Canvas can) {
if (bmpBack != null) {
PixelCellMap map = new PixelCellMap(can.getWidth(), can.getHeight(), bmpBack.getWidth(), bmpBack.getHeight());
int iCells = map.getCellCount();
for (int ss = 0; ss < iCells; ss++) {
Point pt = map.getActual(ss);
can.drawBitmap(bmpBack, pt.x, pt.y, paintPaper);
}
} else {
can.drawColor(paintPaper.getColor());
}
}

Note that the PixelCellMap class is part of a forthcoming Open Source Toolkit. One we are creating for cross-platform Android / Swing / AWT portability. -If you want to learn more about it (the code-named for the graphical part of the framework is Vista (sorry - but that moniker just made the most sense at the moment)), then give us a holler.


[ view entry ] ( 2249 views )   |  permalink  |  related link
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 ] ( 2131 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 ] ( 2298 views )   |  permalink  |  related link

<<First <Back | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | Next> Last>>