Click here to Skip to main content
15,867,885 members
Articles / Programming Languages / C++
Article

Stack based path conversion.

Rate me:
Please Sign up or sign in to vote.
3.79/5 (39 votes)
6 Apr 200712 min read 53.4K   741   35   14
The following article explains a stack based method to convert absolute path to relative and vice versa.

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.<o:p>

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"

<o:p>

Step 1: First extract the path parameters into stacks.

tmpStackAbsPath tmpStackCurrPath tmpStackOutput tmpMatchQueue

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

boby.txt

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

india

<o:p>

path_conv

<o:p>

<o:p>

<o:p>

<o:p>

boby

<o:p>

try

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

d

<o:p>

<o:p>

<o:p>

<o:p>

cygdrive

<o:p>

cygdrive

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

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".<o:p>

tmpStackAbsPath tmpStackCurrPath tmpStackOutput tmpMatchQueue

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

india

<o:p>

path_conv

<o:p>

<o:p>

<o:p>

<o:p>

boby

<o:p>

try

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

d

<o:p>

<o:p>

<o:p>

<o:p>

cygdrive

<o:p>

cygdrive

<o:p>

boby.txt

<o:p>

<o:p>

<o:p>

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.

<o:p>

tmpStackAbsPath tmpStackCurrPath tmpStackOutput tmpMatchQueue

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

boby

<o:p>

try

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

d

<o:p>

india

<o:p>

<o:p>

cygdrive

<o:p>

cygdrive

<o:p>

boby.txt

<o:p>

<o:p>

<o:p>

pcRelPath = "..\"<o:p>

<o:p>

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

<o:p>

tmpStackAbsPath tmpStackCurrPath tmpStackOutput tmpMatchQueue

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

boby

<o:p>

<o:p>

D

<o:p>

d

<o:p>

india

<o:p>

<o:p>

cygdrive

<o:p>

cygdrive

<o:p>

boby.txt

<o:p>

<o:p>

<o:p>

pcRelPath = "..\..\"

<o:p>

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

<o:p>

tmpStackAbsPath tmpStackCurrPath tmpStackOutput tmpMatchQueue

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

boby

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

india

<o:p>

<o:p>

cygdrive

<o:p>

cygdrive

<o:p>

boby.txt

<o:p>

<o:p>

<o:p>

pcRelPath = "..\..\"

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

tmpStackAbsPath tmpStackCurrPath tmpStackOutput tmpMatchQueue

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

cygdrive

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

boby

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

india

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

boby.txt

<o:p>

<o:p>

<o:p>

pcRelPath = "..\..\"<o:p>

Step 7: Once the "tmpStackAbsPath" and "tmpStackCurrPath" are exhausted, move the content of "tmpStackOutput" to the output string pcRelPath .

tmpStackAbsPath tmpStackCurrPath tmpStackOutput tmpMatchQueue

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

cygdrive

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

india

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

boby.txt

<o:p>

<o:p>

<o:p>

pcRelPath = "..\..\boby\"

<o:p>

Step 8: Once the "tmpStackAbsPath" and "tmpStackCurrPath" are exhausted, move the content of "tmpStackOutput" to the output string pcRelPath .

<o:p>

tmpStackAbsPath tmpStackCurrPath tmpStackOutput tmpMatchQueue

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

cygdrive

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

boby.txt

<o:p>

<o:p>

<o:p>

pcRelPath = "..\..\boby\India\"<o:p>

Step 9: Continue the process till the "tmpStackOutput" exhausts.

<o:p>

tmpStackAbsPath tmpStackCurrPath tmpStackOutput tmpMatchQueue

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

cygdrive

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

pcRelPath = "..\..\boby\india\ boby.txt"<o:p>

<o:p>

<o:p>

Relative path to absolute path (Rel2Abs)

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

//------------------------------------------------------------------------------
// Method        : Rel2Abs
// Description    : Convert absolute path to relative path.
// Parameter    : pcRelPath - Input - Relative path
// Parameter    : pcAbsPath - Output - Absolute path
// Parameter    : pcCurrDir - Input - Current Dir/Reference dir path
// Return        : Absolute path
// Author        : Boby Thomas Pazheparampil April 2006
//------------------------------------------------------------------------------

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

Operation.<o:p>

Following sequence will explain the conversion process.

For example consider the relative path "../../boby/../temp.txt" and reference directory "/cygdrive/d/try/path_conv"

<o:p>

Step 1: First extract the relative path into queue "tmpQueueRelPath".

tmpQueueRelPath tmpStackCurrPath tmpStackOutput

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

temp.txt

<o:p>

<o:p>

<o:p>

<o:p>

..

<o:p>

<o:p>

<o:p>

<o:p>

boby

<o:p>

<o:p>

<o:p>

<o:p>

..

<o:p>

<o:p>

<o:p>

<o:p>

..

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

Step 2: Then convert the reference path into stack "tmpStackCurrPath".

tmpQueueRelPath tmpStackCurrPath tmpStackOutput

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

temp.txt

<o:p>

<o:p>

<o:p>

<o:p>

..

<o:p>

<o:p>

<o:p>

<o:p>

boby

<o:p>

<o:p>

<o:p>

<o:p>

..

<o:p>

path_conv

<o:p>

<o:p>

..

<o:p>

try

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

<o:p>

<o:p>

<o:p>

cygdrive

<o:p>

<o:p>

<o:p>

Step 3: Take entries from the front of the queue "tmpQueueRelPath". If it is an ellipse, pop out the last entry in the stack "tmpStackCurrPath". If the front entry in the queue "tmpQueueRelPath" is not ellipse, push the entry into stack "tmpStackCurrPath".

tmpQueueRelPath tmpStackCurrPath tmpStackOutput

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

temp.txt

<o:p>

<o:p>

<o:p>

<o:p>

..

<o:p>

<o:p>

<o:p>

<o:p>

boby

<o:p>

<o:p>

<o:p>

<o:p>

..

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

try

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

<o:p>

<o:p>

<o:p>

cygdrive

<o:p>

<o:p>

<o:p>

Step 4: Continue the same process until the queue "tmpQueueRelPath" exhausts.

tmpQueueRelPath tmpStackCurrPath tmpStackOutput

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

temp.txt

<o:p>

<o:p>

<o:p>

<o:p>

..

<o:p>

<o:p>

<o:p>

<o:p>

boby

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

<o:p>

<o:p>

<o:p>

cygdrive

<o:p>

<o:p>

<o:p>

Step 5: Continue the same process until the queue "tmpQueueRelPath" exhausts.

tmpQueueRelPath tmpStackCurrPath tmpStackOutput

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

temp.txt

<o:p>

<o:p>

<o:p>

<o:p>

..

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

boby

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

<o:p>

<o:p>

<o:p>

cygdrive

<o:p>

<o:p>

<o:p> Step 6: Continue the same process until the queue "tmpQueueRelPath" exhausts.

tmpQueueRelPath tmpStackCurrPath tmpStackOutput

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

temp.txt

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

<o:p>

<o:p>

<o:p>

cygdrive

<o:p>

<o:p>

<o:p>

Step 7: Continue the same process until the queue "tmpQueueRelPath" exhausts.

tmpQueueRelPath tmpStackCurrPath tmpStackOutput

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

temp.txt

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

<o:p>

<o:p>

<o:p>

cygdrive

<o:p>

<o:p>

<o:p>

Step 8: Once the queue "tmpQueueRelPath" exhausts, pop the content of the stack "tmpStackCurrPath" and push it to the stack "tmpStackOutput".

tmpQueueRelPath tmpStackCurrPath tmpStackOutput

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

<o:p>

<o:p>

<o:p>

cygdrive

<o:p>

temp.txt

<o:p> Step 9: Repeat the process till the stack "tmpStackCurrPath" exhausts.

tmpQueueRelPath tmpStackCurrPath tmpStackOutput

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

<o:p>

cygdrive

<o:p>

temp.txt

<o:p>

<o:p>

Step 10: Repeat the process till the stack "tmpStackCurrPath" exhausts.

tmpQueueRelPath tmpStackCurrPath tmpStackOutput

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

cygdrive

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

<o:p>

<o:p>

<o:p>

temp.txt

<o:p>

Step 11: Once the stack "tmpStackCurrPath" is empty, take the content into absolute return path string.

tmpQueueRelPath tmpStackCurrPath tmpStackOutput

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

d

<o:p>

<o:p>

<o:p>

<o:p>

temp.txt

<o:p>

pcAbsPath = "/cygdrive/"<o:p>

<o:p> <o:p>

Step 12: Repeat the process till stack "tmpStackOutput" is empty.

tmpQueueRelPath tmpStackCurrPath tmpStackOutput

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

temp.txt

<o:p>

pcAbsPath = "/cygdrive/d/"<o:p>

<o:p>

Step 13: Repeat the process till stack "tmpStackOutput" is empty.

tmpQueueRelPath tmpStackCurrPath tmpStackOutput

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

<o:p>

pcAbsPath = "/cygdrive/d/ temp.txt"<o:p>

Summary

We can think of lots of different methods for the above implementation. Feedback about this method will be greatly appreciated. Contact me at bobypt@gmail.com.

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

boby

 <o:p>

 <o:p>

