From 66f6e9eff5c2b9f0d5c0bcffcfbc93bf2edaa7b3 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Thu, 17 Mar 2011 11:26:30 +0000 Subject: Rename utils/resource to utils/filepath to avoid confusion with resource: fetcher. svn path=/trunk/netsurf/; revision=12088 --- utils/filepath.c | 310 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils/filepath.h | 116 +++++++++++++++++++++ utils/resource.c | 310 ------------------------------------------------------- utils/resource.h | 107 ------------------- 4 files changed, 426 insertions(+), 417 deletions(-) create mode 100644 utils/filepath.c create mode 100644 utils/filepath.h delete mode 100644 utils/resource.c delete mode 100644 utils/resource.h (limited to 'utils') diff --git a/utils/filepath.c b/utils/filepath.c new file mode 100644 index 000000000..e2d90bbb7 --- /dev/null +++ b/utils/filepath.c @@ -0,0 +1,310 @@ +/* + * Copyright 2010 Vincent Sanders + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** \file + * Provides utility functions for finding readable files. + * + * These functions are intended to make finding resource files more straightforward. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "utils/config.h" +#include "utils/filepath.h" + +/** maximum number of elements in the resource vector */ +#define MAX_RESPATH 128 + +/* exported interface documented in filepath.h */ +char *filepath_vsfindfile(char *str, const char *format, va_list ap) +{ + char *realpathname; + char *pathname; + int len; + + pathname = malloc(PATH_MAX); + if (pathname == NULL) + return NULL; /* unable to allocate memory */ + + len = vsnprintf(pathname, PATH_MAX, format, ap); + + if ((len < 0) || (len >= PATH_MAX)) { + /* error or output exceeded PATH_MAX length so + * operation is doomed to fail. + */ + free(pathname); + return NULL; + } + + realpathname = realpath(pathname, str); + + free(pathname); + + if (realpathname != NULL) { + /* sucessfully expanded pathname */ + if (access(realpathname, R_OK) != 0) { + /* unable to read the file */ + return NULL; + } + } + + return realpathname; +} + +/* exported interface documented in filepath.h */ +char *filepath_sfindfile(char *str, const char *format, ...) +{ + va_list ap; + char *ret; + + va_start(ap, format); + ret = filepath_vsfindfile(str, format, ap); + va_end(ap); + + return ret; +} + +/* exported interface documented in filepath.h */ +char *filepath_findfile(const char *format, ...) +{ + char *str; + char *ret; + va_list ap; + + str = malloc(PATH_MAX); + if (str == NULL) + return NULL; /* unable to allocate memory */ + + va_start(ap, format); + ret = filepath_vsfindfile(str, format, ap); + va_end(ap); + + if (ret == NULL) + free(str); + + return ret; +} + +/* exported interface documented in filepath.h */ +char *filepath_sfind(char **respathv, char *filepath, const char *filename) +{ + int respathc = 0; + + if ((respathv == NULL) || (respathv[0] == NULL) || (filepath == NULL)) + return NULL; + + while (respathv[respathc] != NULL) { + if (filepath_sfindfile(filepath, "%s/%s", respathv[respathc], filename) != NULL) + return filepath; + + respathc++; + } + + return NULL; +} + +/* exported interface documented in filepath.h */ +char *filepath_find(char **respathv, const char *filename) +{ + char *ret; + char *filepath; + + if ((respathv == NULL) || (respathv[0] == NULL)) + return NULL; + + filepath = malloc(PATH_MAX); + if (filepath == NULL) + return NULL; + + ret = filepath_sfind(respathv, filepath, filename); + + if (ret == NULL) + free(filepath); + + return ret; +} + +/* exported interface documented in filepath.h */ +char *filepath_sfinddef(char **respathv, char *filepath, const char *filename, const char *def) +{ + char t[PATH_MAX]; + char *ret; + + if ((respathv == NULL) || (respathv[0] == NULL) || (filepath == NULL)) + return NULL; + + ret = filepath_sfind(respathv, filepath, filename); + + if ((ret == NULL) && (def != NULL)) { + /* search failed, return the path specified */ + ret = filepath; + if (def[0] == '~') { + snprintf(t, PATH_MAX, "%s/%s/%s", getenv("HOME"), def + 1, filename); + } else { + snprintf(t, PATH_MAX, "%s/%s", def, filename); + } + if (realpath(t, ret) == NULL) { + strcpy(ret, t); + } + + } + return ret; +} + + +/* exported interface documented in filepath.h */ +char ** +filepath_generate(char * const *pathv, const char * const *langv) +{ + char **respath; /* resource paths vector */ + int pathc = 0; + int langc = 0; + int respathc = 0; + struct stat dstat; + char tmppath[PATH_MAX]; + + respath = calloc(MAX_RESPATH, sizeof(char *)); + + while (pathv[pathc] != NULL) { + if ((stat(pathv[pathc], &dstat) == 0) && + S_ISDIR(dstat.st_mode)) { + /* path element exists and is a directory */ + langc = 0; + while (langv[langc] != NULL) { + snprintf(tmppath, sizeof tmppath, "%s/%s", pathv[pathc],langv[langc]); + if ((stat(tmppath, &dstat) == 0) && + S_ISDIR(dstat.st_mode)) { + /* path element exists and is a directory */ + respath[respathc++] = strdup(tmppath); + } + langc++; + } + respath[respathc++] = strdup(pathv[pathc]); + } + pathc++; + } + + return respath; +} + +/* expand ${} in a string into environment variables */ +static char * +expand_path(const char *path) +{ + char *exp = strdup(path); + int explen; + int cstart = -1; + int cloop = 0; + char *envv; + int envlen; + int replen; /* length of replacement */ + + if (exp == NULL) + return NULL; + + explen = strlen(exp) + 1; + + while (exp[cloop] != 0) { + if ((exp[cloop] == '$') && + (exp[cloop + 1] == '{')) { + cstart = cloop; + cloop++; + } + + if ((cstart != -1) && + (exp[cloop] == '}')) { + replen = cloop - cstart; + exp[cloop] = 0; + envv = getenv(exp + cstart + 2); + if (envv == NULL) { + memmove(exp + cstart, + exp + cloop + 1, + explen - cloop - 1); + explen -= replen; + } else { + envlen = strlen(envv); + exp = realloc(exp, explen + envlen - replen); + memmove(exp + cstart + envlen, + exp + cloop + 1, + explen - cloop - 1); + memmove(exp + cstart, envv, envlen); + explen += envlen - replen; + } + cloop -= replen; + cstart = -1; + } + + cloop++; + } + + return exp; +} + +/* exported interface documented in filepath.h */ +char ** +filepath_path_to_strvec(const char *path) +{ + char **strvec; + int strc = 0; + + strvec = calloc(MAX_RESPATH, sizeof(char *)); + if (strvec == NULL) + return NULL; + + strvec[strc] = expand_path(path); + if (strvec[strc] == NULL) { + free(strvec); + return NULL; + } + strc++; + + strvec[strc] = strchr(strvec[0], ':'); + while ((strc < (MAX_RESPATH - 2)) && + (strvec[strc] != NULL)) { + /* null terminate previous entry */ + *strvec[strc] = 0; + strvec[strc]++; + + /* skip colons */ + while (*strvec[strc] == ':') + strvec[strc]++; + + if (*strvec[strc] == 0) + break; /* string is terminated */ + + strc++; + + strvec[strc] = strchr(strvec[strc - 1], ':'); + } + + return strvec; +} + +/* exported interface documented in filepath.h */ +void filepath_free_strvec(char **pathv) +{ + free(pathv[0]); + free(pathv); +} diff --git a/utils/filepath.h b/utils/filepath.h new file mode 100644 index 000000000..08dd050d3 --- /dev/null +++ b/utils/filepath.h @@ -0,0 +1,116 @@ +/* + * Copyright 2010 Vincent Sanders + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** \file Utility routines to obtain paths to file resources. */ + +#ifndef _NETSURF_UTILS_FILEPATH_H_ +#define _NETSURF_UTILS_FILEPATH_H_ + +#include + + +/** Create a normalised file name. + * + * If the file described by the format exists and is accessible the + * normalised path is placed in str and a pointer to str returned + * otherwise NULL is returned. The string in str is always modified. + * + * @param str A buffer to contain the normalised file name must be at + * least PATH_MAX bytes long. + * @param format A printf format for the filename. + * @param ap The list of arguments for the format. + * @return A pointer to the expanded filename or NULL if the file is + * not present or accessible. + */ +char *filepath_vsfindfile(char *str, const char *format, va_list ap); + + +/** Create a normalised file name. + * + * Similar to vsfindfile but takes variadic (printf like) parameters + */ +char *filepath_sfindfile(char *str, const char *format, ...); + + +/** Create a normalised file name. + * + * Similar to sfindfile but allocates its own storage for the + * returned string. The caller must free this sorage. + */ +char *filepath_findfile(const char *format, ...); + + +/** Searches an array of resource paths for a file. + * + * Iterates through a vector of resource paths and returns the + * normalised file name of the first acessible file or NULL if no file + * can be found in any of the resource paths. + * + * @param respathv The resource path vector to iterate. + * @param filepath The buffer to place the result in. + * @param filename The filename of the resource to search for. + * @return A pointer to filepath if a target is found or NULL if not. + */ +char *filepath_sfind(char **respathv, char *filepath, const char *filename); + + +/** Searches an array of resource paths for a file. + * + * Similar to filepath_sfind except it allocates its own storage for + * the returned string. The caller must free this sorage. + */ +char *filepath_find(char **respathv, const char *filename); + + +/** Searches an array of resource paths for a file optionally forcing a default. + * + * Similar to filepath_sfind except if no resource is found the default + * is used as an additional path element to search, if that still + * fails the returned path is set to the concatination of the default + * path and the filename. + */ +char *filepath_sfinddef(char **respathv, char *filepath, const char *filename, + const char *def); + + +/** Merge two string vectors into a resource search path vector. + * + * @param pathv A string vector containing path elemets to scan. + * @param langv A string vector containing language names to enumerate. + * @return A pointer to a NULL terminated string vector of valid + * resource directories. + */ +char **filepath_generate(char * const *pathv, const char * const *langv); + + +/** Convert a colon separated list of path elements into a string vector. + * + * @param path A colon separated path. + * @return A pointer to a NULL terminated string vector of valid + * resource directories. + */ +char **filepath_path_to_strvec(const char *path); + + +/** Free a string vector + * + * Free a string vector allocated by filepath_path_to_strvec + */ +void filepath_free_strvec(char **pathv); + +#endif /* _NETSURF_UTILS_FILEPATH_H_ */ diff --git a/utils/resource.c b/utils/resource.c deleted file mode 100644 index 80b1c8280..000000000 --- a/utils/resource.c +++ /dev/null @@ -1,310 +0,0 @@ -/* - * Copyright 2010 Vincent Sanders - * - * This file is part of NetSurf, http://www.netsurf-browser.org/ - * - * NetSurf is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. - * - * NetSurf is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/** \file - * Provides utility functions for finding readable files. - * - * These functions are intended to make finding resource files more straightforward. - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "utils/config.h" -#include "utils/resource.h" - -/** maximum number of elements in the resource vector */ -#define MAX_RESPATH 128 - -/* exported interface documented in findresource.h */ -char *resource_vsfindfile(char *str, const char *format, va_list ap) -{ - char *realpathname; - char *pathname; - int len; - - pathname = malloc(PATH_MAX); - if (pathname == NULL) - return NULL; /* unable to allocate memory */ - - len = vsnprintf(pathname, PATH_MAX, format, ap); - - if ((len < 0) || (len >= PATH_MAX)) { - /* error or output exceeded PATH_MAX length so - * operation is doomed to fail. - */ - free(pathname); - return NULL; - } - - realpathname = realpath(pathname, str); - - free(pathname); - - if (realpathname != NULL) { - /* sucessfully expanded pathname */ - if (access(realpathname, R_OK) != 0) { - /* unable to read the file */ - return NULL; - } - } - - return realpathname; -} - -/* exported interface documented in findresource.h */ -char *resource_sfindfile(char *str, const char *format, ...) -{ - va_list ap; - char *ret; - - va_start(ap, format); - ret = resource_vsfindfile(str, format, ap); - va_end(ap); - - return ret; -} - -/* exported interface documented in findresource.h */ -char *resource_findfile(const char *format, ...) -{ - char *str; - char *ret; - va_list ap; - - str = malloc(PATH_MAX); - if (str == NULL) - return NULL; /* unable to allocate memory */ - - va_start(ap, format); - ret = resource_vsfindfile(str, format, ap); - va_end(ap); - - if (ret == NULL) - free(str); - - return ret; -} - -/* exported interface documented in findresource.h */ -char *resource_sfind(char **respathv, char *filepath, const char *filename) -{ - int respathc = 0; - - if ((respathv == NULL) || (respathv[0] == NULL) || (filepath == NULL)) - return NULL; - - while (respathv[respathc] != NULL) { - if (resource_sfindfile(filepath, "%s/%s", respathv[respathc], filename) != NULL) - return filepath; - - respathc++; - } - - return NULL; -} - -/* exported interface documented in findresource.h */ -char *resource_find(char **respathv, const char *filename) -{ - char *ret; - char *filepath; - - if ((respathv == NULL) || (respathv[0] == NULL)) - return NULL; - - filepath = malloc(PATH_MAX); - if (filepath == NULL) - return NULL; - - ret = resource_sfind(respathv, filepath, filename); - - if (ret == NULL) - free(filepath); - - return ret; -} - -/* exported interface documented in findresource.h */ -char *resource_sfinddef(char **respathv, char *filepath, const char *filename, const char *def) -{ - char t[PATH_MAX]; - char *ret; - - if ((respathv == NULL) || (respathv[0] == NULL) || (filepath == NULL)) - return NULL; - - ret = resource_sfind(respathv, filepath, filename); - - if ((ret == NULL) && (def != NULL)) { - /* search failed, return the path specified */ - ret = filepath; - if (def[0] == '~') { - snprintf(t, PATH_MAX, "%s/%s/%s", getenv("HOME"), def + 1, filename); - } else { - snprintf(t, PATH_MAX, "%s/%s", def, filename); - } - if (realpath(t, ret) == NULL) { - strcpy(ret, t); - } - - } - return ret; -} - - -/* exported interface documented in resource.h */ -char ** -resource_generate(char * const *pathv, const char * const *langv) -{ - char **respath; /* resource paths vector */ - int pathc = 0; - int langc = 0; - int respathc = 0; - struct stat dstat; - char tmppath[PATH_MAX]; - - respath = calloc(MAX_RESPATH, sizeof(char *)); - - while (pathv[pathc] != NULL) { - if ((stat(pathv[pathc], &dstat) == 0) && - S_ISDIR(dstat.st_mode)) { - /* path element exists and is a directory */ - langc = 0; - while (langv[langc] != NULL) { - snprintf(tmppath, sizeof tmppath, "%s/%s", pathv[pathc],langv[langc]); - if ((stat(tmppath, &dstat) == 0) && - S_ISDIR(dstat.st_mode)) { - /* path element exists and is a directory */ - respath[respathc++] = strdup(tmppath); - } - langc++; - } - respath[respathc++] = strdup(pathv[pathc]); - } - pathc++; - } - - return respath; -} - -/* expand ${} in a string into environment variables */ -static char * -expand_path(const char *path) -{ - char *exp = strdup(path); - int explen; - int cstart = -1; - int cloop = 0; - char *envv; - int envlen; - int replen; /* length of replacement */ - - if (exp == NULL) - return NULL; - - explen = strlen(exp) + 1; - - while (exp[cloop] != 0) { - if ((exp[cloop] == '$') && - (exp[cloop + 1] == '{')) { - cstart = cloop; - cloop++; - } - - if ((cstart != -1) && - (exp[cloop] == '}')) { - replen = cloop - cstart; - exp[cloop] = 0; - envv = getenv(exp + cstart + 2); - if (envv == NULL) { - memmove(exp + cstart, - exp + cloop + 1, - explen - cloop - 1); - explen -= replen; - } else { - envlen = strlen(envv); - exp = realloc(exp, explen + envlen - replen); - memmove(exp + cstart + envlen, - exp + cloop + 1, - explen - cloop - 1); - memmove(exp + cstart, envv, envlen); - explen += envlen - replen; - } - cloop -= replen; - cstart = -1; - } - - cloop++; - } - - return exp; -} - -/* exported interface documented in resource.h */ -char ** -resource_path_to_strvec(const char *path) -{ - char **strvec; - int strc = 0; - - strvec = calloc(MAX_RESPATH, sizeof(char *)); - if (strvec == NULL) - return NULL; - - strvec[strc] = expand_path(path); - if (strvec[strc] == NULL) { - free(strvec); - return NULL; - } - strc++; - - strvec[strc] = strchr(strvec[0], ':'); - while ((strc < (MAX_RESPATH - 2)) && - (strvec[strc] != NULL)) { - /* null terminate previous entry */ - *strvec[strc] = 0; - strvec[strc]++; - - /* skip colons */ - while (*strvec[strc] == ':') - strvec[strc]++; - - if (*strvec[strc] == 0) - break; /* string is terminated */ - - strc++; - - strvec[strc] = strchr(strvec[strc - 1], ':'); - } - - return strvec; -} - -/* exported interface documented in resource.h */ -void resource_free_strvec(char **pathv) -{ - free(pathv[0]); - free(pathv); -} diff --git a/utils/resource.h b/utils/resource.h deleted file mode 100644 index fb717cdea..000000000 --- a/utils/resource.h +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright 2010 Vincent Sanders - * - * This file is part of NetSurf, http://www.netsurf-browser.org/ - * - * NetSurf is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. - * - * NetSurf is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/** \file Utility routines to locate file resources. */ - -#ifndef _NETSURF_UTILS_RESOURCE_H_ -#define _NETSURF_UTILS_RESOURCE_H_ - -#include - -/** Create a normalised file name. - * - * If the file described by the format exists and is accessible the - * normalised path is placed in str and a pointer to str returned - * otherwise NULL is returned. The string in str is always modified. - * - * @param str A buffer to contain the normalised file name must be at - * least PATH_MAX bytes long. - * @param format A printf format for the filename. - * @param ap The list of arguments for the format. - * @return A pointer to the expanded filename or NULL if the file is - * not present or accessible. - */ -char *resource_vsfindfile(char *str, const char *format, va_list ap); - -/** Create a normalised file name. - * - * Similar to vsfindfile but takes variadic (printf like) parameters - */ -char *resource_sfindfile(char *str, const char *format, ...); - -/** Create a normalised file name. - * - * Similar to sfindfile but allocates its own storage for the - * returned string. The caller must free this sorage. - */ -char *resource_findfile(const char *format, ...); - -/** Searches an array of resource paths for a file. - * - * Iterates through a vector of resource paths and returns the - * normalised file name of the first acessible file or NULL if no file - * can be found in any of the resource paths. - * - * @param respathv The resource path vector to iterate. - * @param filepath The buffer to place the result in. - * @param filename The filename of the resource to search for. - * @return A pointer to filepath if a target is found or NULL if not. - */ -char *resource_sfind(char **respathv, char *filepath, const char *filename); - -/** Searches an array of resource paths for a file. - * - * Similar to resource_sfind except it allocates its own storage for - * the returned string. The caller must free this sorage. - */ -char *resource_find(char **respathv, const char *filename); - -/** Searches an array of resource paths for a file optionally forcing a default. - * - * Similar to resource_sfind except if no resource is found the default - * is used as an additional path element to search, if that still - * fails the returned path is set to the concatination of the default - * path and the filename. - */ -char *resource_sfinddef(char **respathv, char *filepath, const char *filename, const char *def); - -/** Merge two string vectors into a resource search path vector. - * - * @param pathv A string vector containing path elemets to scan. - * @param langv A string vector containing language names to enumerate. - * @return A pointer to a NULL terminated string vector of valid - * resource directories. - */ -char **resource_generate(char * const *pathv, const char * const *langv); - - -/** Convert a colon separated list of path elements into a string vector. - * - * @param path A colon separated path. - * @return A pointer to a NULL terminated string vector of valid - * resource directories. - */ -char **resource_path_to_strvec(const char *path); - -/** Free a string vector - * - * Free a string vector allocated by resource_path_to_strvec - */ -void resource_free_strvec(char **pathv); - -#endif /* _NETSURF_UTILS_RESOURCE_H_ */ -- cgit v1.2.3