1. Computing & Technology

Discuss in my forum

in Delphi Programming Challenges :: Delphi Programming Challenges Delphi programming challenges / exercises are designed to help you refactor your old code to make it more efficient; get new ideas on how to code faster / better Delphi code; help you solve a particular Delphi programming task and of course: to have fun while coding in Delphi :)

Your first challenge is to code a custom Delphi function with the following signature:

function ExtractBasePath(const path1, path2 : string) : string;

ExtractBasePath takes 2 path names (directory or file) and should return the base / common path for the paths provided.

Read the full article to join Delphi Programming Challenge: ExtractBasePath.

Related:

Comments
May 6, 2009 at 3:14 pm
(1) Rob Kennedy :

Haven’t you pretty much just asked for the PathCommonPrefix API function?

May 6, 2009 at 4:18 pm
(2) Zarko Gajic :

Rob,

Hm, … ok that would be “half-cheating” :) Anyway, join the challenge by creating the function using this api.

Just for notice: I would be much more happy with native Delphi code.

May 8, 2009 at 2:27 pm
(3) Jens Borrisholt :

Since the challange are closed I’ll post my code here :

function ExtractBasePath(const Path1, Path2: string): string;
var
CompareLength: Integer;
i: Integer;
P, Q: PChar;
begin
Result := ”;

asm //Determent the shortest string
mov eax, Path1
mov edx, Path2
test eax, edx //Test for nil string
jnz @NotNilString
mov esp, ebp
pop ebp
ret //restore registers and exit

@NotNilString:
mov ecx, [eax - 4]
cmp ecx, [edx - 4]
jle @Path2Shortest //Length(P1) > Length(P2)
mov ecx, [edx - 4]

@Path2Shortest:
mov CompareLength, ecx
end;

p := PChar(Path1);
q := PChar(Path2);

i := 1;
while i CSTR_EQUAL then
break
else
inc(i);

while (p[i] PathDelim) and (i > 0) do
Dec(i);

if i 0 then
SetString(Result, p, i + 1);
end;

May 8, 2009 at 2:39 pm
(4) Zarko Gajic :

Jens,

Yours was the last one accepted :)

May 9, 2009 at 1:21 am
(5) Jens Borrisholt :

Cool Zarko

Because something went wrong when I posted it …

Jens Borrisholt

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2012 About.com. All rights reserved.

A part of The New York Times Company.