5,276,156 members and growing! (19,152 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

Stack based path conversion.

By Boby Thomas P

The following article explains a stack based method to convert absolute path to relative and vice versa.
C++Win2K, WinXP, Windows, Visual Studio, Dev

Posted: 15 Apr 2006
Updated: 6 Apr 2007
Views: 21,136
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
39 votes for this Article.
Popularity: 6.02 Rating: 3.79 out of 5
5 votes, 12.8%
1
1 vote, 2.6%
2
1 vote, 2.6%
3
2 votes, 5.1%
4
30 votes, 76.9%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

The following article explains a simple algorithm to convert absolute path to relative and relative to absolute. Sample code also can be found along with the article. This code can be used free of cost but at your on risk. (Test the functions properly before using.)

The code works for windows as well as Linux. A description of the algorithm is given below.

Absolute path to relative path (Abs2Rel)

Abs2Rel path function takes absolute path as input and returns relative path.

//------------------------------------------------------------------------------

// Method        : Abs2Rel

// Description        : Convert absolute path to relative path.

// Parameter        : pcAbsPath - Input - Absolute path

// Parameter        : pcRelPath - Output - Relative path

// Parameter        : pcCurrDir - Input - Current Dir/Reference dir path

// Return        : Relative path

// Author        : Boby Thomas Pazheparampil April 2006

//------------------------------------------------------------------------------

char * Abs2Rel(char *pcAbsPath, char *pcRelPath, char* pcCurrDir)

Operation.

Following sequence will explain the conversion process.

For example consider the abs path "/cygdrive/d/boby/india/boby.txt", and reference directory "/cygdrive/d/try/path_conv"

Step 1: First extract the path parameters into stacks.

tmpStackAbsPath tmpStackCurrPath tmpStackOutput tmpMatchQueue

boby.txt

india

path_conv

boby

try

d

d

cygdrive

cygdrive

Step 2: Push the size difference of stacks into output stack. If the absolute path stack size is more, push the actual content of the stack "tmpStackAbsPath" into the stack "tmpStackOutput". If the size of the stack "tmpStackCurrPath" is more, add an ellipse ("..") into output path "pcRelPath".

tmpStackAbsPath tmpStackCurrPath tmpStackOutput tmpMatchQueue

india

path_conv

boby

try

d

d

cygdrive

cygdrive

boby.txt

Step 3: Compare the tops of stacks "tmpStackAbsPath" and "tmpStackCurrPath". If it is matching, add into queue for later possible use. (If there is a mismatch in lower layers of stack, this queue will be used). If it is not matching like in the example above, contents of queue will be pushed to output stack. After pushing queue to "tmpStackOutput", top of "tmpStackAbsPath" will be pushed to stack "tmpStackOutput". Also an ellipse will be added to "pcRelPath", the output relative path.

tmpStackAbsPath tmpStackCurrPath tmpStackOutput tmpMatchQueue

boby

try

d

d

india

cygdrive

cygdrive

boby.txt

pcRelPath = "..\"

Step 4: Repeat the same process till the end of stacks.

tmpStackAbsPath tmpStackCurrPath tmpStackOutput tmpMatchQueue