Author Archives: aidvu

Singleton Pattern


Design patterns are reusable solutions to common software engineering problems. They are best-practice in software design. It is not easy to apply them, and it often happens that pattern misuse may lead to bigger problems, a good design supported by patterns will last and give you less headaches. Examples in PHP and Java are given.

Singleton is a creational pattern (creational patterns deal with object creation mechanisms) which restricts instantiation of a class to one (and only one) object. We’ll discuss it in 3 chapters:

Tagged , , ,

Matlab 7 under Windows 7

If you have trouble installing, switch to “Windows Classic” theme under “Personalization” (should be done automatically). After the installation, we need to setup Matlab to use the newest Java version instead of the supplied one.

  1. Download and install the latest jre version
  2. Go to C:\Program Files\Java (or C:\Program Files (x86)\Java)
  3. Copy the folder name jre6 to <Matlab-installation-dir>\sys\java\jre\win32
  4. Go to <Matlab-installation-dir>sys\java\jre\win32
  5. Open the file named jre.cfg
  6. Instead of (in my case 1.4.2), write 6. Now Matlab will use jre6 instead of jre1.4.2.

Matlab should start now without problems.

Tagged , ,

Apache mod_rewrite setup

This module provides a rule-based rewriting engine to rewrite requested URLs on the fly. Let’s see how to set it up and working in a few steps.

It comes bundled with Apache HTTP Server. To install Apache server on Linux:

apt-get install apache2

or

yum install apache2

then enable the mod (not enabled by default)

a2enmod rewrite

Now we are going to enable application specific settings on the apache server.

  1. Go to /etc/apache2/sites-available/
  2. Open default for editing.
  3. Replace AllowOverride None with AllowOverride All
<Directory /var/www/>
	Options Indexes FollowSymLinks MultiViews
	AllowOverride All
	Order allow,deny
	allow from all
</Directory>

All left is to check if it’s working. Since we’re going to need PHP install it and restart apache.

apt-get install php5 libapache2-mod-php5
/etc/init.d/apache2 restart

Enter the root directory of the web app you want to use mod_rewrite with.
Make index.php:

<?php
	echo "http://" . $_SERVER['HTTP_HOST']  . $_SERVER['REQUEST_URI'];
?>

Open/Make a file .htaccess and add the following lines:

RewriteEngine on
RewriteRule ^.*$ index.php

This rule will redirect everything that comes to your web app to index.php inside the same app, and print the full address.

E.g.
A good mod_rewrite cheat sheet can be found here. (courtesy of AddedBytes)

Tagged , ,

Number converter

Since it’s needed for URL Shorteners, and that’s what I’m writing a tutorial about, here’s a number converter from decimal, to whatever base you need (if you supply a base array long enough). Written in PHP.

Update (2011-07-27):

Changed method to static and added an exception.

/**
 * Class with static methods for converting numbers from decimal to a given base number system
 *
 */
class NumberConverter {
	/**
	 * Array to be used for conversion
	 */ 
	private static $base = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+=";

	/**
	 * Get's the $number character from $base array 
	 * 
	 * @param number - the position of the character needed
	 * @return Returns a character from $base for a given $number 
	 */
	private static function getCharacter($number) {
		return self::$base{$number};
	}

	/**
	 * Converts a decimal number to a given base 
	 * 
	 * @param number - Number to be converted
	 * @param toBase - Number base to be converted to
	 * @throws Exception
	 */
	public static function fromDecimalToBase($number, $toBase){

		// Check if the base is greater then the max possible base
		if ($toBase > strlen(self::$base)) {
			throw new Exception('Base too big.');
		}
		// Convert number to integer (better safe than sorry)
		$number = intval($number);

		// Set a flag if the number is negative, do a positive conversion then just add the minus sign
		$negative = false;
		if ($number <= 0) {
			$negative = true;
			$number = $number * -1;
		}

		$converted = "";
		while ($number != 0) {
			// Get a number in the new base
			$modulus = $number % $toBase;
			// Get the number representation in that base
			$converted = self::getCharacter($modulus) . $converted;
			// Remove the extracted number and continue until the number is 0
			$number = intval(($number - $modulus) / $toBase);
		}

		// Add the minus sign if it was negative
		if ($negative) {
			$converted = "-" . $converted;
		}
		 
		return $converted;

	}
}
Tagged , ,

Skype login window disappearance fix

For 2 days, some people are reporting login window disappearances. Happened to me on both my Windows XP and Ubuntu 10.10 machine. The solution is quite the same:

  1. Locate the Skype user settings folder
  2. Delete it

On Linux:

  1. Open terminal
  2. rm -rf /home/username/.Skype

On Windows:

  1. Start -> Run
  2. %appdata%\Skype
  3. Select All (CTRL + A)
  4. Delete

Update (31.05.2011.):

As some people pointed, there is a better solution than deleting the whole Skype user settings folder. Instead, you should only delete the shared.xml file in it.

Tagged ,