D

 <o:p>

d

 <o:p>

india

 <o:p>

 <o:p>

cygdrive

 <o:p>

cygdrive

 <o:p>

boby.txt

 <o:p>

 <o:p>

 <o:p>

pcRelPath  = “..\..\”

 <o:p>

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

 <o:p>

tmpStackAbsPath                tmpStackCurrPath                  tmpStackOutput                    tmpMatchQueue

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

d

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

boby

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

india

 <o:p>

 <o:p>

cygdrive

 <o:p>

cygdrive

 <o:p>

boby.txt

 <o:p>

 <o:p>

 <o:p>

pcRelPath  = “..\..\”

 

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

tmpStackAbsPath                tmpStackCurrPath                  tmpStackOutput                    tmpMatchQueue

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

cygdrive

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

d

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

boby

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

india

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

boby.txt

 <o:p>

 <o:p>

 <o:p>

pcRelPath  = “..\..\”<o:p>

 

Step 7: Once the “tmpStackAbsPath” and “tmpStackCurrPath” are exhausted, move the content of “tmpStackOutput” to the output string pcRelPath .

tmpStackAbsPath                tmpStackCurrPath                  tmpStackOutput                    tmpMatchQueue

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

cygdrive

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

d

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

india

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

boby.txt

 <o:p>

 <o:p>

 <o:p>

pcRelPath  = “..\..\boby\”

 <o:p>

Step 8: Once the “tmpStackAbsPath” and “tmpStackCurrPath” are exhausted, move the content of “tmpStackOutput” to the output string pcRelPath .

 <o:p>

tmpStackAbsPath                tmpStackCurrPath                  tmpStackOutput                    tmpMatchQueue

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

cygdrive

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

d

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

boby.txt

 <o:p>

 <o:p>

 <o:p>

pcRelPath  = “..\..\boby\India\”<o:p>

Step 9: Continue the process till the “tmpStackOutput” exhausts.

 <o:p>

tmpStackAbsPath                tmpStackCurrPath                  tmpStackOutput                    tmpMatchQueue

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

cygdrive

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

d

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

pcRelPath  = “..\..\boby\india\ boby.txt”<o:p>

 <o:p>

 <o:p>

Relative path to absolute path (Rel2Abs)

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

//------------------------------------------------------------------------------
// Method        : Rel2Abs
// Description    : Convert absolute path to relative path.
// Parameter    : pcRelPath - Input - Relative path
// Parameter    : pcAbsPath - Output - Absolute path
// Parameter    : pcCurrDir - Input - Current Dir/Reference dir path
// Return        : Absolute path
// Author        : Boby Thomas Pazheparampil April 2006
//------------------------------------------------------------------------------

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

Operation.<o:p>

            Following sequence will explain the conversion process.

For example consider the relative path “../../boby/../temp.txt" and reference directory “/cygdrive/d/try/path_conv”

 <o:p>

Step 1: First extract the relative path into queue “tmpQueueRelPath”.

                  tmpQueueRelPath              tmpStackCurrPath                   tmpStackOutput            

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

temp.txt

 <o:p>

 <o:p>

 <o:p>

 <o:p>

..

 <o:p>

 <o:p>

 <o:p>

 <o:p>

boby

 <o:p>

 <o:p>

 <o:p>

 <o:p>

..

 <o:p>

 <o:p>

 <o:p>

 <o:p>

..

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

Step 2: Then convert the reference path into stack “tmpStackCurrPath”.

                  tmpQueueRelPath              tmpStackCurrPath                   tmpStackOutput                  

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

temp.txt

 <o:p>

 <o:p>

 <o:p>

 <o:p>

..

 <o:p>

 <o:p>

 <o:p>

 <o:p>

boby

 <o:p>

 <o:p>

 <o:p>

 <o:p>

..

 <o:p>

path_conv

 <o:p>

 <o:p>

..

 <o:p>

try

 <o:p>

 <o:p>

 <o:p>

 <o:p>

d

 <o:p>

 <o:p>

 <o:p>

 <o:p>

cygdrive

 <o:p>

 <o:p>

 <o:p>

Step 3: Take entries from the front of the queue “tmpQueueRelPath”. If it is an ellipse, pop out the last entry in the stack “tmpStackCurrPath”. If the front entry in the queue “tmpQueueRelPath” is not ellipse, push the entry into stack “tmpStackCurrPath”.

                  tmpQueueRelPath              tmpStackCurrPath                   tmpStackOutput                       

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

temp.txt

 <o:p>

 <o:p>

 <o:p>

 <o:p>

..

 <o:p>

 <o:p>

 <o:p>

 <o:p>

boby

 <o:p>

 <o:p>

 <o:p>

 <o:p>

