2007-05-16

Convert absolute path to relative path

Today I needed to convert an absolute path to a relative path based on a specified base path. E.g.

c:\a\b\c -> c:\a\b\c\d\file.txt = d\file.txt
c:\a\b\c -> c:\a\file.txt = ..\..\file.txt
c:\a\b\c -> c:\a\x\file.txt = ..\..\x\file.txt

I am surprised there is nothing in the .NET framework so I had a hunt around and converted the code from the following URL (http://www.vergentsoftware.com/blogs/ckinsman/default.aspx?date=2006-08-07) into C#....


private string RelativePath(string absolutePath, string relativeTo)
{
string[] absoluteDirectories = absolutePath.Split('\\');
string[] relativeDirectories = relativeTo.Split('\\');

//Get the shortest of the two paths
int length = absoluteDirectories.Length < relativeDirectories.Length ? absoluteDirectories.Length : relativeDirectories.Length;

//Use to determine where in the loop we exited
int lastCommonRoot = -1;
int index;

//Find common root
for (index = 0; index < length; index++)
if (absoluteDirectories[index] == relativeDirectories[index])
lastCommonRoot = index;
else
break;

//If we didn't find a common prefix then throw
if (lastCommonRoot == -1)
throw new ArgumentException("Paths do not have a common base");

//Build up the relative path
StringBuilder relativePath = new StringBuilder();

//Add on the ..
for (index = lastCommonRoot + 1; index < absoluteDirectories.Length; index++)
if (absoluteDirectories[index].Length > 0)
relativePath.Append("..\\");

//Add on the folders
for (index = lastCommonRoot + 1; index < relativeDirectories.Length - 1; index++)
relativePath.Append(relativeDirectories[index] + "\\");
relativePath.Append(relativeDirectories[relativeDirectories.Length - 1]);

return relativePath.ToString();
}

2007-05-12

I have a new computer

Well, my laptop sort of works, but the wireless keeps turning itself off randomly and then wont work for a while. So I've decided to go out and buy a new computer.

I thought that iMacs looked nice, and heard that I can emulate Windows XP/Vista using a program called "Parallels". The only thing that concerned me was that I would spend £1000 buying this computer only to find out that VS2005 running inside Parallels cannot deploy compact framework apps to my Pocket PC.

I phoned my local Apple centre and they basically said "Don't worry about it. If it doesn't work then just bring it back." how good is that for customer service eh? So here I am writing my first blog entry on MacOSX. It's a bit weird but I really like it!

I'm still installing Vista at the moment. If I can't get that working I will try XP.

How weird :-)

2007-05-08

Acer Skoda - I mean Ferrari

I have owned an Acer Ferrari now for approximately 13 months. Previously I had a problem with it freezing randomly. I spent some time trying to reproduce the problem and after a month or two was finally able to reproduce it 100% of the time. I sent my laptop back to Acer armed with exact steps and as a result my laptop was returned to me after only a couple of days.

For the past 2-3 months I have been seeing very rare, random resets. Obviously I blamed Windows, as you do :-) Some software I had to convert AVI to MPEG would always reset the laptop, but I just put that down to dodgy software. More recently I noticed that 7-zip would also reset my laptop *only* if I used ULTRA compression on a file larger than 500MB.

So I tried Winzip, same problem. I installed Vista and tried both Winzip and 7-zip in there, same problem. With steps to reproduce + proof that it was not the OS I felt I could finally send the laptop back for repair without running the risk of it coming back "No fault found".

The problem lies in my opening sentence "I have owned an Acer Ferrari now for approximately 13 months". The problem is that in my quest to find 100% reproducible steps my warranty has expired by about 30 days.

I explained all of this to the guy on the phone at Acer. "If only more customers were like you!" he said, and then continued to tell me that I had to pay for the repair. After talking over me for a few minutes I finally lost my temper and swore at him, something I have never done before.

So, now I am really annoyed! I have an old £400 Hewlett Packard laptop that has been working (slowly) without problems for years now, yet I pay thousands for a laptop with a quality brand such as "Ferrari" and it breaks twice in 13 months! I'd expect the quality to be better, wouldn't you?

What *really* annoys me though is that Acer didn't send me a card telling me my warranty was nearly up and asking if I would like to extend it (which I would have done), and ofcourse I had no idea it was coming to an end because the receipt is.......surprise surprise it is with my accountant who is preparing last year's accounts!

Ferrari I am disappointed. You have put your name to a machine that has the body of an F1 and the inner workings of a cheap old banger!

Sprites

Someone sent me this on Skype this morning. I think it's really cool, it reminds me of the hardware sprites on the old Commodore 64!

01: Go to any site with lots of images (image search on google is a good one)
02: Once the images appear copy/paste this text into your address bar.

javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300; y4=200; x5=300; y5=200; DI=document.images; DIL=DI.length; function A(){for(i=0; i<DIL; i++){DIS=DI[ i ].style; DIS.position='absolute'; DIS.left=Math.sin(R*x1+i*x2+x3)*x4+x5; DIS.top=Math.cos(R*y1+i*y2+y3)*y4+y5}R++}setInterval('A()',5 ); void(0);

2007-05-01

!DEBUG != RELEASE

Okay, I'm sure lots of people know this but it caught me out so I'm going to write about it. I had to add some code to the Settings class in my PocketPC application recently. The code changed from this

public string FtpServer
{
get { return "liveserver.com"; }
}

to this

public string FtpServer
{
#if RELEASE
get { return "liveserver.com"; }
#else
get { return "testserver.com"; }
#endif
}

The idea was to allow the testers (using a DEBUG build) to synchronise data with the test server. Can you see the error?

The answer is this.....
"#if RELEASE" should have been "#if !DEBUG"

public string FtpServer
{
#if !DEBUG
get { return "liveserver.com"; }
#else
get { return "testserver.com"; }
#endif
}

I mistakenly thought that setting the build type in VS2005 to either Release or Debug would define RELEASE and DEBUG respectively, but it doesn't. If you look at the build options there is a checkbox "Define DEBUG constant". This is ticked in the Debug configuration and unchecked in the Release configuration. So the difference is

Debug = DEBUG is defined
Release = DEBUG is not defined

So, there you are :-)