summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorVincent Sanders <vince@netsurf-browser.org>2010-07-28 15:22:44 +0000
committerVincent Sanders <vince@netsurf-browser.org>2010-07-28 15:22:44 +0000
commitbda534e12a5071434d700d91750f9c1eaa422812 (patch)
treedf2f256fd55d6e5589e59074aba1ee6edada9848 /utils
parent4bfd6ea6f0b68202a00f9faa29ad6ff0144af0d9 (diff)
downloadnetsurf-bda534e12a5071434d700d91750f9c1eaa422812.tar.gz
netsurf-bda534e12a5071434d700d91750f9c1eaa422812.tar.bz2
Clean up how GTK frontend finds resources
svn path=/trunk/netsurf/; revision=10668
Diffstat (limited to 'utils')
-rw-r--r--utils/findresource.c210
-rw-r--r--utils/findresource.h88
-rw-r--r--utils/messages.c7
3 files changed, 303 insertions, 2 deletions
diff --git a/utils/findresource.c b/utils/findresource.c
new file mode 100644
index 000000000..828b73276
--- /dev/null
+++ b/utils/findresource.c
@@ -0,0 +1,210 @@
+/*
+ * Copyright 2010 Vincent Sanders <vince@kyllikki.org>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+/** \file
+ * Provides utility functions for finding readable files.
+ *
+ * These functions are intended to make finding resource files more straightforward.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdarg.h>
+#include <malloc.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include "utils/findresource.h"
+
+#define MAX_RESPATH 128 /* maximum number of elements in the resource vector */
+
+/* exported interface documented in findresource.h */
+char *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 *sfindfile(char *str, const char *format, ...)
+{
+ va_list ap;
+ char *ret;
+
+ va_start(ap, format);
+ ret = vsfindfile(str, format, ap);
+ va_end(ap);
+
+ return ret;
+}
+
+/* exported interface documented in findresource.h */
+char *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 = vsfindfile(str, format, ap);
+ va_end(ap);
+
+ if (ret == NULL)
+ free(str);
+
+ return ret;
+}
+
+/* exported interface documented in findresource.h */
+char *sfindresource(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 (sfindfile(filepath, "%s/%s", respathv[respathc], filename) != NULL)
+ return filepath;
+
+ respathc++;
+ }
+
+ return NULL;
+}
+
+/* exported interface documented in findresource.h */
+char *findresource(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 = sfindresource(respathv, filepath, filename);
+
+ if (ret == NULL)
+ free(filepath);
+
+ return ret;
+}
+
+/* exported interface documented in findresource.h */
+char *sfindresourcedef(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 = sfindresource(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 findresource.h */
+char **
+findresource_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, PATH_MAX,"%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;
+}
diff --git a/utils/findresource.h b/utils/findresource.h
new file mode 100644
index 000000000..9e499f570
--- /dev/null
+++ b/utils/findresource.h
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2010 Vincent Sanders <vince@kyllikki.org>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _NETSURF_UTILS_FINDRESOURCE_H_
+#define _NETSURF_UTILS_FINDRESOURCE_H_
+
+/** 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 *vsfindfile(char *str, const char *format, va_list ap);
+
+/** Create a normalised file name.
+ *
+ * Similar to vsfindfile but takes variadic (printf like) parameters
+ */
+char *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 *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 *sfindresource(char **respathv, char *filepath, const char *filename);
+
+/** Searches an array of resource paths for a file.
+ *
+ * Similar to sfindresource except it allocates its own storage for
+ * the returned string. The caller must free this sorage.
+ */
+char *findresource(char **respathv, const char *filename);
+
+/** Searches an array of resource paths for a file optionally forcing a default.
+ *
+ * Similar to sfindresource 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 *sfindresourcedef(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 **findresource_generate(char * const *pathv, const char * const *langv);
+
+#endif
diff --git a/utils/messages.c b/utils/messages.c
index 5ac44ae3c..2f3bc173e 100644
--- a/utils/messages.c
+++ b/utils/messages.c
@@ -116,8 +116,11 @@ void messages_load(const char *path)
struct hash_table *m;
char s[400];
- assert(path != NULL);
-
+ if (path == NULL)
+ return;
+
+ LOG(("Loading Messages from '%s'", path));
+
m = messages_load_ctx(path, messages_hash);
if (m == NULL) {
LOG(("Unable to open Messages file '%s'. Possible reason: %s",