Tuesday, 29 October 2024

[netsurf-dev] [PATCH 3/3] Use dynamic path allocation in gtk and fb frontends

From: Olivier Valentin <valentio@free.fr>

---
frontends/framebuffer/fetch.c | 6 ++++--
frontends/gtk/fetch.c | 6 ++++--
frontends/gtk/gui.c | 9 ++++++---
utils/filepath.c | 35 +++++++++++++++++++++++++----------
utils/filepath.h | 14 +++++++++++++-
5 files changed, 52 insertions(+), 18 deletions(-)

diff --git a/frontends/framebuffer/fetch.c b/frontends/framebuffer/fetch.c
index f917bddf3..9d76f0818 100644
--- a/frontends/framebuffer/fetch.c
+++ b/frontends/framebuffer/fetch.c
@@ -48,14 +48,16 @@
*/
static nsurl *get_resource_url(const char *path)
{
- char buf[PATH_MAX];
+ char *buf;
nsurl *url = NULL;

if (strcmp(path, "favicon.ico") == 0)
path = "favicon.png";

- if (filepath_sfind(respaths, buf, PATH_MAX, path) == NSERROR_OK)
+ if (filepath_find(respaths, path, &buf) == NSERROR_OK) {
netsurf_path_to_nsurl(buf, &url);
+ free(buf);
+ }

return url;
}
diff --git a/frontends/gtk/fetch.c b/frontends/gtk/fetch.c
index 98682ffc4..91219ad96 100644
--- a/frontends/gtk/fetch.c
+++ b/frontends/gtk/fetch.c
@@ -250,15 +250,17 @@ const char *fetch_filetype(const char *unix_path)

static nsurl *nsgtk_get_resource_url(const char *path)
{
- char buf[PATH_MAX];
nsurl *url = NULL;

/* favicon.ico -> favicon.png */
if (strcmp(path, "favicon.ico") == 0) {
nsurl_create("resource:favicon.png", &url);
} else {
- if (filepath_sfind(respaths, buf, PATH_MAX, path) == NSERROR_OK)
+ char *buf;
+ if (filepath_find(respaths, path, &buf) == NSERROR_OK) {
netsurf_path_to_nsurl(buf, &url);
+ free(buf);
+ }
}

return url;
diff --git a/frontends/gtk/gui.c b/frontends/gtk/gui.c
index 2c5558ddb..725c55b2a 100644
--- a/frontends/gtk/gui.c
+++ b/frontends/gtk/gui.c
@@ -910,7 +910,7 @@ static nserror nsgtk_add_named_icons_to_theme(void)
*/
static nserror nsgtk_setup(int argc, char** argv, char **respath)
{
- char buf[PATH_MAX];
+ char *buf;
char *resource_filename;
char *addr = NULL;
nsurl *url;
@@ -987,8 +987,11 @@ static nserror nsgtk_setup(int argc, char** argv, char **respath)
.pma = true,
});

- filepath_sfinddef(respath, buf, PATH_MAX, "mime.types", "/etc/");
- gtk_fetch_filetype_init(buf);
+ res = filepath_finddef(respath, "mime.types", "/etc/", &buf);
+ if (res == NSERROR_OK) {
+ gtk_fetch_filetype_init(buf);
+ free(buf);
+ }

save_complete_init();

diff --git a/utils/filepath.c b/utils/filepath.c
index 82568e298..626dc2c89 100644
--- a/utils/filepath.c
+++ b/utils/filepath.c
@@ -160,29 +160,44 @@ nserror filepath_find(char **respathv, const char *filename, char **retstr)

/* exported interface documented in filepath.h */
nserror
-filepath_sfinddef(char **respathv,
- char *filepath,
- size_t size,
- const char *filename,
- const char *def)
+filepath_finddef(char **respathv,
+ const char *filename,
+ const char *def,
+ char **retstr)
{
- char *allocatedfilepath;
nserror ret;

- if ((respathv == NULL) || (respathv[0] == NULL) || (filepath == NULL))
+ if ((respathv == NULL) || (respathv[0] == NULL) || (retstr == NULL))
return NSERROR_INVALID;

- ret = filepath_find(respathv, filename, &allocatedfilepath);
+ ret = filepath_find(respathv, filename, retstr);

if ((ret != NSERROR_OK) && (def != NULL)) {
/* search failed, return the path specified */
if (def[0] == '~') {
- ret = filepath_printpath(&allocatedfilepath, "%s/%s/%s", getenv("HOME"), def + 1, filename);
+ ret = filepath_printpath(retstr, "%s/%s/%s", getenv("HOME"), def + 1, filename);
} else {
- ret = filepath_printpath(&allocatedfilepath, "%s/%s", def, filename);
+ ret = filepath_printpath(retstr, "%s/%s", def, filename);
}
}

+ return ret;
+}
+
+
+/* exported interface documented in filepath.h */
+nserror
+filepath_sfinddef(char **respathv,
+ char *filepath,
+ size_t size,
+ const char *filename,
+ const char *def)
+{
+ nserror ret;
+ char *allocatedfilepath;
+
+ ret = filepath_finddef(respathv, filename, def, &allocatedfilepath);
+
if (ret == NSERROR_OK) {
if (strlen(allocatedfilepath) >= size)
ret = NSERROR_NOSPACE;
diff --git a/utils/filepath.h b/utils/filepath.h
index c44f5af7e..f9316c997 100644
--- a/utils/filepath.h
+++ b/utils/filepath.h
@@ -68,12 +68,24 @@ nserror filepath_find(char **respathv, const char *filename, char **retstr);
/**
* 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
+ * Similar to filepath_find 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.
*
* @param respathv The resource path vector to iterate.
+ * @param filename The filename of the resource to search for.
+ * @param def The default path to use.
+ * @param retstr The result string containing the path.
+ * @return NSERROR_OK on success or error code on failure.
+ */
+nserror filepath_finddef(char **respathv, const char *filename,
+ const char *def, char **retstr);
+
+/**
+ * Variant of filepath_finddef returning the result in a prealocated buffer.
+ *
+ * @param respathv The resource path vector to iterate.
* @param filepath The buffer to place the result in.
* @param size The size of the filepath buffer.
* @param filename The filename of the resource to search for.
--
2.45.2

No comments:

Post a Comment