Skip to content

Display and control your Android device

Extract function to build file paths

Romain Vimontc994f9df6aa8
diff --git a/app/src/util/file.c b/app/src/util/file.c
index 174e5efd..83b0d612 100644
--- a/app/src/util/file.c
+++ b/app/src/util/file.c
@@ -5,6 +5,25 @@
#include "util/log.h"
+char *
+sc_file_build_path(const char *dir, const char *name) {
+ size_t dir_len = strlen(dir);
+ size_t name_len = strlen(name);
+
+ size_t len = dir_len + name_len + 2; // +2: '/' and '\0'
+ char *path = malloc(len);
+ if (!path) {
+ LOG_OOM();
+ return NULL;
+ }
+
+ memcpy(path, dir, dir_len);
+ path[dir_len] = SC_PATH_SEPARATOR;
+ // namelen + 1 to copy the final '\0'
+ memcpy(&path[dir_len + 1], name, name_len + 1);
+ return path;
+}
+
char *
sc_file_get_local_path(const char *name) {
char *executable_path = sc_file_get_executable_path();
@@ -25,24 +44,9 @@ sc_file_get_local_path(const char *name) {
*p = '\0'; // modify executable_path in place
char *dir = executable_path;
- size_t dirlen = strlen(dir);
- size_t namelen = strlen(name);
-
- size_t len = dirlen + namelen + 2; // +2: '/' and '\0'
- char *file_path = malloc(len);
- if (!file_path) {
- LOG_OOM();
- free(executable_path);
- return NULL;
- }
-
- memcpy(file_path, dir, dirlen);
- file_path[dirlen] = SC_PATH_SEPARATOR;
- // namelen + 1 to copy the final '\0'
- memcpy(&file_path[dirlen + 1], name, namelen + 1);
+ char *file_path = sc_file_build_path(dir, name);
free(executable_path);
return file_path;
}
-
diff --git a/app/src/util/file.h b/app/src/util/file.h
index 089f6f75..56c369d7 100644
--- a/app/src/util/file.h
+++ b/app/src/util/file.h
@@ -40,6 +40,15 @@ sc_file_get_executable_path(void);
char *
sc_file_get_local_path(const char *name);
+/**
+ * Return the concatenation of dir, the path separator and the filename.
+ *
+ * The result must be freed by the caller using free(). It may return NULL on
+ * error.
+ */
+char *
+sc_file_build_path(const char *dir, const char *filename);
+
/**
* Indicate if the file exists and is not a directory
*/