Archive for the ‘Programming’ Category.

Generic WeakReference

I always felt that I wanted to use the WeakReference class a lot like the Nullable<T> class.


internal class WrappedWeakReference<T> : WeakReference
 {
 public WrappedWeakReference(T target)
 : base(target)
 {
 }

 public new T Target
 {
 get { return (T) base.Target; }
 set { base.Target = value; }
 }

 public static implicit operator WrappedWeakReference<T>(T item)
 {
 return new WrappedWeakReference<T>(item);
 }
 }

This allows you to write things like:

 private WrappedWeakReference<MyObject> weakMyObject;

 public void Foo()
 {
 weakMyObject = new MyObject();
 }

 public void Bar()
 {
 weakMyObject.Target.SomeProperty....etc
 }
}

I think its nice idea.

However never did actually use this, I implemented the solution in a different manner where I did not need a weakreference, so this never got tested. I am not sure if it would work.

Empty object code snippet pattern

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>Empty pattern snippet</Title>
			<Shortcut>empty</Shortcut>
			<Description>Creates an empty static property and nested null object </Description>
			<Author>Alex Boyne-Aitken</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>type</ID>
					<ToolTip>Class type</ToolTip>
					<Default>Class</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp"><![CDATA[
			private static readonly $type$ empty = new NullObject();
public static $type$ Empty { get { return empty; } }

private class NullObject : $type$
{
}
			$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

Download

View and ViewModel pattern code snippet

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>ViewModel pattern snippet</Title>
			<Shortcut>viewmodel</Shortcut>
			<Description>Creates a view and viewmodel interface and a concreate view model </Description>
			<Author>Alex Boyne-Aitken</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>name</ID>
					<ToolTip>Base name</ToolTip>
					<Default>Class</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp"><![CDATA[
    public interface I$name$View
    {
        I$name$ViewModel Model { get; set; }
    }

    public interface I$name$ViewModel
    {
        I$name$View View { get; }
    }

    class $name$ViewModel : I$name$ViewModel
    {
        private readonly I$name$View view;

        public $name$ViewModel(I$name$View view)
        {
            this.view = view;
            view.Model = this;
        }

        public I$name$View View
        {
            get { return view; }
        }
    }
			$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

Download

Using 7-zip and batch files to perform backups automatically

UPDATE: I have recently overhauled this and created a simpler script in this post: http://www.logaans-site.co.uk/2009/10/17/simpler-version-of-the-7-zip-backup/

First, a little history:

Since the days when I started using XP I had gotten used to the control that NT Backup provided to allow me to backup my files.

About a year ago I moved to Vista and was unhappy with the “All or nothing” approach that the bundled backup software provided. I was also using OneCare at the time and it followed a similar policy.

Since moving to BitDefender I was much happier with the backup software it provided yet the fact that I was not using a well known backup format rubbed me.

So, using this article: http://www.wilshireone.com/article/115/easy-backups-with-7-zip as a guide, I engineered my own backup mechnism using 7-zip.

Now heres the fun. My backup schedule consists of a weekly incremental backup and one monthly full backup. My files are not so important that I need frequent backups, this schedule suits me down to the ground.

My solution consists of 7 parts:

  • Backup.cmd, this is the main script that executes the command
  • Backupset.txt, a list of files and folders that I wish to backup
  • CurrentSet.txt, a path to the current backup file. Used by incremental backups to update
  • FullBackup.cmd, the script to run a full backup
  • FullBackupSettings.ini, the settings used by a full backup
  • IncrementalBackup.cmd, the script to run an incremental backup
  • IncrementalBackupSettings.ini, the settings used by the incremental backup

Now you could remove the separate scripts for the full and incremental backup, the reason I created separate scripts was that I don’t have to change the arguments in the task scheduler. Instead it is all controlled through those scripts.

Backup.cmd

@echo off

echo TRACE: Start

IF EXIST %1 GOTO Begin

echo Settings file does not exist
GOTO End

:Begin

echo TRACE: Read settings
for /f "eol=# tokens=1,2 delims==" %%i in (%1) do SET %%i=%%j

IF "%varBackupType%"=="full" GOTO CreateNewSet

echo TRACE: Load existing set
for /F %%i in (CurrentSet.txt) do set varTargetBackupSet=%%i

IF EXIST "%varTargetBackupSet%" GOTO Execute

echo Backup set does not exist!
GOTO End

:CreateNewSet
echo TRACE: Create new set

set varTargetBackupSet=%varBackupLocation%\%DATE:~-4%-%DATE:~3,2%-%DATE:~0,2%-%TIME:~0,2%-%TIME:~3,2%-backup.%varFormat%
echo %varTargetBackupSet% > CurrentSet.txt

:Execute
echo TRACE: Execute backup
"%var7zipPath%\7z" %varMode% -t%varFormat% "%varTargetBackupSet%" @"%varFileList%"

