summaryrefslogtreecommitdiff
path: root/utils/DirPath.cc
blob: 0536eb2e175df647dafe2fb58491a118754d9f76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 *	CENA C++ Utilities
 *
 *	by Stephane Chatty
 *
 *	Copyright 1991, 1992
 *	Laboratoire de Recherche en Informatique (LRI)
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	management of search paths
 *
 *	$Id$
 *	$CurLog$
 */

#include "DirPath.h"
#include "String.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>

/*?class CcuDirPath
\typ{CcuDirPath}s generalize the idea contained in the \com{PATH} environment variable
of UNIX.
A \typ{CcuDirPath} is a list of strings, which are thought of as directory names.
When looking for a file, each directory in the list is scanned, until the file is found or the
end of the list is reached.
?*/


/*?
Construct an empty directory path.
?*/
CcuDirPath :: CcuDirPath ()
: CcuList (),
  Alloc (DontAllocNames)
{
}

/*?
Construct an empty directory path.
The default allocation mode is set to \var{alloc} (see function \fun{FindFile}).
All other constructors initialize it to 0.
?*/
CcuDirPath :: CcuDirPath (alloc_mode alloc)
: CcuList (),
  Alloc (alloc)
{
}

/*?
Construct a \typ{CcuDirPath} which has one element \var{name}. The string \var{name}
is copied.
?*/
CcuDirPath :: CcuDirPath (const char* name)
: CcuList (NewString (name)),
  Alloc (DontAllocNames)
{
}

/*?
Construct a \typ{CcuDirPath} with two or more elements (\var{first}, \var{second},
and so on). All strings (\var{first} and the following ones) are copied.
{\bf The argument list must be null-terminated.}
?*/
CcuDirPath :: CcuDirPath (const char* first, const char*, ...)
: CcuList (),
  Alloc (DontAllocNames)
{

	va_list ap;
	va_start(ap, first);
	
	for (;;) {
		const char* p = va_arg(ap,char*);
		if (!p)
			break;
		Append (p);
	}
	
	va_end(ap);
}

/*?nodoc?*/
CcuDirPath :: ~CcuDirPath ()
{
	CcuListIter li (*this);
	while (++li)
		FreeString ((char*) (*li));
}

/*?nextdoc?*/
void
CcuDirPath :: Append (const char* dir)
{
	CcuList::Append (NewString (dir));
}

/*?
Append (resp. prepend) a directory name to a search path. A copy of the
string \var{dir} is created.
?*/
void
CcuDirPath :: Prepend (const char* dir)
{
	CcuList::Prepend (NewString (dir));
}

/*?nodoc?*/
int
CcuDirPath :: Remove (const char* d)
{
	int removed = 0;
	CcuListIter li (*this);
	CcuListIter lj (*this);
	while (++li) {
		int remove = (strcmp (d, (const char*) *li) == 0);
		if (remove) {
			++removed;
			RemoveAfter (lj);
		} else
			++lj;
	}
	return removed;
}

/*?
Search a path for a readable file with name \var{fn}. The absolute path name 
is returned, or 0 if the search fails.
If the search succeeds, \var{alloc} determines whether
the returned name should be a dynamically allocated string or not.
If \var{alloc} is not null, an allocation is performed.
If \var{alloc} is 0, and \var{fn} was already a full pathname, \var{fn} is returned.
If \var{alloc} is 0, and \var{fn} was a relative pathname found in the search path,
a named stored in a static buffer is returned.
A pathname is considered a full pathname (and hence is not looked up in
the search path) if it begins with ``\com{/}'', ``\com{./}'' or ``\com{../}''.
?*/
const char*
CcuDirPath :: FindFile (const char* fn, alloc_mode alloc)
{
	if (!fn)
		return 0;

	static char absfn[128];
	char *dir;
	char* pathend;

	/* If fn begins with '/', './' or '../' it is considered absolute */
	
	register const char* p = fn;
	while (*p == '.')
		++p;
	if ((p - fn) <= 2 && (! *p || *p == '/'))
		return access (fn, R_OK) ? 0 : alloc ? NewString (fn) : fn;

	/* Else, look through search path */

	CcuListIter it (*this) ; 
	while (++it) {
		dir = (char*) (*it);
		strcpy (absfn, dir);
		pathend = absfn + strlen (dir);
		if (*(pathend-1) != '/') {
			*pathend++ = '/';
			*pathend = '\0';
		}
		strcat (absfn, fn);
		if (access (absfn, R_OK) == 0)
			return (alloc != DontAllocNames) ? NewString (absfn) : absfn;
	}
	return 0;	
}

#ifdef DOC

/*?
This function is similar to the previous one.
It uses the allocation mode of this \typ{CcuDirPath} to decide whether to allocate
the returned string or not.
?*/
const char*
CcuDirPath :: FindFile (const char* fn)
{
}

/*?
Set the allocation mode of this dir path to \var{alloc}.
?*/
void
CcuDirPath :: SetAlloc (alloc_mode alloc)
{
}

#endif	/* DOC */

/*?
Add the directories contained in a string of the same form as the environment variable ``PATH'',
i.e. dir1:dir2:\ldots:dirN. \var{sep} is the character separating the different components.
Its default value is ``:''.
?*/
void
CcuDirPath :: AppendEnvPath (const char* path, char sep)
{
	const char *dir, *next = path;
	
	for (dir = path; next; dir = next+1) {
		next = strchr (dir, sep);
		if (next != dir)
			CcuList::Append (next ? NewString (dir, next-dir) : NewString (dir));
	}
}