]> git.street.me.uk Git - andy/viking.git/commitdiff
Functions to convert potentially relative filenames to absolute filenames.
authorRob Norris <rw_norris@hotmail.com>
Sat, 20 May 2017 13:19:36 +0000 (14:19 +0100)
committerRob Norris <rw_norris@hotmail.com>
Sat, 20 May 2017 14:22:20 +0000 (15:22 +0100)
src/util.c
src/util.h

index 2b129dc4199fb36a150befc97505a670ca15d63b..d8939f9f3670f5f407555e7a0ddb176123d78308 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "util.h"
 #include "globals.h"
+#include "fileutils.h"
 
 #ifdef WINDOWS
 #include <windows.h>
@@ -234,3 +235,46 @@ gchar* util_write_tmp_file_from_bytes ( const void *buffer, gsize count )
 
        return tmpname;
 }
+
+/**
+ * util_make_absolute_filename:
+ *
+ * Returns a newly allocated string of the absolute filename or
+ *   NULL if name is already absolute (or dirpath is unusable)
+ */
+gchar* util_make_absolute_filename ( const gchar *filename, const gchar *dirpath )
+{
+       if ( !dirpath ) return NULL;
+
+       // Is it ready absolute?
+       if ( g_path_is_absolute ( filename ) ) {
+               return NULL;
+       }
+       else {
+               // Otherwise create the absolute filename from the given directory and filename
+               gchar *full = g_strconcat ( dirpath, G_DIR_SEPARATOR_S, filename, NULL );
+               gchar *absolute = file_realpath_dup ( full ); // resolved into the canonical name
+               g_free ( full );
+               return absolute;
+       }
+}
+
+/**
+ * util_make_absolute_filenames:
+ *
+ * Ensures the supplied list of filenames are all absolute
+ */
+void util_make_absolute_filenames ( GList *filenames, const gchar *dirpath )
+{
+       if ( !filenames ) return;
+       if ( !dirpath ) return;
+
+       GList *gl;
+       foreach_list ( gl, filenames ) {
+               gchar *fn = util_make_absolute_filename ( gl->data, dirpath );
+               if ( fn ) {
+                       g_free ( gl->data );
+                       gl->data = fn;
+               }
+       }
+}
index 42799d19f9ae6beed001ae64580163f77bbdd83a..ab1bf7dbb4367b58a6b1b0bd47e35dcb0ab6b363 100644 (file)
@@ -65,6 +65,10 @@ int util_remove ( const gchar *filename );
 
 gchar* util_write_tmp_file_from_bytes ( const void *buffer, gsize count );
 
+gchar* util_make_absolute_filename ( const gchar *filename, const gchar *dirpath );
+
+void util_make_absolute_filenames ( GList *filenames, const gchar *dirpath );
+
 G_END_DECLS
 
 #endif