:End
echo TRACE: Finished
pause

The backup script loads the settings passed as argument 1, does some checking, then calls 7-zip to begin backing up. The settings files define how the files are added and where to etc.

BackupSet.txt

D:\Guild Wars\Screens
D:\Logaan\Documents
D:\Logaan\Favorites
C:\Users\Logaan\Desktop
D:\Logaan\Saved Games
C:\Users\Logaan\AppData\Local\2DBoy
C:\Users\Logaan\AppData\Local\Ascaron Entertainment
C:\Users\Logaan\AppData\Local\id Software
C:\Users\Logaan\AppData\Local\Ironclad Games
C:\Users\Logaan\AppData\Local\Rockstar Games
C:\Users\Logaan\AppData\Roaming\EditPlus 3
C:\Users\Logaan\AppData\Roaming\FileZilla
C:\Users\Logaan\AppData\Roaming\Free Download Manager
C:\Users\Logaan\AppData\Roaming\Xfire

This is the list file passed to 7-zip. The only downside is that you cannot list the same folder or file name twice in this file. The work around would be to invoke multiple backup scripts and then add the duplicate folders and nested zips.

CurrentSet.txt

D:\2009-04-05-17-16-backup.zip

This contains the backup file created in the last full backup. Incremental backups then read it in and use it to update files.

FullBackup.cmd

@echo off
Backup D:\Logaan\Documents\Tools\7ZipBackup\FullBackupSettings.ini

Simples, it just calls the main backup script with the correct settings.

FullBackupSettings.ini

# File path to the 7-zip executables
var7zipPath=C:\Program Files\7-Zip

# Backup format
varFormat=zip

# Target location for the backup
varBackupLocation=D:

# List file
varFileList=D:\Logaan\Documents\Tools\7ZipBackup\BackupSet.txt

# Mode
varMode=a

# Type
varBackupType=full

The settings file is read in and used in the main backup script. Note the mode and backup type, this is what separates the two types of backup.

IncrementalBackup.cmd

@echo off
Backup D:\Logaan\Documents\Tools\7ZipBackup\IncrementalBackupSettings.ini

Similar to the full version, except its providing the increment backup settings

IncrementalBackupSettings.ini

# File path to the 7-zip executables
var7zipPath=C:\Program Files\7-Zip

# Backup format
varFormat=zip

# Target location for the backup
varBackupLocation=D:

# List file
varFileList=D:\Logaan\Documents\Tools\7ZipBackup\BackupSet.txt

# Mode
varMode=u

# Type
varBackupType=incremental

Similar to the full settings, but with the zip mode set to update and its backup type defined as incremental.

Once that has all been put in place, all I do is create two tasks in the windows task scheduler.

You could go one step further and have the script copy the backups to another disk, I just do this myself when I feel like.

Update

After a little testing I found a flaw in the technique that I was using. Essentially what is happening is that new files are being added to the old archive and any deleted files were being left. I want to keep any files that I deleted but I do not want them muddying up the actual latest image.

Instead, what I did was use the -u switch to stop updates to the base archive being made and adding new files to a new archive, which makes it incremental in its true sense.

Backup.cmd

@echo off

echo TRACE: Start

IF EXIST %1 GOTO Begin

echo Settings file does not exist
GOTO End

:Begin

echo TRACE: Read settings
for /f "eol=# tokens=1,2 delims==" %%i in (%1) do SET %%i=%%j

set varNewBackupSet=%varBackupLocation%\%DATE:~-4%-%DATE:~3,2%-%DATE:~0,2%-%TIME:~0,2%-%TIME:~3,2%-backup.%varFormat%

IF "%varBackupType%"=="full" GOTO CreateNewSet

echo TRACE: Load existing set
for /F %%i in (CurrentSet.txt) do set varOldBackupSet=%%i

IF EXIST "%varOldBackupSet%" GOTO ExecuteIncremental

echo Backup set does not exist!
GOTO End

:CreateNewSet
echo TRACE: Create new set
echo %varNewBackupSet% > CurrentSet.txt

:ExecuteFull
echo TRACE: Execute Full backup
"%var7zipPath%\7z" a -t%varFormat% "%varNewBackupSet%" @"%varFileList%"
GOTO End

:ExecuteIncremental
echo TRACE: Execute Incremental backup
"%var7zipPath%\7z" u -u- -u!"%varNewBackupSet%" -t%varFormat% "%varOldBackupSet%" @"%varFileList%"

:End
echo TRACE: Finished
pause

In fact, I have gone one step further and I have built a command line tool that wraps 7-zip and provides a nicer mechanism for configuring the backups. It then executes 7-zip. Once I have tested it a little I will post the source code.

Techinque for loading batch file parameters/settings

This technique allows you to define all of your parameters in a single file and not have to pass them all in on the command line.

