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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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