in 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 :)
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:


Haven’t you pretty much just asked for the PathCommonPrefix API function?
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.
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;
Jens,
Yours was the last one accepted
Cool Zarko
Because something went wrong when I posted it …
Jens Borrisholt