Jump to content
  • Advertisement
Sign in to follow this  
PumpkinPieman

Relative File Paths

This topic is 4833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been having quite a bit of issues with a program I'm developing which could be caused by the lack there of relative file path functions. Right now I'm developing a small animated sprite format for my engine. Each sprite file has a texture filename relative to the sprite file. So if the sprite path is "c:\sprite\spr.lyr" and the texture path is "c:\image\tex.png" I would be left with "..\image\tex.png" as the relative texture. I also need to be able to convert back the absolute image path given the relative texture path and the sprite path. Does anyone know functions that could simplify this process? Or am I going to have to write my way out of this one (I want to avoid this)?

Share this post


Link to post
Share on other sites
Advertisement
Well, you can always just browse their CVS repository and take just the code you need. The string manipulation boost does for manipulating directories is here.

Share this post


Link to post
Share on other sites
It's alright, thanks for the help though. I just made some quick and dirty code to get around that little problem I was having.

For anyone else looking for the same here's my code.
RelativePath.h

#pragma once

#include <windows.h>
#include <string.h>
#include <list>

#define SLASH '\\'

char* GetRelativeFile(LPCSTR currentdirectory, LPCSTR absolutefilename);
char* GetAbsoluteFile(LPCSTR currentdirectory, LPCSTR relativefilename);
void BreakFilepath(LPCSTR currentdirectory, std::list<std::string>* slist);




RelativePath.cpp

#include "./RelativePath.h"

char* GetRelativeFile(LPCSTR currentdirectory, LPCSTR absolutefilename)
{
std::list<std::string> dircur;
std::list<std::string> absfile;

std::string resultstr;

BreakFilepath(currentdirectory, &dircur);
BreakFilepath(absolutefilename, &absfile);

static char relativeFilename[MAX_FILENAME_LEN+1];

DWORD max = (DWORD)(dircur.size() <= absfile.size() ? dircur.size() : absfile.size());
DWORD min = 0;

std::list<std::string>::iterator idircur;
std::list<std::string>::iterator iabsfile;

for(DWORD i = 0; i < max; i++){
idircur = dircur.begin();
iabsfile = absfile.begin();
if(strcmp(idircur->c_str(), iabsfile->c_str()) == 0){
min++;
dircur.pop_front();
absfile.pop_front();
}else break;
}

for(idircur = dircur.begin(); idircur != dircur.end(); idircur++){
resultstr += "..\\";
}

std::list<std::string>::iterator iend = absfile.end();
iend--;
for(iabsfile = absfile.begin(); iabsfile != absfile.end(); iabsfile++){
resultstr += iabsfile->c_str();
if(iabsfile != iend)
resultstr += SLASH;
}

strcpy(relativeFilename, resultstr.c_str());

return relativeFilename;
};

char* GetAbsoluteFile(LPCSTR currentdirectory, LPCSTR relativefilename)
{
std::list<std::string> dircur;
std::list<std::string> relfile;

std::list<std::string>::iterator irelfile;
std::list<std::string>::iterator idircur;

std::string resultstr;

BreakFilepath(currentdirectory, &dircur);
BreakFilepath(relativefilename, &relfile);

static char relativeFilename[MAX_FILENAME_LEN+1];

DWORD dropdir = 0;

for(DWORD i = 0; i < relfile.size(); i++){
irelfile = relfile.begin();
if(strcmp(irelfile->c_str(), "..") == 0){
dircur.pop_back();
relfile.pop_front();
}else break;
}

std::list<std::string>::iterator iend = dircur.end();
iend--;
for(idircur = dircur.begin(); idircur != dircur.end(); idircur++){
resultstr += idircur->c_str();
if(idircur != iend)
resultstr += SLASH;
}


for(irelfile = relfile.begin(); irelfile != relfile.end(); irelfile++){
resultstr += SLASH;
resultstr += irelfile->c_str();
}

strcpy(relativeFilename, resultstr.c_str());

return relativeFilename;
};

void BreakFilepath(LPCSTR currentdirectory, std::list<std::string>* slist)
{
std::string curdir;

DWORD dirlen = (DWORD)strlen(currentdirectory);

for(DWORD i = 0; i <= dirlen; i++){
if(currentdirectory == SLASH){
if(curdir.size() > 0){
slist->push_back(curdir.c_str());
curdir.clear();
}
}else{
if(currentdirectory != 0x00)
curdir += currentdirectory;

if((i == dirlen) && (curdir.size() > 0)){
slist->push_back(curdir.c_str());
}
}
}
};


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!