/** * create_full_path: * @filename: a gchar * with the (relative or not) filename * @basedir: a gchar * with a basedir or NULL for current dir * * if filename is already absolute, it returns it * else it will use basedir if available, else the current dir * to add to the filename to form the full path * * for URL's it will simply return a strdup(), except for file:// URL's, * there the file:// bit is stripped and * IF YOU HAVE GNOME_VFS any %XX sequenves are converted * so if you DON'T have gnome_vfs, you should not feed file:// uri's!! * * it does use most_efficient_filename() to remote unwanted dir/../ entries * * Return value: a newly allocated gchar * with the full path **/ gchar *create_full_path(const gchar * filename, const gchar *basedir) { gchar *absolute_filename; gchar *tmpcdir; if (!filename) return NULL; DEBUG_MSG("create_full_path, filename=%s, basedir=%s\n", filename, basedir); #ifdef STRIP_FILE_URI if (strchr(filename, ':') != NULL) { /* it is an URI!! */ DEBUG_MSG("create_full_path, %s is an URI\n",filename); if (strncmp(filename, "file://", 7)==0) { #ifdef HAVE_GNOME_VFS return gnome_vfs_get_local_path_from_uri(filename); #else /* THIS IS A BUG, IF YOU DON'T HAVE GNOME_VFS BUT YOU DO HAVE GTK-2.4 A %21 OR SOMETHING LIKE THAT IS NOW NOT CONVERTED !!!!!!!!! */ #ifdef WIN32 return g_strdup(bf_strrepl(filename+7,"%20"," ")); #endif return g_strdup(filename+7); /* file:// URI's are never relative paths */ #endif } #ifdef WIN32 return g_strdup(bf_strrepl(filename,"%20"," ")); #endif return g_strdup(filename); /* cannot do this on remote paths */ } #endif /* HAVE_GNOME_VFS */ if (g_path_is_absolute(filename)) { absolute_filename = g_strdup(filename); } else { if (basedir) { tmpcdir = ending_slash(basedir); } else { gchar *curdir = g_get_current_dir(); tmpcdir = ending_slash(curdir); g_free(curdir); } absolute_filename = g_strconcat(tmpcdir, filename, NULL); g_free(tmpcdir); } absolute_filename = most_efficient_filename(absolute_filename); #ifdef WIN32 return g_strdup(bf_strrepl(absolute_filename,"%20"," ")); #endif return absolute_filename; }
gchar *create_full_path(gchar const * filename, gchar *basedir) { gchar *absolute_filename; gchar *tmpcdir; if (g_path_is_absolute(filename)) { absolute_filename = g_strdup(filename); } else { if (basedir) { tmpcdir = ending_slash(basedir); } else { gchar *curdir = g_get_current_dir(); tmpcdir = ending_slash(curdir); g_free(curdir); } absolute_filename = g_strconcat(tmpcdir, filename, NULL); g_free(tmpcdir); } absolute_filename = most_efficient_filename(absolute_filename); return absolute_filename; }
gchar *create_relative_link_to(gchar * current_filepath, gchar * link_to_filepath) { gchar *returnstring = NULL; gchar *eff_current_filepath, *eff_link_to_filepath; gint common_lenght, maxcommonlen; gint current_filename_length, link_to_filename_length, current_dirname_length, link_to_dirname_length; gint count, deeper_dirs; if (!current_filepath){ returnstring = most_efficient_filename(g_strdup(link_to_filepath)); return returnstring; } if (!link_to_filepath) { return NULL; } eff_current_filepath = most_efficient_filename(g_strdup(current_filepath)); eff_link_to_filepath = most_efficient_filename(g_strdup(link_to_filepath)); current_filename_length = strlen(strrchr(eff_current_filepath, G_DIR_SEPARATOR))-1; link_to_filename_length = strlen(strrchr(eff_link_to_filepath, G_DIR_SEPARATOR))-1; current_dirname_length = strlen(eff_current_filepath) - current_filename_length; link_to_dirname_length = strlen(eff_link_to_filepath) - link_to_filename_length; if (current_dirname_length < link_to_dirname_length) { maxcommonlen = current_dirname_length; } else { maxcommonlen = link_to_dirname_length; } common_lenght = 0; while ((strncmp(eff_current_filepath, eff_link_to_filepath, common_lenght + 1)) == 0) { common_lenght++; if (common_lenght >= maxcommonlen) { common_lenght = maxcommonlen; break; } } if (eff_current_filepath[common_lenght] != G_DIR_SEPARATOR) { gchar *ltmp = &eff_current_filepath[common_lenght]; while ((*ltmp != G_DIR_SEPARATOR) && (common_lenght > 0)) { common_lenght--; ltmp--; } } deeper_dirs = 0; for (count = common_lenght+1; count <= current_dirname_length; count++) { if (eff_current_filepath[count] == G_DIR_SEPARATOR) { deeper_dirs++; } } returnstring = g_malloc0((strlen(link_to_filepath) - common_lenght + 3 * deeper_dirs + 1) * sizeof(gchar *)); count = deeper_dirs; while (count) { strcat(returnstring, "../"); count--; } strcat(returnstring, &eff_link_to_filepath[common_lenght+1]); g_free(eff_current_filepath); g_free(eff_link_to_filepath); return returnstring; }
/** * create_relative_link_to: * @current_filepath: a #gchar * with the current filename * @link_to_filepath: a #gchar * with a file to link to * * creates a newly allocated relative link from current_filepath * to link_to_filepath * * if current_filepath == NULL it returns the most efficient filepath * for link_to_filepath * * if link_to_filepath == NULL it will return NULL * * Return value: a newly allocated gchar * with the relative link **/ gchar *create_relative_link_to(gchar * current_filepath, gchar * link_to_filepath) { gchar *returnstring = NULL; gchar *eff_current_filepath, *eff_link_to_filepath; gint common_lenght, maxcommonlen; gint current_filename_length, link_to_filename_length, current_dirname_length, link_to_dirname_length; gint count, deeper_dirs; gchar *temp, *temp2; if (!current_filepath){ returnstring = most_efficient_filename(g_strdup(link_to_filepath)); return returnstring; } if (!link_to_filepath) { return NULL; } eff_current_filepath = most_efficient_filename(g_strdup(current_filepath)); eff_link_to_filepath = most_efficient_filename(g_strdup(link_to_filepath)); DEBUG_MSG("eff_current: '%s'\n",eff_current_filepath); DEBUG_MSG("eff_link_to: '%s'\n",eff_link_to_filepath); /* get the size of the directory of the current_filename */ #ifdef WIN32 temp = strrchr(eff_current_filepath,DIRCHR); if (temp == NULL){temp = strrchr(eff_current_filepath,'/');} current_filename_length = (temp == NULL ? 0 : strlen(temp)-1); temp2 = strrchr(eff_link_to_filepath, DIRCHR); if (temp2 == NULL){temp2 = strrchr(eff_link_to_filepath, '/');} link_to_filename_length = (temp2 == NULL ? 0 : strlen(temp2)-1); #else current_filename_length = strlen(strrchr(eff_current_filepath, DIRCHR))-1; link_to_filename_length = strlen(strrchr(eff_link_to_filepath, DIRCHR))-1; #endif DEBUG_MSG("create_relative_link_to, filenames: current: %d, link_to:%d\n", current_filename_length,link_to_filename_length); current_dirname_length = strlen(eff_current_filepath) - current_filename_length; link_to_dirname_length = strlen(eff_link_to_filepath) - link_to_filename_length; DEBUG_MSG("create_relative_link_to, dir's: current: %d, link_to:%d\n", current_dirname_length, link_to_dirname_length); if (current_dirname_length < link_to_dirname_length) { maxcommonlen = current_dirname_length; } else { maxcommonlen = link_to_dirname_length; } /* first lets get the common basedir for both dir+file by comparing the common path in the directories */ common_lenght = 0; while ((strncmp(eff_current_filepath, eff_link_to_filepath, common_lenght + 1)) == 0) { common_lenght++; if (common_lenght >= maxcommonlen) { common_lenght = maxcommonlen; break; } } DEBUG_MSG("create_relative_link_to, common_lenght=%d (not checked for directory)\n", common_lenght); /* this is the common length, but we need the common directories */ if (eff_current_filepath[common_lenght] != DIRCHR) { gchar *ltmp = &eff_current_filepath[common_lenght]; while ((*ltmp != DIRCHR) && (common_lenght > 0)) { common_lenght--; ltmp--; } } DEBUG_MSG("create_relative_link_to, common_lenght=%d (checked for directory)\n", common_lenght); /* now we need to count how much deeper (in directories) the current_filename is compared to the link_to_file, that is the amount of ../ we need to add */ deeper_dirs = 0; for (count = common_lenght+1; count <= current_dirname_length; count++) { if (eff_current_filepath[count] == DIRCHR) { deeper_dirs++; DEBUG_MSG("create_relative_link_to, on count=%d, deeper_dirs=%d\n", count, deeper_dirs); } } DEBUG_MSG("create_relative_link_to, deeper_dirs=%d\n", deeper_dirs); /* now we know everything we need to know we can create the relative link */ returnstring = g_malloc0((strlen(link_to_filepath) - common_lenght + 3 * deeper_dirs + 1) * sizeof(gchar *)); count = deeper_dirs; while (count) { strcat(returnstring, "../"); count--; } strcat(returnstring, &eff_link_to_filepath[common_lenght+1]); DEBUG_MSG("create_relative_link_to, returnstring=%s\n", returnstring); g_free(temp); g_free(temp2); g_free(eff_current_filepath); g_free(eff_link_to_filepath); return returnstring; }