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

Contents of /trunk/ascend/general/ospath.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2552 - (show annotations) (download) (as text)
Thu Feb 2 20:54:40 2012 UTC (6 years, 7 months ago) by jpye
File MIME type: text/x-chdr
File size: 9474 byte(s)
Fix detection of SUNDIALS in MinGW-w64.
Fix paths in test_ida and test_lsode.
1 /* ASCEND modelling environment
2 Copyright (C) 2006-2007 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 Includes (fairly basic) support for environment variable expansion here for
32 use in cases like "$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 <ascend/utilities/config.h>
50 # include "platform.h"
51 # include "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 # ifndef stat
65 # include <sys/stat.h>
66 # else
67 # error "STAT already defined..."
68 # endif
69 # endif
70 #endif
71
72 /** @addtogroup general_ospath General File Pathname Manipulation
73 @{
74 */
75
76 #ifndef PATH_MAX
77 # define PATH_MAX 1023
78 #endif
79
80 #ifdef __WIN32__
81 # define WINPATHS
82 # define OSPATH_DIV ";"
83 #else
84 # define OSPATH_DIV ":"
85 #endif
86
87 #ifdef _MSC_VER
88 typedef struct _stat ospath_stat_t;
89 #else
90 typedef struct stat ospath_stat_t;
91 #endif
92
93 struct FilePath;
94
95 /**
96 Create a new ospath object from a string. This will
97 normalise the path and attempt to add a drive-prefix
98 if one is missing.
99 */
100 ASC_DLLSPEC struct FilePath *ospath_new(const char *path);
101
102 /**
103 Create but with no 'cleanup', and no fixing of / vs \.
104 */
105 ASC_DLLSPEC struct FilePath *ospath_new_noclean(const char *path);
106
107 /**
108 Free an ospath
109 */
110 ASC_DLLSPEC void ospath_free(struct FilePath *);
111
112 /**
113 Free a string allocated from ospath.
114 */
115 ASC_DLLSPEC void ospath_free_str(char *str);
116
117 /**
118 Create a new ospath object from a string, assuming
119 that standard forward-slash paths are used.
120 */
121 ASC_DLLSPEC struct FilePath *ospath_new_from_posix(const char *posixpath);
122
123 /**
124 Path with *VERY SIMPLE* environment variable expansion.
125 You must specify what 'getenv' function should be used.
126 */
127 ASC_DLLSPEC struct FilePath *ospath_new_expand_env(const char *path, GetEnvFn *getenvptr);
128
129 /**
130 This function cleans up the path string used to construct the FilePath object:
131 1. Get rid of multiple / 's one after the other...
132
133 ie. "///usr/bin///hello/////there// --> "/usr/bin/hello/there/"
134
135 2. Resolve a leading tilde (~) to the current user's HOME path
136
137 3. Remove redundant /./ in middle of path
138
139 4. Remove redundant dir/.. in path
140
141 5. Environment substitution??
142
143 6. On windows, drive reference if not specified
144
145 7. What about \\server\path and URLs, gnomefs, etc?
146 */
147 ASC_DLLSPEC void ospath_cleanup(struct FilePath *);
148
149 /**
150 Create a copy of a FilePath. This allocated new memory, so you must
151 separately free both the original fp as well as the new copy, when the time comes.
152 */
153 ASC_DLLSPEC struct FilePath *ospath_new_copy(const struct FilePath *);
154
155 /**
156 Check that the created FilePath was valid (i.e. able
157 to be parsed. Doesn't check that the directory/file
158 actually exists.)
159 */
160 int ospath_isvalid(const struct FilePath *fp);
161
162 /**
163 Return the FilePath in the form of a string.
164 You must ASC_FREE the allocated string when you don't need it any more.
165 */
166 ASC_DLLSPEC char *ospath_str(const struct FilePath *fp);
167
168 /**
169 Return the FilePath in the string location given.
170 If the user has allocated a local array before calling ospath_strncpy,
171 then this allows the FREE(dest) call to be avoided.
172
173 @param dest the location of allocated storage space where the FilePath
174 will be written.
175 @param destsize the amount of allocated string space at dest.
176 */
177 ASC_DLLSPEC void ospath_strncpy(struct FilePath *fp,char *dest, int destsize);
178
179 /**
180 Append string from FilePath to the already-allocated string 'dest',
181 while ensuring that the 'dest' strings *total* size doesn't exceed
182 destsize.
183 */
184 ASC_DLLSPEC void ospath_strcat(struct FilePath *fp,char *dest, int destsize);
185
186 /**
187 Output a filepath to a file (ie print the filename fp)
188 */
189 void ospath_fwrite(struct FilePath *fp, FILE *dest);
190
191 /**
192 Write out the internal structure of the FilePath object
193 to stderr.
194 */
195 ASC_DLLSPEC void ospath_debug(struct FilePath *fp);
196
197 /**
198 Return length of path string
199 */
200 unsigned int ospath_length(struct FilePath *fp);
201
202 /**
203 Create a new path that is the direct parent of this one.
204 */
205 ASC_DLLSPEC struct FilePath *ospath_getparent(struct FilePath *fp);
206
207 /**
208 Return a new path object which is a parent path of the current path up to the specified depth.
209 If the specifed depth is >= to the current path's depth, then a copy of the current path object is returned.
210
211 ie.
212 FilePath("/lev1/lev2/lev3/lev4/lev5").GetParentAtDepthN(3).c_str()
213 returns
214 "/lev1/lev2/lev3/"
215 */
216 struct FilePath *ospath_getparentatdepthn(struct FilePath *fp, unsigned nDepth);
217
218 /**
219 Return then name of the bottom most level path entry (includes any extension)
220
221 The caller owns *and must free* the returned string.
222 */
223 ASC_DLLSPEC char *ospath_getbasefilename(struct FilePath *fp);
224
225 /**
226 Retrieve the path's bottom level filename without the extension.
227 A path that ends in a slash will be assumed to be directory, so the
228 file stem will be NULL (ie not a file)
229 */
230 ASC_DLLSPEC char *ospath_getfilestem(struct FilePath *fp);
231
232 /**
233 retrieve the paths extension (if it has one) does not include the dot
234 */
235 ASC_DLLSPEC char *ospath_getfileext(struct FilePath *fp);
236
237 /**
238 Return a new FilePath containing the directory component of the path
239 (everything up the the last slash).
240
241 Returns an empty path if there is no slash.
242 */
243 ASC_DLLSPEC struct FilePath *ospath_getdir(struct FilePath *fp);
244
245 /**
246 Return a new FilePath containin the absolute path reference generated from
247 the current path. This just means that if the path isn't already starting
248 with a slash, it will be concatenated with the current working directory.
249 */
250 ASC_DLLSPEC struct FilePath *ospath_getabs(const struct FilePath *fp);
251
252 /**
253 Function returns true if the current path is the root directory, otherwise it returns false.
254 */
255 int ospath_isroot(struct FilePath *fp);
256
257 /**
258 Return the current paths' depth
259 ie. "/usr/some directory with spaces in it/hello"
260 returns a depth value of 3
261 */
262 unsigned ospath_depth(struct FilePath *fp);
263
264 /**
265 Return the root path
266 */
267 ASC_DLLSPEC struct FilePath *ospath_root(struct FilePath *fp);
268
269 ASC_DLLSPEC int ospath_cmp(struct FilePath *fp1, struct FilePath *fp2);
270
271 ASC_DLLSPEC struct FilePath *ospath_concat(const struct FilePath *fp1, const struct FilePath *fp2);
272
273 ASC_DLLSPEC void ospath_append(struct FilePath *fp, struct FilePath *fp1);
274
275 /**
276 File-open function. Simply a wrapper about the 'fopen' call, except
277 that if the FilePath is not 'valid' it will return NULL without
278 attempting to open.
279 */
280 ASC_DLLSPEC FILE *ospath_fopen(struct FilePath *fp, const char *mode);
281
282 /**
283 Stat function. Simply a wrappen around the 'stat' call.
284 The exception is that if the FilePath is not 'valid', -1 is returned
285 and no call to 'stat' is made.
286
287 @return 0 on success
288 */
289 ASC_DLLSPEC int ospath_stat(struct FilePath *fp,ospath_stat_t *buf);
290
291 /*
292 Get the current working directory from the operating system, and return
293 it in the form of a FilePath. You must free the FilePath object when you're
294 finished with it.
295 */
296 ASC_DLLSPEC struct FilePath *ospath_getcwd(void);
297
298 /*
299 Change the current working directory to that specified in fp. No checks
300 are made that the directory actually exists. The return status from the
301 system 'chdir' function is returned (should be 0 for no error)
302 */
303 ASC_DLLSPEC int ospath_chdir(struct FilePath *fp);
304
305 /*------------------------*/
306 /* SEARCH PATH FUNCTIONS*/
307
308 ASC_DLLSPEC struct FilePath **ospath_searchpath_new(const char *path);
309
310 ASC_DLLSPEC void ospath_searchpath_free(struct FilePath **searchpath);
311
312 typedef int (FilePathTestFn)(struct FilePath *,void *);
313
314 ASC_DLLSPEC int ospath_searchpath_length(struct FilePath **searchpath);
315
316 /**
317 @return pointer to path component in which testfn passed, or else
318 NULL if no component passed the test. There is no need to free the returned
319 path component, as it will be freed when you (later) free the searchpath
320 param.
321 */
322 ASC_DLLSPEC struct FilePath *ospath_searchpath_iterate(
323 struct FilePath **searchpath
324 , FilePathTestFn *testfn
325 , void *searchdata
326 );
327
328 /* @} */
329
330 #endif

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