iFocus.Life News News - Breaking News & Top Stories - Latest World, US & Local News,Get the latest news, exclusives, sport, celebrities, showbiz, politics, business and lifestyle from The iFocus.Life,

Delphi Exercise: Create a Custom Delphi Function: ExtractBasePath

106 30


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

Challenge: ExtractBasePath

Your 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.
Note: the function should not raise any errors even if the provided paths do not exist.

For example: given the two paths "c:\code\examples\test.pas" and "c:\code\challenge.dfm", the ExtractBasePath should return "c:\code\"

Note: for the following two paths "c:\folder\code\file.pdf" and "c:\folder\comet\document.doc" the common base path is "c:\folder" and NOT "c:\folder\co". This means that the result must represent a folder / path name.

If the two paths provided do not have a common base path the function should return an empty string.

For example: "c:\folder\file.doc" and "d:\directory\document.pdf" do NOT have a common base path - as one file is on the C drive and another file is on the D drive.

Enter Challenge

Note: This is for glory only. About.com does not permit prizes to be given.
To "win" the challenge you obviously need to submit a function that solves the given problem.

Also, make sure the code is "nice", has no memory leaks and preferably does not have hundreds of lines.

Selected entries will be listed. A condition of entry is that you allow your source code to be published on this website, with full credits to you as the author.

ExtractBasePath : Closed!

Note: this challenge is closed. 12 valid entries have been submitted.

ExtractBasePath : Accepted Entries

Here's the list of the accepted entries. In total 12 entries were accepted before the challenge was closed.
Some implementations use TStringList, some pointers, some use iterating through paths one character at a time, some have less than 10 lines, some have more than 30 lines, one even uses assembler. Common to entries is the usage of the RTL routines.

Note: the list is in random order.

ExtractBasePath by Boris KumparExtractBasePath by Pablo AnizioExtractBasePath by Vlad ManExtractBasePath by Josip BrozovicExtractBasePath by Korhan TaþçýkarExtractBasePath by Jeff LawsonExtractBasePath by Ivan CvetkovicExtractBasePath by Paul BennettExtractBasePath by Caleb HattinghExtractBasePath by Ricardo de O. SoaresExtractBasePath by Antonio BakulaExtractBasePath by Jens BorrisholtParticipate in active Delphi programming challenges
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time
You might also like on "Technology"

Leave A Reply

Your email address will not be published.