/[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 585 - (show annotations) (download) (as text)
Wed May 10 17:02:21 2006 UTC (18 years, 1 month ago) by johnpye
File MIME type: text/x-chdr
File size: 7361 byte(s)
Making ospath accessible from other libraries. In progress.
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 (.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
40 #ifndef OSPATH_H
41 #define OSPATH_H
42
43 #ifdef TEST
44 # define ASC_DLLSPEC(T) T
45 # include "env.h"
46 #else
47 # include <utilities/config.h>
48 # include <utilities/ascConfig.h>
49 # include <utilities/ascMalloc.h>
50 # include "env.h"
51 #endif
52
53 #if defined(__WIN32__) && !defined(__MINGW32__)
54 # include <direct.h>
55 # include <stdlib.h>
56 #else
57 # ifdef __MINGW32__
58 # include <io.h>
59 # include <limits.h>
60 # endif
61 #endif
62
63 #ifndef PATH_MAX
64 # define PATH_MAX 1023
65 #endif
66
67 struct FilePath;
68
69 /**
70 Create a new ospath object from a string. This will
71 normalise the path and attempt to add a drive-prefix
72 if one is missing.
73 */
74 ASC_DLLSPEC(struct FilePath *) ospath_new(const char *path);
75
76 ASC_DLLSPEC(struct FilePath *) ospath_new_noclean(const char *path);
77
78 /**
79 Free an ospath
80 */
81 ASC_DLLSPEC(void) ospath_free(struct FilePath *);
82
83 /**
84 Free a string allocated from ospath.
85 */
86 ASC_DLLSPEC(void) ospath_free_str(char *str);
87
88 /**
89 Create a new ospath object from a string, assuming
90 that standard forward-slash paths are used.
91 */
92 struct FilePath *ospath_new_from_posix(const char *posixpath);
93
94 /**
95 Path with *VERY SIMPLE* environment variable expansion.
96 You must specify what 'getenv' function should be used.
97 */
98 ASC_DLLSPEC(struct FilePath *) ospath_new_expand_env(const char *path, GetEnvFn *getenvptr);
99
100 /**
101 This function cleans up the path string used to construct the FilePath object:
102 1. Get rid of multiple / 's one after the other...
103
104 ie. "///usr/bin///hello/////there// --> "/usr/bin/hello/there/"
105
106 2. Resolve a leading tilde (~) to the current user's HOME path
107
108 3. Remove redundant /./ in middle of path
109
110 4. Remove reduncant dir/.. in path
111
112 5. Environment substitution??
113
114 6. On windows, drive reference if not specified
115
116 7. What about \\server\path and URLs, gnomefs, etc?
117 */
118 ASC_DLLSPEC(void) ospath_cleanup(struct FilePath *);
119
120 /**
121 Check that the created FilePath was valid (i.e. able
122 to be parsed. Doesn't check that the directory/file
123 actually exists.)
124 */
125 int ospath_isvalid(struct FilePath *fp);
126
127 /**
128 Return the FilePath in the form of a string.
129 You must FREE the allocated string when you don't need it any more.
130 */
131 ASC_DLLSPEC(char *) ospath_str(struct FilePath *fp);
132
133 /**
134 Return the FilePath in the string location given.
135 If the user has allocated a local array before calling ospath_strcpy,
136 then this allows the FREE(dest) call to be avoided.
137
138 @param dest the location of allocated storage space where the FilePath
139 will be written.
140 @param destsize the amount of allocated string space at dest.
141 */
142 ASC_DLLSPEC(void) ospath_strcpy(struct FilePath *fp,char *dest, int destsize);
143
144 /**
145 Append string from FilePath to the already-allocated string 'dest',
146 while ensuring that the 'dest' strings *total* size doesn't exceed
147 destsize.
148 */
149 ASC_DLLSPEC(void) ospath_strcat(struct FilePath *fp,char *dest, int destsize);
150
151 /**
152 Output the FilePath to a file
153 */
154 void ospath_fwrite(struct FilePath *fp, FILE *dest);
155
156 /**
157 Write out the internal structure of the FilePath object
158 to stderr.
159 */
160 void ospath_debug(struct FilePath *fp);
161
162 /**
163 Return length of path string
164 */
165 unsigned int ospath_length(struct FilePath *fp);
166
167 /**
168 Create a new path that is the direct parent of this one.
169 */
170 struct FilePath *ospath_getparent(struct FilePath *fp);
171
172 /**
173 Return a new path object which is a parent path of the current path up to the specified depth.
174 If the specifed depth is >= to the current path's depth, then a copy of the current path object is returned.
175
176 ie.
177 FilePath("/lev1/lev2/lev3/lev4/lev5").GetParentAtDepthN(3).c_str()
178 returns
179 "/lev1/lev2/lev3/"
180 */
181 struct FilePath *ospath_getparentatdepthn(struct FilePath *fp, unsigned nDepth);
182
183 /**
184 Return then name of the bottom most level path entry (includes any extension)
185 */
186 char *ospath_getbasefilename(struct FilePath *fp);
187
188 /**
189 Retrieve the path's bottom level filename without the extension.
190 A path that ends in a slash will be assumed to be directory, so the
191 file stem will be NULL (ie not a file)
192 */
193 char *ospath_getfilestem(struct FilePath *fp);
194
195 /**
196 retrieve the paths extension (if it has one) does not include the dot
197 */
198 char *ospath_getfileext(struct FilePath *fp);
199
200 /**
201 Return a new FilePath containing the directory component of the path
202 (everything up the the last slash).
203 */
204 ASC_DLLSPEC(struct FilePath *) ospath_getdir(struct FilePath *fp);
205
206 /**
207 Function returns true if the current path is the root directory, otherwise it returns false.
208 */
209 int ospath_isroot(struct FilePath *fp);
210
211 /**
212 Return the current paths' depth
213 ie. "/usr/some directory with spaces in it/hello"
214 returns a depth value of 3
215 */
216 unsigned ospath_depth(struct FilePath *fp);
217
218 /**
219 Return the root path
220 */
221 struct FilePath *ospath_root(struct FilePath *fp);
222
223 int ospath_cmp(struct FilePath *fp1, struct FilePath *fp2);
224
225 ASC_DLLSPEC(struct FilePath *) ospath_concat(struct FilePath *fp1, struct FilePath *fp2);
226
227 ASC_DLLSPEC(void) ospath_append(struct FilePath *fp, struct FilePath *fp1);
228
229 /**
230 File-open function. Simply a wrapper about the 'fopen' call, except
231 that if the FilePath is not 'valid' it will return NULL without
232 attempting to open.
233 */
234 ASC_DLLSPEC(FILE *) ospath_fopen(struct FilePath *fp, const char *mode);
235
236 //------------------------
237 // SEARCH PATH FUNCTIONS
238
239 struct FilePath **ospath_searchpath_new(const char *path);
240
241 typedef int (FilePathTestFn)(struct FilePath *,void *);
242
243 /**
244 @return pointer to path component in which testfn passed, or else
245 NULL if no component passed the test.
246 */
247 struct FilePath *ospath_searchpath_iterate(
248 struct FilePath **searchpath
249 , FilePathTestFn *testfn
250 , void *searchdata
251 );
252
253 #endif

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