/[ascend]/trunk/base/generic/general/ospath.h
ViewVC logotype

Contents of /trunk/base/generic/general/ospath.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 721 - (show annotations) (download) (as text)
Mon Jul 3 06:22:42 2006 UTC (13 years, 6 months ago) by johnpye
File MIME type: text/x-chdr
File size: 8332 byte(s)
Added '--models' option to ascend-config.
Fixed a bug with packages.c wrt generating absolute paths to library files.
Added SCons directives to install header files in INSTALL_INCLUDE subdirs.
Changed rel.c so that init function is only run if provided in the package.
Added 'ospath_getabs' to convert relative paths into absolute paths by adding prefix of fully-resolved '.'
1 /* ASCEND modelling environment
2 Copyright (C) 2006 Carnegie Mellon University
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 *//**
19 @file
20 A C-language class for simple file-path manipulations alla python os.path.
21 Attempts to handle windows 'drive' prefixes (eg "C:") and automatic
22 substitution of forward slashes with appropriate platform-specific
23 path separators.
24
25 Has the goal of providing path-search functionality to ASCEND for
26 modules (.a4c) and external library (.so/.dll) files, etc.
27
28 Heavily modified version of C++ code from codeproject.com
29 originally written by Simon Parkinson-Bates.
30
31 @TODO add environment variable expansion here for use in case like
32 "$ASCENDTK/bitmaps" or "ascend-$VERSION$EXESUFFIX"
33
34 @NOTE this library makes no allowance for fancy escape characters.
35 You better not try to put escaped slashes into your paths, and
36 make sure you've unescaped everything before you send it to
37 ospath.
38 *//*
39 by John Pye, May 2006
40 */
41
42 #ifndef OSPATH_H
43 #define OSPATH_H
44
45 #ifdef TEST
46 # define ASC_DLLSPEC(T) T
47 # include "env.h"
48 #else
49 # include <utilities/config.h>
50 # include <utilities/ascConfig.h>
51 # include <utilities/ascMalloc.h>
52 # include "env.h"
53 #endif
54
55 #if defined(__WIN32__) && !defined(__MINGW32__)
56 # include <direct.h>
57 # include <stdlib.h>
58 #else
59 # ifdef __MINGW32__
60 # include <io.h>
61 # include <limits.h>
62 # include <sys/stat.h>
63 # else
64 # include <sys/stat.h>
65 # endif
66 #endif
67
68 #ifndef PATH_MAX
69 # define PATH_MAX 1023
70 #endif
71
72 struct FilePath;
73
74 /**
75 Create a new ospath object from a string. This will
76 normalise the path and attempt to add a drive-prefix
77 if one is missing.
78 */
79 ASC_DLLSPEC(struct FilePath *) ospath_new(const char *path);
80
81 /**
82 Create but with no 'cleanup', and no fixing of / vs \.
83 */
84 ASC_DLLSPEC(struct FilePath *) ospath_new_noclean(const char *path);
85
86 /**
87 Free an ospath
88 */
89 ASC_DLLSPEC(void) ospath_free(struct FilePath *);
90
91 /**
92 Free a string allocated from ospath.
93 */
94 ASC_DLLSPEC(void) ospath_free_str(char *str);
95
96 /**
97 Create a new ospath object from a string, assuming
98 that standard forward-slash paths are used.
99 */
100 ASC_DLLSPEC(struct FilePath *) ospath_new_from_posix(const char *posixpath);
101
102 /**
103 Path with *VERY SIMPLE* environment variable expansion.
104 You must specify what 'getenv' function should be used.
105 */
106 ASC_DLLSPEC(struct FilePath *) ospath_new_expand_env(const char *path, GetEnvFn *getenvptr);
107
108 /**
109 This function cleans up the path string used to construct the FilePath object:
110 1. Get rid of multiple / 's one after the other...
111
112 ie. "///usr/bin///hello/////there// --> "/usr/bin/hello/there/"
113
114 2. Resolve a leading tilde (~) to the current user's HOME path
115
116 3. Remove redundant /./ in middle of path
117
118 4. Remove reduncant dir/.. in path
119
120 5. Environment substitution??
121
122 6. On windows, drive reference if not specified
123
124 7. What about \\server\path and URLs, gnomefs, etc?
125 */
126 ASC_DLLSPEC(void) ospath_cleanup(struct FilePath *);
127
128 /**
129 Check that the created FilePath was valid (i.e. able
130 to be parsed. Doesn't check that the directory/file
131 actually exists.)
132 */
133 int ospath_isvalid(struct FilePath *fp);
134
135 /**
136 Return the FilePath in the form of a string.
137 You must FREE the allocated string when you don't need it any more.
138 */
139 ASC_DLLSPEC(char *) ospath_str(struct FilePath *fp);
140
141 /**
142 Return the FilePath in the string location given.
143 If the user has allocated a local array before calling ospath_strcpy,
144 then this allows the FREE(dest) call to be avoided.
145
146 @param dest the location of allocated storage space where the FilePath
147 will be written.
148 @param destsize the amount of allocated string space at dest.
149 */
150 ASC_DLLSPEC(void) ospath_strcpy(struct FilePath *fp,char *dest, int destsize);
151
152 /**
153 Append string from FilePath to the already-allocated string 'dest',
154 while ensuring that the 'dest' strings *total* size doesn't exceed
155 destsize.
156 */
157 ASC_DLLSPEC(void) ospath_strcat(struct FilePath *fp,char *dest, int destsize);
158
159 /**
160 Output the FilePath to a file
161 */
162 void ospath_fwrite(struct FilePath *fp, FILE *dest);
163
164 /**
165 Write out the internal structure of the FilePath object
166 to stderr.
167 */
168 ASC_DLLSPEC(void) ospath_debug(struct FilePath *fp);
169
170 /**
171 Return length of path string
172 */
173 unsigned int ospath_length(struct FilePath *fp);
174
175 /**
176 Create a new path that is the direct parent of this one.
177 */
178 ASC_DLLSPEC(struct FilePath *) ospath_getparent(struct FilePath *fp);
179
180 /**
181 Return a new path object which is a parent path of the current path up to the specified depth.
182 If the specifed depth is >= to the current path's depth, then a copy of the current path object is returned.
183
184 ie.
185 FilePath("/lev1/lev2/lev3/lev4/lev5").GetParentAtDepthN(3).c_str()
186 returns
187 "/lev1/lev2/lev3/"
188 */
189 struct FilePath *ospath_getparentatdepthn(struct FilePath *fp, unsigned nDepth);
190
191 /**
192 Return then name of the bottom most level path entry (includes any extension)
193
194 The caller owns *and must free* the returned string.
195 */
196 ASC_DLLSPEC(char *) ospath_getbasefilename(struct FilePath *fp);
197
198 /**
199 Retrieve the path's bottom level filename without the extension.
200 A path that ends in a slash will be assumed to be directory, so the
201 file stem will be NULL (ie not a file)
202 */
203 ASC_DLLSPEC(char *) ospath_getfilestem(struct FilePath *fp);
204
205 /**
206 retrieve the paths extension (if it has one) does not include the dot
207 */
208 ASC_DLLSPEC(char *) ospath_getfileext(struct FilePath *fp);
209
210 /**
211 Return a new FilePath containing the directory component of the path
212 (everything up the the last slash).
213
214 Returns an empty path if there is no slash.
215 */
216 ASC_DLLSPEC(struct FilePath *) ospath_getdir(struct FilePath *fp);
217
218 /**
219 Return a new FilePath containin the absolute path reference generated from
220 the current path. This just means that if the path isn't already starting
221 with a slash, it will be concatenated with the current working directory.
222 */
223 ASC_DLLSPEC(struct FilePath *) ospath_getabs(struct FilePath *fp);
224
225 /**
226 Function returns true if the current path is the root directory, otherwise it returns false.
227 */
228 int ospath_isroot(struct FilePath *fp);
229
230 /**
231 Return the current paths' depth
232 ie. "/usr/some directory with spaces in it/hello"
233 returns a depth value of 3
234 */
235 unsigned ospath_depth(struct FilePath *fp);
236
237 /**
238 Return the root path
239 */
240 ASC_DLLSPEC(struct FilePath *) ospath_root(struct FilePath *fp);
241
242 ASC_DLLSPEC(int) ospath_cmp(struct FilePath *fp1, struct FilePath *fp2);
243
244 ASC_DLLSPEC(struct FilePath *) ospath_concat(struct FilePath *fp1, struct FilePath *fp2);
245
246 ASC_DLLSPEC(void) ospath_append(struct FilePath *fp, struct FilePath *fp1);
247
248 /**
249 File-open function. Simply a wrapper about the 'fopen' call, except
250 that if the FilePath is not 'valid' it will return NULL without
251 attempting to open.
252 */
253 ASC_DLLSPEC(FILE *) ospath_fopen(struct FilePath *fp, const char *mode);
254
255 /**
256 Stat function. Simply a wrappen around the 'stat' call.
257 The exception is that if the FilePath is not 'valid', -1 is returned
258 and no call to 'stat' is made.
259 */
260 ASC_DLLSPEC(int) ospath_stat(struct FilePath *fp,struct stat *buf);
261
262 //------------------------
263 // SEARCH PATH FUNCTIONS
264
265 ASC_DLLSPEC(struct FilePath **) ospath_searchpath_new(const char *path);
266
267 ASC_DLLSPEC(void) ospath_searchpath_free(struct FilePath **searchpath);
268
269 typedef int (FilePathTestFn)(struct FilePath *,void *);
270
271 ASC_DLLSPEC(int) ospath_searchpath_length(struct FilePath **searchpath);
272
273 /**
274 @return pointer to path component in which testfn passed, or else
275 NULL if no component passed the test. There is no need to free the returned
276 path component, as it will be freed when you (later) free the searchpath
277 param.
278 */
279 ASC_DLLSPEC(struct FilePath *) ospath_searchpath_iterate(
280 struct FilePath **searchpath
281 , FilePathTestFn *testfn
282 , void *searchdata
283 );
284
285 #endif

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22