Archive for July, 2007

How to revert to Internet Explorer 6

Monday, July 30th, 2007
I installed Internet Explorer 7 on my Windows XP just for curiosity. Yeah, it was hate from the first sight, and that's why I wanted to get rid of it and go back to the "good" old IE6.

To revert to Internet Explorer 6, go to Control Panel Add/Remove Programs and locate Internet Explorer 7. Then choose to uninstall it. This will uninstall only IE7 from your system, the old version will be still available. You will prompted to restart Windows. Do so and you are done.

I use Internet Explorer only at work because my job requires that. I wouldn't use it otherwise, and I suggest that you don't use it either, unless someone pays you to do so...

WTP metadata migration error in eclipse

Friday, July 27th, 2007
Since I changed the location of a Dynamic Web Project in Eclipse, I started getting an error message in problems which says that "This project needs to migrate WTP metadata." I right-clicked the row and chose Quick Fix, but the error remained in the problems section. I checked some forums on the web and I saw that other people are facing exactly the same case.

I tried to Refresh the project, or Close it and Open again, but none of these worked. The solution was to Validate the project. After that the error was removed from the problems list. If you have a similar case, maybe you should try it and see if that works for you too.

UnxUtils: GNU utilities for Win32

Thursday, July 26th, 2007
UnxUtils is a port of common GNU utilities to native Windows binaries. I have been using it for a couple of years and I found it very convenient because it contains all of GNU's coreutils, plus several other useful programs that I use often, like: bc, bzip2/gzip/zcat, diff, grep/egrep, find, gawk, less, sed, tar, wget and many more.

At the time of writing the download link seems to be broken, but you can still get a copy from this website. Make sure to check the MD5 sums of the executable files, for integrity's sake.

It seems that the project has not been maintained for several years, thus the programs' versions are a little stale. If you need something more up-to-date you may find what you're looking for in GnuWin32, a project that "provides Win32 ports of tools with a GNU or similar open source license."

read and write compressed files

Thursday, July 26th, 2007
Sometimes you may want to read or write compressed files from your Java or .NET programs. This is fairly easy to accomplish in both platforms.

In Java you can use the GZIPInputStream and GZIPOutputStream to respectively read from and write to files or streams compressed in the GZIP format. These classes are part of the J2SE since version 1.4.2. Here is an example of reading a gzip-compressed file:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;

public class GZIPExample
{
public static void main( String[] args )
throws IOException
{
FileInputStream fileInputStream =
new FileInputStream("myCompressedFile.gz");
GZIPInputStream gzipInputStream =
new GZIPInputStream(fileInputStream);
InputStreamReader inputStreamReader =
new InputStreamReader(gzipInputStream);
BufferedReader in =
new BufferedReader(inputStreamReader);
String line;
while ((line = in.readLine()) != null)
System.out.println(line);
in.close();
}
}


Alternatively you can use ZIPInputStream and ZIPOutputStram to cope with file compressed in the ZIP format.

On the .NET platform you will need to get some third party libraries. #ZipLib (pronounced: Sharp-Zip-Lib) is a free open source library, written entirely in pure C# by ic#code. It supports the GZip, Zip, BZip2 and Tar formats. Here is an example that utilizes #ZipLib, it looks very much like the Java example above:

using System;
using System.IO;
using ICSharpCode.SharpZipLib.GZip;

namespace SharpZipLibTest
{
class Program
{
static void Main(string[] args)
{
FileStream fileStream =
File.OpenRead("myCompressedFile.gz");
GZipInputStream gzipInputStream =
new GZipInputStream(fileStream);
StreamReader input =
new StreamReader(gzipInputStream);
String line;
while ((line = input.ReadLine()) != null)
Console.WriteLine(line);
input.Close();
}
}
}


How to install fonts in Ubuntu

Monday, July 23rd, 2007
There are several ways to install new fonts in your Ubuntu system. However, the one I describe here is from the command line, which is the one that worked for me.