This command reads in all of the tokens from the settings file and sets a series of environment variables

for /f "eol=# tokens=1,2 delims==" %%i in (settings.ini) do SET %%i=%%j

The settings file looks like this

# Comment
FirstVariableName=A value

# Comment
AnotherVariableName=A value

Just repeat those blocks. You can then do some validation on the enviroment variables if needs be.

I learnt this technique a little while ago, but I cannot remember where, so I cannot give credit where it is due.

A managed Guild Wars Template Parser

I am currently working on a small project to help me organise my Guild Wars skill builds but I came across a problem, I had nothing to help me decode the templates and I found I had a gap in my knowledge when dealing with converting binary and base 64.

I found some source code long ago from GWFreaks however it was so old, messy and written in VB.NET that I really struggled to understand it. The last resort was a PHP based version of the template parser and a small snippet I found on a Code Guru forum. With this information I begun playing around and eventually began to build a greater understanding of what was involved.

Using the wiki guide on the Skill Template Format and the Equipment Template Format I finally built a set of objects to help encode and decode the templates.

You will find the details and the project files on the Template Parser project page

Unit Testing with NetBeans and phpUnit

Huzzah! I have found a way of being able to run the Unit tests in NetBeans.

Since phpUnit is essentially a set of php scripts itself, I thought to myself there must be a way of invoking it.

What I did was setup a source file as the boostrap for the Test Runner, set this file as NetBeans index file and from there you can just run it and see the test results in the output window or debug and it step into the test you are interested in.

It is not a fantastic solution but it is definately a stop gap until they do implement php Unit testing.

Heres my boostrap file for running tests:

<php
$args = array(
__FILE__,
"--coverage-html",
"D:\\Logaan\\Documents\\XamppData\\htdocs\\FormManager\\CodeCoverage",
"--verbose",
"D:\\Logaan\\Documents\\XamppData\\htdocs\\FormManager\\tests\\TestSuite.php"
);

$_SERVER['argv'] = $args;
$_SERVER['argc'] = count($args);

require_once 'PHPUnit/TextUI/Command.php';

// Exclude this file from code coverage
PHPUnit_Util_Filter::addFileToFilter(__FILE__);

?>

Its very simple, what I am doing is hard coding my arguments into the server environment variable (you could use the arguments in the NetBeans configuration) which is where phpUnit gets the arguments from.
Including the phpUnit TestUI test runner which has the main entry point, excluding this file from code coverage and then I just sit back and l let phpUnit run its course.

This is only a work around until Netbeans supports phpUnit.

NetBeans IDE

The other day I had hit a brick wall, I was really struggling with part of my FormManager project and really needed to debug it.

I started looking into XDebug and discovered what it could really do, and diving into the website I found they listed a set of compatible IDEs that could use the extension to debug php. Since I had spurned eclipse I thought there was nothing else short of the Zend IDE that could help. Thats when I noticed NetBeans. The last time I had used NetBeans was back in college for some Java projects. I was really surprised when I found out that they offered a PHP IDE.

Within a couple of seconds of getting the project setup in the IDE it found the problem already for me. It does a much better job of context sensitive variables than Eclipse ever did, and it can pick up all kinds of problems much better than Eclipse, within a few minutes I had fixed the problem and even found a couple of problems that I did not yet know about.

It even includes some basic refactoring tools!

NetBeans does support Unit Testing however there is currently no support for phpUnit at this time. It is planned as a feature some point in the future.

Modelling OO

NClass is a free open source class diagram modelling editor. It currently supports C# and Java. However recently I have used it to help model some of my php applications. Its quick and easy to use, simple installs, all round good app.

Form Manager

I finally found a decent name for this “DynamicDataModel” project I have been working on; Form Manager.

Synopsis: A set of objects to allow the developer to write as little code as possible to instantly get a form with validated fields and basic data manipulation.

My idea to was to write something that would allow me to quickly describe what data I want to edit/display, have a standard mechanism for verify data from the user and a generic method of talking to a database to create/get that data.

So far this is what I have come up with:

I built the diagram in NClass, but I kind of cheated and built the model in C# and imported it

The modes know how to render the data, where the data needs to come from and at what time.
The fields are quite the opposite, they know where the data is, and what to do with it, but don’t know when to use it.

So I built this model, the TableDefinition and FieldSet have all the fields hanging off them. And the relevant modes are hanging off the FormManager.

Although I plan to integrate this into phpBB, I have put in layers that should allow for this model to be used in anything.

The other thing is that I am trying to build in as little state as possible so that it can be cached to improve speed. However this will be seen once I start using it in more realistic situations.

The only wierd thing about the model that I am not completely happy with yet is that when the field is told to render itself, it is given the data to use.

I have unit testing around it all, and generated documentation so once its finished I should have quite a nice comprehensive application.