Last active
August 29, 2015 14:22
-
-
Save phr34k/55f26202c61514b9588b to your computer and use it in GitHub Desktop.
relative to absolute path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdio.h> | |
#include <string> | |
#include <map> | |
void relative2absolute(std::string& result, const char* relativeFile) | |
{ | |
int state = 0; | |
int length = strlen(relativeFile); | |
result.reserve(length); | |
char* start = const_cast<char*>(relativeFile) + length + 1; | |
for( char* x = start - 1; x >= relativeFile; --x ) { | |
if( (*x == '/' || *x == '\\') || x == relativeFile) { | |
char* v; char* sep = x == relativeFile ? x : x + 1; | |
if( sep[0] == '.' && (sep[1] == 0 || sep[1] == '/' || sep[1] == '\\') ) { | |
start = x; | |
continue; | |
} else if( x[1] == '/' || x[1] == '\\' ) { | |
start = x; | |
continue; | |
} else if( sep[0] == '.' && sep[1] == '.' && (sep[2] == 0 || sep[2] == '/' || sep[2] == '\\') ) { | |
state++; //reference count the number of directories to go up | |
} | |
else | |
{ | |
if( state > 0 ) { | |
state--; //decrement reference count of directories to skip | |
} else { | |
if( x == relativeFile && ( *x == '\\' || *x == '/' ) ) { | |
result.insert(0, x + 1, start - x - 1); | |
} else if( *x == '\\' ) { | |
result.insert(0, x + 1, start - x - 1); | |
result.insert(0, "/"); | |
} else { | |
result.insert(0, x, start - x); | |
} | |
} | |
} | |
start = x; | |
} | |
} | |
} | |
void main() | |
{ | |
const char* relativeFile = "/sound//PCFemale_Voice00.gpk_000aebb6.ogg"; | |
std::string result; | |
relative2absolute(result, relativeFile); | |
printf("%s\r\n", result.c_str()); | |
getchar(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment