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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.668  
changed lines
  Added in v.669

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