]> git.sur5r.net Git - cc65/blobdiff - src/common/searchpath.c
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / src / common / searchpath.c
index c44a9c9a80449fed740c6afaa24b9b30c0e626bf..16945f36d4ab344a2a57533286cc6de4561be477 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2000-2010, Ullrich von Bassewitz                                      */
+/* (C) 2000-2013, Ullrich von Bassewitz                                      */
 /*                Roemerstrasse 52                                           */
 /*                D-70794 Filderstadt                                        */
 /* EMail:         uz@cc65.org                                                */
@@ -38,6 +38,7 @@
 #if defined(_MSC_VER)
 /* Microsoft compiler */
 #  include <io.h>
+#  pragma warning(disable : 4996)
 #else
 /* Anyone else */
 #  include <unistd.h>
 
 
 /*****************************************************************************/
-/*                                  Code                                    */
+/*                                   Code                                    */
 /*****************************************************************************/
 
 
 
-static void Add (SearchPath* P, const char* New)
-/* Cleanup a new search path and add it to the list */
+static char* CleanupPath (const char* Path)
+/* Prepare and return a clean copy of Path */
 {
-    unsigned NewLen;
+    unsigned Len;
     char*    NewPath;
 
-    /* Get the length of the new path */
-    NewLen = strlen (New);
+    /* Get the length of the path */
+    Len = strlen (Path);
 
     /* Check for a trailing path separator and remove it */
-    if (NewLen > 0 && (New[NewLen-1] == '\\' || New[NewLen-1] == '/')) {
-       --NewLen;
+    if (Len > 0 && (Path[Len-1] == '\\' || Path[Len-1] == '/')) {
+        --Len;
     }
 
     /* Allocate memory for the new string */
-    NewPath = (char*) xmalloc (NewLen + 1);
+    NewPath = (char*) xmalloc (Len + 1);
+
+    /* Copy the path and terminate it, then return the copy */
+    memcpy (NewPath, Path, Len);
+    NewPath [Len] = '\0';
+    return NewPath;
+}
+
 
-    /* Copy the path and terminate it */
-    memcpy (NewPath, New, NewLen);
-    NewPath [NewLen] = '\0';
 
-    /* Add the path to the collection */
-    CollAppend (P, NewPath);
+static void Add (SearchPath* P, const char* New)
+/* Cleanup a new search path and add it to the list */
+{
+    /* Add a clean copy of the path to the collection */
+    CollAppend (P, CleanupPath (New));
 }
 
 
@@ -122,8 +130,8 @@ void AddSubSearchPathFromEnv (SearchPath* P, const char* EnvVar, const char* Sub
 
     const char* EnvVal = getenv (EnvVar);
     if (EnvVal == 0) {
-       /* Not found */
-       return;
+        /* Not found */
+        return;
     }
 
     /* Copy the environment variable to the buffer */
@@ -131,9 +139,9 @@ void AddSubSearchPathFromEnv (SearchPath* P, const char* EnvVar, const char* Sub
 
     /* Add a path separator if necessary */
     if (SB_NotEmpty (&Dir)) {
-       if (SB_LookAtLast (&Dir) != '\\' && SB_LookAtLast (&Dir) != '/') {
-           SB_AppendChar (&Dir, '/');
-       }
+        if (SB_LookAtLast (&Dir) != '\\' && SB_LookAtLast (&Dir) != '/') {
+            SB_AppendChar (&Dir, '/');
+        }
     }
 
     /* Add the subdirectory and terminate the string */
@@ -149,14 +157,82 @@ void AddSubSearchPathFromEnv (SearchPath* P, const char* EnvVar, const char* Sub
 
 
 
-void ForgetSearchPath (SearchPath* P)
-/* Forget all search paths in the given list */
+void AddSubSearchPathFromWinBin (SearchPath* P, const char* SubDir)
 {
-    unsigned I;
-    for (I = 0; I < CollCount (P); ++I) {
-        xfree (CollAt (P, I));
+/* Windows only:
+ * Add a search path from the running binary, adding a subdirectory to
+ * the parent directory of the directory containing the binary.
+ */
+#if defined(_MSC_VER)
+
+    char Dir[_MAX_PATH];
+    char* Ptr;
+
+    if (_get_pgmptr (&Ptr) != 0) {
+        return;
+    }
+    strcpy (Dir, Ptr);
+
+    /* Remove binary name */
+    Ptr = strrchr (Dir, '\\');
+    if (Ptr == 0) {
+        return;
+    }
+    *Ptr = '\0';
+
+    /* Check for 'bin' directory */
+    Ptr = strrchr (Dir, '\\');
+    if (Ptr == 0) {
+        return;
+    }
+    if (strcmp (Ptr++, "\\bin") != 0) {
+        return;
     }
-    CollDeleteAll (P);
+
+    /* Append SubDir */
+    strcpy (Ptr, SubDir);
+
+    /* Add the search path */
+    AddSearchPath (P, Dir);
+
+#else
+
+    (void) P;
+    (void) SubDir;
+
+#endif
+}
+
+
+int PushSearchPath (SearchPath* P, const char* NewPath)
+/* Add a new search path to the head of an existing search path list, provided
+ * that it's not already there. If the path is already at the first position,
+ * return zero, otherwise return a non zero value.
+ */
+{                                      
+    /* Generate a clean copy of NewPath */
+    char* Path = CleanupPath (NewPath);   
+
+    /* If we have paths, check if Path is already at position zero */
+    if (CollCount (P) > 0 && strcmp (CollConstAt (P, 0), Path) == 0) {
+        /* Match. Delete the copy and return to the caller */
+        xfree (Path);
+        return 0;
+    }
+
+    /* Insert a clean copy of the path at position 0, return success */
+    CollInsert (P, Path, 0);
+    return 1;
+}
+
+
+
+void PopSearchPath (SearchPath* P)
+/* Remove a search path from the head of an existing search path list */
+{
+    /* Remove the path at position 0 */
+    xfree (CollAt (P, 0));
+    CollDelete (P, 0);
 }
 
 
@@ -176,19 +252,19 @@ char* SearchFile (const SearchPath* P, const char* File)
         /* Copy the next path element into the buffer */
         SB_CopyStr (&PathName, CollConstAt (P, I));
 
-       /* Add a path separator and the filename */
-               if (SB_NotEmpty (&PathName)) {
-           SB_AppendChar (&PathName, '/');
-       }
-       SB_AppendStr (&PathName, File);
-       SB_Terminate (&PathName);
-
-       /* Check if this file exists */
-               if (access (SB_GetBuf (&PathName), 0) == 0) {
-           /* The file exists, we're done */
-           Name = xstrdup (SB_GetBuf (&PathName));
+        /* Add a path separator and the filename */
+        if (SB_NotEmpty (&PathName)) {
+            SB_AppendChar (&PathName, '/');
+        }
+        SB_AppendStr (&PathName, File);
+        SB_Terminate (&PathName);
+
+        /* Check if this file exists */
+        if (access (SB_GetBuf (&PathName), 0) == 0) {
+            /* The file exists, we're done */
+            Name = xstrdup (SB_GetBuf (&PathName));
             break;
-       }
+        }
     }
 
     /* Cleanup and return the result of the search */