..

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

try

 <o:p>

 <o:p>

 <o:p>

 <o:p>

d

 <o:p>

 <o:p>

 <o:p>

 <o:p>

cygdrive

 <o:p>

 <o:p>

 <o:p>

Step 4: Continue the same process until the queue “tmpQueueRelPath” exhausts.

                  tmpQueueRelPath              tmpStackCurrPath                   tmpStackOutput            

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

temp.txt

 <o:p>

 <o:p>

 <o:p>

 <o:p>

..

 <o:p>

 <o:p>

 <o:p>

 <o:p>

boby

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

d

 <o:p>

 <o:p>

 <o:p>

 <o:p>

cygdrive

 <o:p>

 <o:p>

 <o:p>

Step 5: Continue the same process until the queue “tmpQueueRelPath” exhausts.

                  tmpQueueRelPath              tmpStackCurrPath                   tmpStackOutput             

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

temp.txt

 <o:p>

 <o:p>

 <o:p>

 <o:p>

..

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

boby

 <o:p>

 <o:p>

 <o:p>

 <o:p>

d

 <o:p>

 <o:p>

 <o:p>

 <o:p>

cygdrive

 <o:p>

 <o:p>

 <o:p> Step 6: Continue the same process until the queue “tmpQueueRelPath” exhausts.

                  tmpQueueRelPath              tmpStackCurrPath                   tmpStackOutput                       

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

temp.txt

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

d

 <o:p>

 <o:p>

 <o:p>

 <o:p>

cygdrive

 <o:p>

 <o:p>

 <o:p>

Step 7: Continue the same process until the queue “tmpQueueRelPath” exhausts.

                  tmpQueueRelPath              tmpStackCurrPath                   tmpStackOutput                 

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

temp.txt

 <o:p>

 <o:p>

 <o:p>

 <o:p>

d

 <o:p>

 <o:p>

 <o:p>

 <o:p>

cygdrive

 <o:p>

 <o:p>

 <o:p>

Step 8: Once the queue “tmpQueueRelPath” exhausts, pop the content of the stack “tmpStackCurrPath” and push it to the stack “tmpStackOutput”.

                  tmpQueueRelPath              tmpStackCurrPath                   tmpStackOutput            

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

d

 <o:p>

 <o:p>

 <o:p>

 <o:p>

cygdrive

 <o:p>

temp.txt

 <o:p> Step 9: Repeat the process till the stack “tmpStackCurrPath” exhausts.

                  tmpQueueRelPath              tmpStackCurrPath                   tmpStackOutput                    

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

d

 <o:p>

 <o:p>

cygdrive

 <o:p>

temp.txt

 <o:p>

 <o:p>

Step 10: Repeat the process till the stack “tmpStackCurrPath” exhausts.

                  tmpQueueRelPath              tmpStackCurrPath                   tmpStackOutput                    

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

cygdrive

 <o:p>

 <o:p>

 <o:p>

 <o:p>

d

 <o:p>

 <o:p>

 <o:p>

 <o:p>

temp.txt

 <o:p>

Step 11: Once the stack “tmpStackCurrPath” is empty, take the content into absolute return path string.

                  tmpQueueRelPath              tmpStackCurrPath                   tmpStackOutput            

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

d

 <o:p>

 <o:p>

 <o:p>

 <o:p>

temp.txt

 <o:p>

pcAbsPath = “/cygdrive/”<o:p>

 <o:p> <o:p>

Step 12: Repeat the process till stack “tmpStackOutput” is empty.

                  tmpQueueRelPath              tmpStackCurrPath                   tmpStackOutput               

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

temp.txt

 <o:p>

pcAbsPath = “/cygdrive/d/”<o:p>

 <o:p>

Step 13: Repeat the process till stack “tmpStackOutput” is empty.

                  tmpQueueRelPath              tmpStackCurrPath                   tmpStackOutput                    

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

pcAbsPath = “/cygdrive/d/ temp.txt”<o:p>  

 

Summary

    We can think of lots of different methods for the above implementation. Feedback about this method will be greatly appreciated. Contact me at bobypt@gmail.com. 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) DWS
Australia Australia

Comments and Discussions

 
Generalnice logic Pin
ismail2315-Apr-06 21:45
ismail2315-Apr-06 21:45 
GeneralNotes Pin
YoSilver15-Apr-06 10:33
YoSilver15-Apr-06 10:33 
GeneralRe: Notes Pin
Boby Thomas P15-Apr-06 21:21
Boby Thomas P15-Apr-06 21:21 
Generalgreat idea Pin
gomas_1115-Apr-06 7:57
gomas_1115-Apr-06 7:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.