Archive for March, 2007

Free Text Editors Reviewed

It has been a while since I looked at using a different text editor. I have been a die hard SciTE user for the past 4 years. Today though I thought I would look at some other editors and see if they would meet my needs.

SciTE Home Page: http://www.scintilla.org/SciTE.html

First, I went to Wikipedia to review the list of text editors available: http://en.wikipedia.org/wiki/Comparison_of_text_editors

I also found a wiki that compares most of the popular editors with a shareware editor called Edit Plus.
http://editplus.info/wiki/Alternative_Editors
I visited the web sites to most of the free text editors. Through the process of elimination, limited my testing to two editors, Notepad++ and PSPad.

Notepad++
I was pretty blown away by the number of features that are packaged in Notepad++. IT also uses the Scintilla text control, which is found in SciTE. The only big feature that was missing in this editor was the api function arguments tool tips, sometimes referred to as call tips. This is one of the main reasons I use SciTE to this day. If someone could add this logic to the FunctionList plugin then I may be willing to switch to Notepad++.
http://notepad-plus.sourceforge.net/uk/site.htm
PSPad
PSPad had some features listed that really got my attention. Once I downloaded and installed, I was really disappointed. I was under the impression that TopStyle Light was integrated into PSPad. All PSPad does is launch TopStyle Lite. On top of that, PSPad does not include code folding and quite frankly, has so many menu options I was overwhelmed.
http://www.pspad.com/

TopStyle Lite
If there is one thing I did find from this search was TopStyle Lite. This CSS editor works reasonably well. I think Dreamweaver 8’s CSS editor has a cleaner interface but if you don’t have the cash to buy Dreamweaver and you want a CSS specific editor, then I would recommend this for sure.
http://www.newsgator.com/NGOLProduct.aspx?ProdId=TopStyle&ProdView=lite

I am going to stick with SciTE for now.

What text editor do you recommend?

basename() function for JavaScript

I wrote this basename() function for JavaScript today. It parses a file portion for either windows ‘c:\path\to\file.ext’, *nix ‘/path/to/file.ext’ or href ‘http://server/path/to/file.ext’.

function GetBaseName(file)
{
var Parts = file.split('\\');
if( Parts.length < 2 )
Parts = file.split('/');
return Parts[ Parts.length -1 ];
}

PHP file verification - md5_file vs sha1_file vs crc32 and no native crc32_file

The following was originally posted on php.net/crc32/ under the ‘User Contributed Notes’ but was recently removed. Since this information came up in topic again with a colleague, I am making this research available on my blog.

If you are trying to decide on a function for file verification, I came to the conclusion that md5_file() is the best all around solution.

file_crc() function that Bulk at bulksplace dot com posted on php.net/crc32/ is the most efficient solution on Windows for small and medium size files. It is most likely because file_get_contents() uses memory mapping techniques. Unfortunately on Linux (Fedora), the results were slightly better for md5_file().

sha1_file() on large files is slower than md5_file(). The time it takes for the __crc32_file() function found on php.net/crc32/ is linear to the size of the file. I would avoid using __crc32_file(). The file_crc() function will fail when using the file_get_contents() if the file is larger than the PHP.ini memory_limit setting. Windows does not seem to use the memory_limit for file_get_contents(), but I did run into an error ‘FATAL: emalloc(): Unable to allocate x bytes’ when testing iso files.

I ran the following tests on both WindowsXP and Fedora 4 machines.

< ?php
// File verification tests by Angelo Mandato (angelo [at] mandato {period} com)

// __crc32_file() is very slow, you can uncomment to test for yourself.
//require_once('crc32_file.php');
// Copy and paste the contents of the crc32_file() code found on
// the php.net crc32 PHP manual page in a new file and save
// as crc32_file.php in the same directory as this script.

// Get microseconds
function GetMicrotime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}

// file_crc() - function to test
function file_crc($file)
{
$file_string = file_get_contents($file);
$crc = crc32($file_string);
return sprintf("%u", $crc);
}

$Methods = array('sha1_file()', 'md5_file()', 'file_crc()');
if( function_exists('__crc32_file') )
$Methods[] = '__crc32_file()';
$directory = '/path/to/directory/'; // Don't forget trailing backslash.
$files = scandir($directory);

for( $method_index = 0; $method_index < count($Methods); $method_index++ )
{
$start_time = GetMicrotime();
while( list($index,$file) = each($files) )
{
if( $file != '.' && $file != '..' && is_file($directory.$file) )
{
switch( $method_index )
{
case 0: { // sha1_file()
$value = sha1_file($directory.$file);
}; break;
case 1: { // md5_file()
$value = md5_file($directory.$file);
}; break;
case 2: { // file_crc()
$value = file_crc($directory.$file);
}; break;
case 3: { // __crc32_file()
$value = __crc32_file($directory.$file);
}; break;
}
}
else // It is not part of our test results, lets remove it from the array
{
unset($files[$index]);
}
}
$end_time = GetMicrotime();
echo sprintf("%s took %.03f seconds to calculate %d files.\n", $Methods[$method_index], $end_time-$start_time, count($files) );
reset($files); // Reset pointer in array
}
echo "file verification tests completed.\n";
?>

In conclusion, the md5_file() function was the all around fastest file verification function in PHP. I suspect if a well written crc32_file() function was incorporated into PHP then it would be the best way verify files.