commit e7f7005a0184b358cb0d1f28d388976979b5f274
parent e8dd88aff314f5354b039f6afb08efc19abd54eb
Author: nibo <nibo@relim.de>
Date: Sun, 5 Jan 2025 16:13:46 +0100
Resolve a '~' in a image or font filepath
Diffstat:
3 files changed, 31 insertions(+), 2 deletions(-)
diff --git a/chordpro.c b/chordpro.c
@@ -2163,7 +2163,11 @@ cho_image_option_parse(struct ChoImage *image, const char *name, const char *val
image->id = strdup(value);
} else
if (!strcmp(name, "src")) {
- image->src = strdup(value);
+ image->src = filepath_resolve_tilde(value);
+ if (!image->src) {
+ LOG_DEBUG("filepath_resolve_tilde failed.");
+ return false;
+ }
} else
if (!strcmp(name, "width")) {
size = size_create(value);
@@ -2432,7 +2436,11 @@ cho_image_tag_parse(struct Attr **attrs)
int a;
for (a = 0; attrs[a]; a++) {
if (!strcmp(attrs[a]->name, "src")) {
- image->src = strdup(attrs[a]->value);
+ image->src = filepath_resolve_tilde(attrs[a]->value);
+ if (!image->src) {
+ LOG_DEBUG("filepath_resolve_tilde failed.");
+ return NULL;
+ }
} else
if (!strcmp(attrs[a]->name, "id")) {
image->id = strdup(attrs[a]->value);
diff --git a/util.c b/util.c
@@ -399,6 +399,26 @@ filepath_dirname(const char *path)
return dirname;
}
+char *
+filepath_resolve_tilde(const char *path)
+{
+ char *home;
+ char *str = NULL;
+ if (*path == '~') {
+ home = getenv("HOME");
+ if (!home) {
+ LOG_DEBUG("getenv failed.");
+ return NULL;
+ }
+ str = erealloc(str, (strlen(home)+strlen(path)) * sizeof(char));
+ strcpy(str, home);
+ strcat(str, &path[1]);
+ return str;
+ } else {
+ return strdup(path);
+ }
+}
+
static const char *
size_type_to_string(enum SizeType type)
{
diff --git a/util.h b/util.h
@@ -60,6 +60,7 @@ bool file_extension_equals(const char *filepath, const char *extension);
char *filepath_add_ending_slash_if_missing(const char *path);
char *filepath_basename(const char *path);
char *filepath_dirname(const char *path);
+char *filepath_resolve_tilde(const char *path);
struct Size *size_create(const char *str);
struct Size *size_copy(struct Size *size);