Open a terminal and run a shell as root. Then go to /usr/share/fonts/truetype which is where all your true-type fonts are installed. Create a new directory in there and put all the fonts you want to install in it. Then refresh the font cache (by issuing the fc-cache command):

$ sudo bash
Password:
# cd /usr/share/fonts/truetype
# mkdir myFonts
# cp /path/to/fonts/* myFonts
# exit
$ fc-cache -f -v
...
fc-cache: succeeded
$


This will make your fonts available system-wide, thus it requires root privileges. If you do not have system administrator's privileges, or you simply want to make the fonts available only to you, you can put your fonts in your $HOME/.fonts directory (you have to create it if it does not exist):

$ mkdir $HOME/.fonts
$ cp /path/to/fonts/* $HOME/.fonts
$ fc-cache -f -v
...
fc-cache: succeeded
$


Note that you do not have to have root privileges to run the fc-cache command.

Mac Fonts Free Download

Sunday, July 22nd, 2007
This one is worth sharing. While looking for the Lucida Grande font, I found this Mac Fonts free download. The fonts included in the ZIP file are: Apple Garamond, Aquabase, Lithograph, Lucida Grande, Lucida MAC, Lucida Console and MACGrande. Most of them are in true-type format, thus ready to be installed on Windows or GNU/Linux.

Comparing double values

Friday, July 20th, 2007
Even the experienced programmers often fall in a common mistake when it comes to double value comparison, which is using the equality operator == to compare two double precision (or float) values.

In order to represent a floating point number, a computer uses a finite number of bits. But there are certain numbers that require a bigger -even infinite- number of bits to be represented. In such cases the best the computer can do is to store the closest approximation.

The == equality operator performs a bit-to-bit comparison of the two values. This is fine when comparing integer values, but could be error prone in case of floating point numbers. It is possible, especially after a big number of floating point computations, two values that in theory would be considered equal, to end up being slightly different due to round ups. But even the slightest difference in the less important bits of the two numbers will make the equality operator to return false.

To compare two double values the proper way, you first have to choose a threshold value that defines the level of accuracy. This is how close two numbers should approach to be considered equal. Having this threshold value defined, to compare two double numbers you have to subtract them and see whether the absolute value of the difference is less than the threshold. Here is some some sample Java code:

if (Math.abs(d1-d2) < THRESHOLD) ...

The threshold value should be wisely chosen and depends on the level of accuracy that your application requires. For example, if you are working with decimal numbers of 4 significant digits after the decimal point, a good choice for the threshold value would be 0.00005. The rule is to take in account the numbers you are working with, and identify the smallest difference that two numbers should have to be considered not equal. Then you take this tiny value and cut it exactly in half, and that will be your threshold value.

Restoring GRUB after Windows installation

Wednesday, July 18th, 2007
When choosing to dual-boot between GNU/Linux and Windows it is recommended that you first install Windows and then GNU. However, sometimes you may want to install Windows on a system with an existing GNU/Linux installation, or you may simply need to re-install Windows.

Windows have the bad habit of removing any boot loaders and installing their own which is able to boot only Microsoft specific operating systems. Modern GNU/Linux distributions (like Ubuntu) use GRUB as the default boot loader. In order to restore GRUB on your dual-boot system you have to run the grub shell.

If possible try to boot into your Linux system from a CD or floppy disk. If you're unable to do so you can boot a GNU/Linux Live CD (like the Ubuntu installation CD/DVD) and run the grub shell from there. Open a terminal or a console and type the following:

# grub
grub> root (hd0,0)
grub> setup (hd0)
grub> quit
# reboot


Make sure to replace (hd0,0) and (hd0) with the values that correspond to your disk's configuration. Here, hd0 represents the first hard disk (which is the primary master, in most configurations). After rebooting, GRUB should be in position and take it from there.

Chances are that you may want to also edit the file /boot/grub/menu.lst to add a menu option for booting to your Windows installation. Edit the file and add somewhere the following lines:

title Windows
root (hd0,1)
chainloader +1


Again, make sure that you replace (hd0,1) to point to your Windows' partition.

Duff's Device

Tuesday, July 17th, 2007
Duff's Device is an optimization technique for loop unrolling that allows faster execution of loops by minimizing the number of tests. Tom Duff was credited for this unique C implementation:

switch ( count % 8 )
{
case 0: do { *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while (( count -= 8 ) > 0);
}


I couldn't believe my eyes when my friend Mark showed me this piece of code some years ago. I couldn't imagine that this thing would even compile. If you feel the same, just try it and see for yourself that this is perfectly valid C code.

What confused me was the combined use of the switch statement and the do-while loop and how those two interfere to each other. I asked myself, what a do-while loop really is? In fact, it's nothing more than one label, one if-statement and one goto statement. If you rewrite the code above in terms of labels and gotos you will end up with something like this:

switch ( count % 8 )
{
case 0:
start:
*to++ = *from++;
case 7:
*to++ = *from++;
case 6:
*to++ = *from++;
case 5:
*to++ = *from++;
case 4:
*to++ = *from++;
case 3:
*to++ = *from++;
case 2:
*to++ = *from++;
case 1:
*to++ = *from++;
if (( count -= 8 ) > 0) goto start;
}


This is equivalent to the original code, but it looks way too familiar. At this point you may wonder "Why interference between switch and loop statement is allowed in C?" The short answer is "Why not?" The long answer could be that the purpose of the C programming language was to make programmers' lives easier, but by no means to deprive the unlimited power and control that assembly so generously offered. If something could be implemented in assembly, it should also be implementable in C, only look prettier...

Because C# does not allow fall through, the closer you can get to Duff's Device is this ugly implementation:

if (count % 8 == 7) goto case_7;
if (count % 8 == 6) goto case_6;
if (count % 8 == 5) goto case_5;
if (count % 8 == 4) goto case_4;
if (count % 8 == 3) goto case_3;
if (count % 8 == 2) goto case_2;
if (count % 8 == 1) goto case_1;

case_0:
toBuffer[i++] = fromBuffer[i++];
case_7:
toBuffer[i++] = fromBuffer[i++];
case_6:
toBuffer[i++] = fromBuffer[i++];
case_5:
toBuffer[i++] = fromBuffer[i++];
case_4:
toBuffer[i++] = fromBuffer[i++];
case_3:
toBuffer[i++] = fromBuffer[i++];
case_2:
toBuffer[i++] = fromBuffer[i++];
case_1:
toBuffer[i++] = fromBuffer[i++];
if (( count -= 8 ) > 0) goto case_0;


In Java things are even worse. Java allows fall through, but will not allow interference between the switch statement and the do-while loop. Also Java's lack of a goto statement makes impossible to follow the other two approaches.

Broadcom wi-fi to work on Ubuntu

Thursday, July 12th, 2007
A Broadcom wireless card does not work on Ubuntu out of the box. Ubuntu includes the right driver for these cards (bcm43xx) and the card is recognized by the kernel. You can see it in the available wireless interfaces, but you cannot connect to any wireless network. This is because the driver itself is not enough for these cards to work. Additional firmware is required by the driver. To solve this problem on Feisty Fawn could not be simpler, you just have to type:

sudo apt-get install bcm43xx-fwcutter

This will install the bcm43xx-fwcutter utility (which is used to extract firmware out of Windows drivers binaries), but it will also automatically download windows binaries from the Internet, run the fwcutter program and install the extracted firmware, so you won't have to do nothing at all (perhaps the only thing you may need to do is to reload the bcm43xx driver).

This easiness of making things work should be much appreciated, if you consider that on earlier Ubuntu versions you had to download and compile the bcm43xx-fwcutter program from source, find and download driver binaries, run the fwcutter, and finally install the output firmware files.