1
0
mirror of https://github.com/cc65/cc65.git synced 2024-07-30 04:29:26 +00:00

Rewrote the search path routines to use collections internally.

git-svn-id: svn://svn.cc65.org/cc65/trunk@4654 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
uz 2010-05-01 14:44:14 +00:00
parent 3fd52eb57f
commit 39108e20b5

View File

@ -6,7 +6,7 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 2000-2009, Ullrich von Bassewitz */ /* (C) 2000-2010, Ullrich von Bassewitz */
/* Roemerstrasse 52 */ /* Roemerstrasse 52 */
/* D-70794 Filderstadt */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
@ -44,6 +44,7 @@
#endif #endif
/* common */ /* common */
#include "coll.h"
#include "searchpath.h" #include "searchpath.h"
#include "strbuf.h" #include "strbuf.h"
#include "xmalloc.h" #include "xmalloc.h"
@ -56,7 +57,19 @@
static char* SearchPaths[MAX_SEARCH_PATHS]; /* A search path list is a collection containing path elements. We have
* several of those.
*/
static Collection SearchPaths[MAX_SEARCH_PATHS] = {
STATIC_COLLECTION_INITIALIZER,
STATIC_COLLECTION_INITIALIZER,
STATIC_COLLECTION_INITIALIZER,
STATIC_COLLECTION_INITIALIZER,
STATIC_COLLECTION_INITIALIZER,
STATIC_COLLECTION_INITIALIZER,
STATIC_COLLECTION_INITIALIZER,
STATIC_COLLECTION_INITIALIZER,
};
@ -66,66 +79,47 @@ static char* SearchPaths[MAX_SEARCH_PATHS];
static char* Add (char* Orig, const char* New) static void Add (Collection* Paths, const char* New)
/* Create a new path from Orig and New, delete Orig, return the result */ /* Cleanup a new search path and add it to the list */
{ {
unsigned OrigLen, NewLen; unsigned NewLen;
char* NewPath; char* NewPath;
/* Get the length of the original string */
OrigLen = Orig? strlen (Orig) : 0;
/* Get the length of the new path */ /* Get the length of the new path */
NewLen = strlen (New); NewLen = strlen (New);
/* Check for a trailing path separator and remove it */ /* Check for a trailing path separator and remove it */
if (NewLen > 0 && (New [NewLen-1] == '\\' || New [NewLen-1] == '/')) { if (NewLen > 0 && (New[NewLen-1] == '\\' || New[NewLen-1] == '/')) {
--NewLen; --NewLen;
} }
/* Allocate memory for the new string */ /* Allocate memory for the new string */
NewPath = (char*) xmalloc (OrigLen + NewLen + 2); NewPath = (char*) xmalloc (NewLen + 1);
/* Copy the strings */ /* Copy the path and terminate it */
memcpy (NewPath, Orig, OrigLen); memcpy (NewPath, New, NewLen);
memcpy (NewPath+OrigLen, New, NewLen); NewPath [NewLen] = '\0';
NewPath [OrigLen+NewLen+0] = ';';
NewPath [OrigLen+NewLen+1] = '\0';
/* Delete the original path */ /* Add the path to the collection */
xfree (Orig); CollAppend (Paths, NewPath);
/* Return the new path */
return NewPath;
} }
static char* Find (const char* Path, const char* File) static char* Find (const Collection* PathList, const char* File)
/* Search for a file in a list of directories. If found, return the complete /* Search for a file in a list of directories. If found, return the complete
* name including the path in a malloced data area, if not found, return 0. * name including the path in a malloced data area, if not found, return 0.
*/ */
{ {
const char* P; char* Name = 0;
StrBuf PathName = AUTO_STRBUF_INITIALIZER; StrBuf PathName = AUTO_STRBUF_INITIALIZER;
/* Initialize variables */
P = Path;
/* Handle a NULL pointer as replacement for an empty string */
if (P == 0) {
P = "";
}
/* Start the search */ /* Start the search */
while (*P) { unsigned I;
/* Clear the string buffer */ for (I = 0; I < CollCount (PathList); ++I) {
SB_Clear (&PathName);
/* Copy the next path element into the buffer */ /* Copy the next path element into the buffer */
while (*P != '\0' && *P != ';') { SB_CopyStr (&PathName, CollConstAt (PathList, I));
SB_AppendChar (&PathName, *P++);
}
/* Add a path separator and the filename */ /* Add a path separator and the filename */
if (SB_NotEmpty (&PathName)) { if (SB_NotEmpty (&PathName)) {
@ -136,21 +130,15 @@ static char* Find (const char* Path, const char* File)
/* Check if this file exists */ /* Check if this file exists */
if (access (SB_GetBuf (&PathName), 0) == 0) { if (access (SB_GetBuf (&PathName), 0) == 0) {
/* The file exists, return its name */ /* The file exists, we're done */
char* Name = xstrdup (SB_GetBuf (&PathName)); Name = xstrdup (SB_GetBuf (&PathName));
SB_Done (&PathName); break;
return Name;
}
/* Skip a list separator if we have one */
if (*P == ';') {
++P;
} }
} }
/* Not found */ /* Cleanup and return the result of the search */
SB_Done (&PathName); SB_Done (&PathName);
return 0; return Name;
} }
@ -162,9 +150,8 @@ void AddSearchPath (const char* NewPath, unsigned Where)
if (NewPath) { if (NewPath) {
unsigned I; unsigned I;
for (I = 0; I < MAX_SEARCH_PATHS; ++I) { for (I = 0; I < MAX_SEARCH_PATHS; ++I) {
unsigned Mask = (0x01U << I); if (Where & (0x01U << I)) {
if (Where & Mask) { Add (&SearchPaths[I], NewPath);
SearchPaths[I] = Add (SearchPaths[I], NewPath);
} }
} }
} }
@ -223,10 +210,13 @@ void ForgetAllSearchPaths (unsigned Where)
{ {
unsigned I; unsigned I;
for (I = 0; I < MAX_SEARCH_PATHS; ++I) { for (I = 0; I < MAX_SEARCH_PATHS; ++I) {
unsigned Mask = (0x01U << I); if (Where & (0x01U << I)) {
if (Where & Mask) { unsigned J;
xfree (SearchPaths[I]); Collection* P = &SearchPaths[I];
SearchPaths[I] = 0; for (J = 0; J < CollCount (P); ++J) {
xfree (CollAt (P, J));
}
CollDeleteAll (P);
} }
} }
} }
@ -240,9 +230,8 @@ char* SearchFile (const char* Name, unsigned Where)
{ {
unsigned I; unsigned I;
for (I = 0; I < MAX_SEARCH_PATHS; ++I) { for (I = 0; I < MAX_SEARCH_PATHS; ++I) {
unsigned Mask = (0x01U << I); if (Where & (0x01U << I)) {
if (Where & Mask) { char* Path = Find (&SearchPaths[I], Name);
char* Path = Find (SearchPaths[I], Name);
if (Path) { if (Path) {
/* Found the file */ /* Found the file */
return Path; return Path;