Commit 9fe2cb47 authored by Olivier Lantsoght's avatar Olivier Lantsoght
Browse files

[PATH] New function to concatenate string into new memory with memory...

[PATH] New function to concatenate string into new memory with memory allocation and get relative path between path.
parent 4f5fe946
......@@ -273,3 +273,94 @@ char* mbs_getcwd(){
#endif
}
char* mbs_get_relative_path(const char* origin, const char* destination){
char *w_origin = strdup(origin);
char *w_destination = strdup(destination);
// replace '\' by '/' if any (i.e. win compatibility)
char* c;
for (c = w_origin; *c != '\0'; c++) {
if (*c == '\\') {
*c = '/';
}
}
for (c = w_destination; *c != '\0'; c++) {
if (*c == '\\') {
*c = '/';
}
}
// Remove final separator of origin, if any
c = &w_origin[strlen(w_origin) - 1];
if (*c =='/'){
*c = '\0';
}
// Check if path are identical
if (!strcmp(w_origin, w_destination)){
free(w_origin);
return w_destination;
}
// Get the first different character
int index, index_difference = 0;
int size = strlen(w_origin) < strlen(w_destination) ? strlen(w_origin) : strlen(w_destination);
for (index = 0; index < size; index++){
if(w_origin[index] != w_destination[index]){
index_difference = index;
break;
}
}
if (index_difference == 0){
// No commun path
free(w_origin);
return w_destination;
}
// Compute the required size
int required_size = 1; // null terminating string
int nb_rewind = 0; // Number of '../' to write
// To rewind the origin path to commun path
if (w_origin[index_difference] != '\0'){
// It has been ensured the path is not ending with '/'.
// So if we have to go back at least the final folder that will not be
// spotted by next test.
required_size += 3;
nb_rewind += 1;
// Now it counts additionnal folders to rewind
for (c = &w_origin[index_difference]; *c != '\0'; c++) {
if (*c == '/') {
required_size += 3; // must write '../' for each directory
nb_rewind += 1;
}
}
}
// To go to en of provided destination
required_size += strlen(&w_destination[index_difference]);
// Create and fill relative path
char* rel_path = (char *)malloc(required_size * sizeof(char));
rel_path[0] = '\0';
int i;
for(i=0 ; i < nb_rewind; i++){
strcat(rel_path, "../"); // Write in place so no need to capture output.
}
strcat(rel_path, &w_destination[index_difference]);
free(w_origin);
free(w_destination);
return rel_path;
}
char* mbs_strcat(const char* prefix, const char* suffix){
int lenght = strlen(prefix) + strlen(suffix) + 1;
char* cat = (char*) malloc(lenght * sizeof(char));
cat[0] = '\0';
cat = strcat(cat, prefix);
cat = strcat(cat, suffix);
return cat;
}
......@@ -76,4 +76,30 @@ char* mbs_dirname(const char* path);
* NULL in case of failure.
*/
char* mbs_getcwd();
/**
* \brief mbs_get_relative_path Returns the relative path between two absolute path.
*
* If the paths have nothing in common, a copy of the destination path is returned.
* The returned path use the "/" directory separator and omit the initial "./".
*
* \param origin The origin path, this path cannot point to a file.
* \param destination The destination path.
* \return A newly allocated pointer containing the relative path between the two path.
* If the destination path is a file, the returned path will point to this file.
* The terminating separator (final "/") is included if it was included in the destination path.
*/
char* mbs_get_relative_path(const char* origin, const char* destination);
/**
* \brief mbs_strcat Concatenate the two string in a new memory space.
*
* \param prefix[in] First chain of characters.
* \param suffix[in] Second chain of characters.
*
* \return Newly allocated string containing the concatenation of both parameters.
*/
char* mbs_strcat(const char* prefix, const char* suffix);
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment