]> git.street.me.uk Git - andy/viking.git/commitdiff
Shift pure glib code from file.c to fileutils.c to simplify dependencies.
authorRob Norris <rw_norris@hotmail.com>
Tue, 17 Jan 2017 23:07:17 +0000 (23:07 +0000)
committerRob Norris <rw_norris@hotmail.com>
Fri, 14 Apr 2017 10:24:22 +0000 (11:24 +0100)
src/file.c
src/file.h
src/fileutils.c
src/fileutils.h

index 3e1abf64820a18cd56efcae7f18bbc7f6f5c613b..943c56db62f7dc1cd60a8dc4ad2fca94cc29761f 100644 (file)
@@ -38,9 +38,6 @@
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
-#ifdef WINDOWS
-#define realpath(X,Y) _fullpath(Y,X,MAX_PATH)
-#endif
 #include <glib.h>
 #include <glib/gstdio.h>
 #include <glib/gi18n.h>
 #include <glib.h>
 #include <glib/gstdio.h>
 #include <glib/gi18n.h>
@@ -876,144 +873,3 @@ gboolean a_file_export_babel ( VikTrwLayer *vtl, const gchar *filename, const gc
   g_free(args);
   return result;
 }
   g_free(args);
   return result;
 }
-
-/**
- * Just a wrapper around realpath, which itself is platform dependent
- */
-char *file_realpath ( const char *path, char *real )
-{
-  return realpath ( path, real );
-}
-
-#ifndef MAXPATHLEN
-#define MAXPATHLEN 1024
-#endif
-/**
- * Always return the canonical filename in a newly allocated string
- */
-char *file_realpath_dup ( const char *path )
-{
-       char real[MAXPATHLEN];
-
-       g_return_val_if_fail(path != NULL, NULL);
-
-       if (file_realpath(path, real))
-               return g_strdup(real);
-
-       return g_strdup(path);
-}
-
-/**
- * Permission granted to use this code after personal correspondance
- * Slightly reworked for better cross platform use, glibisms, function rename and a compacter format
- *
- * FROM http://www.codeguru.com/cpp/misc/misc/fileanddirectorynaming/article.php/c263
- */
-
-// GetRelativeFilename(), by Rob Fisher.
-// rfisher@iee.org
-// http://come.to/robfisher
-
-// The number of characters at the start of an absolute filename.  e.g. in DOS,
-// absolute filenames start with "X:\" so this value should be 3, in UNIX they start
-// with "\" so this value should be 1.
-#ifdef WINDOWS
-#define ABSOLUTE_NAME_START 3
-#else
-#define ABSOLUTE_NAME_START 1
-#endif
-
-// Given the absolute current directory and an absolute file name, returns a relative file name.
-// For example, if the current directory is C:\foo\bar and the filename C:\foo\whee\text.txt is given,
-// GetRelativeFilename will return ..\whee\text.txt.
-
-const gchar *file_GetRelativeFilename ( gchar *currentDirectory, gchar *absoluteFilename )
-{
-  gint afMarker = 0, rfMarker = 0;
-  gint cdLen = 0, afLen = 0;
-  gint i = 0;
-  gint levels = 0;
-  static gchar relativeFilename[MAXPATHLEN+1];
-
-  cdLen = strlen(currentDirectory);
-  afLen = strlen(absoluteFilename);
-
-  // make sure the names are not too long or too short
-  if (cdLen > MAXPATHLEN || cdLen < ABSOLUTE_NAME_START+1 ||
-      afLen > MAXPATHLEN || afLen < ABSOLUTE_NAME_START+1) {
-    return NULL;
-  }
-
-  // Handle DOS names that are on different drives:
-  if (currentDirectory[0] != absoluteFilename[0]) {
-    // not on the same drive, so only absolute filename will do
-    g_strlcpy(relativeFilename, absoluteFilename, MAXPATHLEN+1);
-    return relativeFilename;
-  }
-
-  // they are on the same drive, find out how much of the current directory
-  // is in the absolute filename
-  i = ABSOLUTE_NAME_START;
-  while (i < afLen && i < cdLen && currentDirectory[i] == absoluteFilename[i]) {
-    i++;
-  }
-
-  if (i == cdLen && (absoluteFilename[i] == G_DIR_SEPARATOR || absoluteFilename[i-1] == G_DIR_SEPARATOR)) {
-    // the whole current directory name is in the file name,
-    // so we just trim off the current directory name to get the
-    // current file name.
-    if (absoluteFilename[i] == G_DIR_SEPARATOR) {
-      // a directory name might have a trailing slash but a relative
-      // file name should not have a leading one...
-      i++;
-    }
-
-    g_strlcpy(relativeFilename, &absoluteFilename[i], MAXPATHLEN+1);
-    return relativeFilename;
-  }
-
-  // The file is not in a child directory of the current directory, so we
-  // need to step back the appropriate number of parent directories by
-  // using "..\"s.  First find out how many levels deeper we are than the
-  // common directory
-  afMarker = i;
-  levels = 1;
-
-  // count the number of directory levels we have to go up to get to the
-  // common directory
-  while (i < cdLen) {
-    i++;
-    if (currentDirectory[i] == G_DIR_SEPARATOR) {
-      // make sure it's not a trailing slash
-      i++;
-      if (currentDirectory[i] != '\0') {
-       levels++;
-      }
-    }
-  }
-
-  // move the absolute filename marker back to the start of the directory name
-  // that it has stopped in.
-  while (afMarker > 0 && absoluteFilename[afMarker-1] != G_DIR_SEPARATOR) {
-    afMarker--;
-  }
-
-  // check that the result will not be too long
-  if (levels * 3 + afLen - afMarker > MAXPATHLEN) {
-    return NULL;
-  }
-
-  // add the appropriate number of "..\"s.
-  rfMarker = 0;
-  for (i = 0; i < levels; i++) {
-    relativeFilename[rfMarker++] = '.';
-    relativeFilename[rfMarker++] = '.';
-    relativeFilename[rfMarker++] = G_DIR_SEPARATOR;
-  }
-
-  // copy the rest of the filename into the result string
-  strcpy(&relativeFilename[rfMarker], &absoluteFilename[afMarker]);
-
-  return relativeFilename;
-}
-/* END http://www.codeguru.com/cpp/misc/misc/fileanddirectorynaming/article.php/c263 */
index a0b50d4f63f6019fce00ab4c09c97a79b8141772..2bb71f9032153107e09d388005d9d99d6bd96166 100644 (file)
@@ -67,12 +67,6 @@ gboolean a_file_export_babel ( VikTrwLayer *vtl, const gchar *filename, const gc
 
 void file_write_layer_param ( FILE *f, const gchar *name, VikLayerParamType type, VikLayerParamData data );
 
 
 void file_write_layer_param ( FILE *f, const gchar *name, VikLayerParamType type, VikLayerParamData data );
 
-char *file_realpath ( const char *path, char *real );
-
-char *file_realpath_dup ( const char *path );
-
-const gchar *file_GetRelativeFilename ( gchar *currentDirectory, gchar *absoluteFilename );
-
 G_END_DECLS
 
 #endif
 G_END_DECLS
 
 #endif
index 7181c4fc9c443af437ce0af2259065a299a906b8..a6f8ef7036849529638dad784187f8aced07713a 100644 (file)
@@ -29,6 +29,8 @@
 #include "fileutils.h"
 
 #ifdef WINDOWS
 #include "fileutils.h"
 
 #ifdef WINDOWS
+#include <windef.h>
+#define realpath(X,Y) _fullpath(Y,X,MAX_PATH)
 #define FILE_SEP '\\'
 #else
 #define FILE_SEP '/'
 #define FILE_SEP '\\'
 #else
 #define FILE_SEP '/'
@@ -44,3 +46,144 @@ const gchar *a_file_basename ( const gchar *filename )
     return t;
   return filename;
 }
     return t;
   return filename;
 }
+
+/**
+ * Just a wrapper around realpath, which itself is platform dependent
+ */
+char *file_realpath ( const char *path, char *real )
+{
+  return realpath ( path, real );
+}
+
+#ifndef MAXPATHLEN
+#define MAXPATHLEN 1024
+#endif
+/**
+ * Always return the canonical filename in a newly allocated string
+ */
+char *file_realpath_dup ( const char *path )
+{
+       char real[MAXPATHLEN];
+
+       g_return_val_if_fail(path != NULL, NULL);
+
+       if (file_realpath(path, real))
+               return g_strdup(real);
+
+       return g_strdup(path);
+}
+
+/**
+ * Permission granted to use this code after personal correspondance
+ * Slightly reworked for better cross platform use, glibisms, function rename and a compacter format
+ *
+ * FROM http://www.codeguru.com/cpp/misc/misc/fileanddirectorynaming/article.php/c263
+ */
+
+// GetRelativeFilename(), by Rob Fisher.
+// rfisher@iee.org
+// http://come.to/robfisher
+
+// The number of characters at the start of an absolute filename.  e.g. in DOS,
+// absolute filenames start with "X:\" so this value should be 3, in UNIX they start
+// with "\" so this value should be 1.
+#ifdef WINDOWS
+#define ABSOLUTE_NAME_START 3
+#else
+#define ABSOLUTE_NAME_START 1
+#endif
+
+// Given the absolute current directory and an absolute file name, returns a relative file name.
+// For example, if the current directory is C:\foo\bar and the filename C:\foo\whee\text.txt is given,
+// GetRelativeFilename will return ..\whee\text.txt.
+
+const gchar *file_GetRelativeFilename ( gchar *currentDirectory, gchar *absoluteFilename )
+{
+  gint afMarker = 0, rfMarker = 0;
+  gint cdLen = 0, afLen = 0;
+  gint i = 0;
+  gint levels = 0;
+  static gchar relativeFilename[MAXPATHLEN+1];
+
+  cdLen = strlen(currentDirectory);
+  afLen = strlen(absoluteFilename);
+
+  // make sure the names are not too long or too short
+  if (cdLen > MAXPATHLEN || cdLen < ABSOLUTE_NAME_START+1 ||
+      afLen > MAXPATHLEN || afLen < ABSOLUTE_NAME_START+1) {
+    return NULL;
+  }
+
+  // Handle DOS names that are on different drives:
+  if (currentDirectory[0] != absoluteFilename[0]) {
+    // not on the same drive, so only absolute filename will do
+    g_strlcpy(relativeFilename, absoluteFilename, MAXPATHLEN+1);
+    return relativeFilename;
+  }
+
+  // they are on the same drive, find out how much of the current directory
+  // is in the absolute filename
+  i = ABSOLUTE_NAME_START;
+  while (i < afLen && i < cdLen && currentDirectory[i] == absoluteFilename[i]) {
+    i++;
+  }
+
+  if (i == cdLen && (absoluteFilename[i] == G_DIR_SEPARATOR || absoluteFilename[i-1] == G_DIR_SEPARATOR)) {
+    // the whole current directory name is in the file name,
+    // so we just trim off the current directory name to get the
+    // current file name.
+    if (absoluteFilename[i] == G_DIR_SEPARATOR) {
+      // a directory name might have a trailing slash but a relative
+      // file name should not have a leading one...
+      i++;
+    }
+
+    g_strlcpy(relativeFilename, &absoluteFilename[i], MAXPATHLEN+1);
+    return relativeFilename;
+  }
+
+  // The file is not in a child directory of the current directory, so we
+  // need to step back the appropriate number of parent directories by
+  // using "..\"s.  First find out how many levels deeper we are than the
+  // common directory
+  afMarker = i;
+  levels = 1;
+
+  // count the number of directory levels we have to go up to get to the
+  // common directory
+  while (i < cdLen) {
+    i++;
+    if (currentDirectory[i] == G_DIR_SEPARATOR) {
+      // make sure it's not a trailing slash
+      i++;
+      if (currentDirectory[i] != '\0') {
+       levels++;
+      }
+    }
+  }
+
+  // move the absolute filename marker back to the start of the directory name
+  // that it has stopped in.
+  while (afMarker > 0 && absoluteFilename[afMarker-1] != G_DIR_SEPARATOR) {
+    afMarker--;
+  }
+
+  // check that the result will not be too long
+  if (levels * 3 + afLen - afMarker > MAXPATHLEN) {
+    return NULL;
+  }
+
+  // add the appropriate number of "..\"s.
+  rfMarker = 0;
+  for (i = 0; i < levels; i++) {
+    relativeFilename[rfMarker++] = '.';
+    relativeFilename[rfMarker++] = '.';
+    relativeFilename[rfMarker++] = G_DIR_SEPARATOR;
+  }
+
+  // copy the rest of the filename into the result string
+  strcpy(&relativeFilename[rfMarker], &absoluteFilename[afMarker]);
+
+  return relativeFilename;
+}
+/* END http://www.codeguru.com/cpp/misc/misc/fileanddirectorynaming/article.php/c263 */
index 4052e42f86e134bd39d611b0d8bd31b14310fadd..fdbd5906c29c746036d6428f6513f80416ebf0b8 100644 (file)
@@ -27,6 +27,12 @@ G_BEGIN_DECLS
 
 const gchar *a_file_basename ( const gchar *filename );
 
 
 const gchar *a_file_basename ( const gchar *filename );
 
+char *file_realpath ( const char *path, char *real );
+
+char *file_realpath_dup ( const char *path );
+
+const gchar *file_GetRelativeFilename ( gchar *currentDirectory, gchar *absoluteFilename );
+
 G_END_DECLS
 
 #endif
 G_END_DECLS
 
 #endif