source: git/src/filename.c @ 0a603b6

RELEASE/1.0RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereostereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since 0a603b6 was 6ba8d69, checked in by Olly Betts <olly@…>, 26 years ago

minor

git-svn-id: file:///home/survex-svn/survex/trunk@173 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 5.3 KB
Line 
1/* OS dependent filename manipulation routines */
2
3#ifdef HAVE_CONFIG_H
4# include <config.h>
5#endif
6
7#include "filename.h"
8#include <string.h>
9
10/* FIXME: finish sorting out safe_fopen vs fopenWithPthAndExt... */
11
12/* Wrapper for fopen which throws a fatal error if it fails.
13 * Some versions of fopen() are quite happy to open a directory.
14 * We aren't, so catch this case. */
15extern FILE *
16safe_fopen(const char *fnm, const char *mode)
17{
18   FILE *f;
19   if (fDirectory(fnm))
20      fatalerror(/*Filename '%s' refers to directory*/44, fnm);
21
22   f = fopen(fnm, mode);
23   if (!f) fatalerror(mode[0] == 'w' ?
24                      /*Failed to open output file*/47 :
25                      /*Couldn't open data file*/24, fnm);
26   return f;
27}
28
29extern FILE *
30safe_fopen_with_ext(const char *fnm, const char *ext, const char *mode)
31{
32   FILE *f;
33   char *p;
34   p = add_ext(fnm, ext);
35   f = safe_fopen(p, mode);
36   osfree(p);
37   return f;
38}
39
40static FILE *
41fopen_not_dir(const char *fnm, const char *mode)
42{
43   if (fDirectory(fnm)) return NULL;
44   return fopen(fnm, mode);
45}
46
47extern char * FAR
48path_from_fnm(const char *fnm)
49{
50   char *pth;
51   const char *lf;
52   int lenpth = 0;
53
54   lf = strrchr(fnm, FNM_SEP_LEV);
55#ifdef FNM_SEP_LEV2
56   if (!lf) lf = strrchr(fnm, FNM_SEP_LEV2);
57#endif
58#ifdef FNM_SEP_DRV
59   if (!lf) lf = strrchr(fnm, FNM_SEP_DRV);
60#endif
61   if (lf) lenpth = lf - fnm + 1;
62
63   pth = osmalloc(lenpth + 1);
64   memcpy(pth, fnm, lenpth);
65   pth[lenpth] = '\0';
66
67   return pth;
68}
69
70extern char *
71base_from_fnm(const char *fnm)
72{
73   char *p;
74   
75   p = strrchr(fnm, FNM_SEP_EXT);
76   /* Trim off any leaf extension, but dirs can have extensions too */
77   if (p && !strrchr(p, FNM_SEP_LEV)
78#ifdef FNM_SEP_LEV2
79       && !strrchr(p, FNM_SEP_LEV2)
80#endif
81       ) {
82      size_t len = p - fnm;
83
84      p = osmalloc(len + 1);
85      memcpy(p, fnm, len);
86      p[len] = '\0';
87      return p;
88   }
89
90   return osstrdup(fnm);
91}
92
93extern char *
94baseleaf_from_fnm(const char *fnm)
95{
96   const char *p;
97   char *q;
98   size_t len;
99   
100   p = fnm;
101   q = strrchr(p, FNM_SEP_LEV);
102   if (q) p = q + 1;
103#ifdef FNM_SEP_LEV2
104   q = strrchr(p, FNM_SEP_LEV2);
105   if (q) p = q + 1;
106#endif
107   
108   q = strrchr(p, FNM_SEP_EXT);
109   if (q) len = q - p; else len = strlen(p);
110
111   q = osmalloc(len + 1);
112   memcpy(q, p, len);
113   q[len] = '\0';
114   return q;
115}
116
117extern char * FAR
118leaf_from_fnm(const char *fnm)
119{
120   char *lf;
121   lf = strrchr(fnm, FNM_SEP_LEV);
122   if (lf != NULL
123#ifdef FNM_SEP_LEV2
124       || (lf = strrchr(fnm, FNM_SEP_LEV2)) != NULL
125#endif
126#ifdef FNM_SEP_DRV
127       || (lf = strrchr(fnm, FNM_SEP_DRV)) != NULL
128#endif
129       ) {
130      return osstrdup(lf + 1);
131   }
132   return osstrdup(fnm);
133}
134
135/* Make fnm from pth and lf, inserting an FNM_SEP_LEV if appropriate */
136extern char * FAR
137use_path(const char *pth, const char *lf)
138{
139   char *fnm;
140   int len, len_total;
141   bool fAddSep = fFalse;
142
143   len = strlen(pth);
144   len_total = len + strlen(lf) + 1;
145
146   /* if there's a path and it doesn't end in a separator, insert one */
147   if (len && pth[len - 1] != FNM_SEP_LEV) {
148#ifdef FNM_SEP_LEV2
149      if (pth[len - 1] != FNM_SEP_LEV2) {
150#endif
151#ifdef FNM_SEP_DRV
152         if (pth[len - 1] != FNM_SEP_DRV) {
153#endif
154            fAddSep = fTrue;
155            len_total++;
156#ifdef FNM_SEP_DRV
157         }
158#endif
159#ifdef FNM_SEP_LEV2
160      }
161#endif
162   }
163
164   fnm = osmalloc(len_total);
165   strcpy(fnm, pth);
166   if (fAddSep) fnm[len++] = FNM_SEP_LEV;
167   strcpy(fnm + len, lf);
168   return fnm;
169}
170
171/* Add ext to fnm, inserting an FNM_SEP_EXT if appropriate */
172extern char * FAR
173add_ext(const char *fnm, const char *ext)
174{
175   char * fnmNew;
176   int len, len_total;
177#ifdef FNM_SEP_EXT
178   bool fAddSep = fFalse;
179#endif
180
181   len = strlen(fnm);
182   len_total = len + strlen(ext) + 1;
183#ifdef FNM_SEP_EXT
184   if (ext[0] != FNM_SEP_EXT) {
185      fAddSep = fTrue;
186      len_total++;
187   }
188#endif
189
190   fnmNew = osmalloc(len_total);
191   strcpy(fnmNew, fnm);
192#ifdef FNM_SEP_EXT
193   if (fAddSep) fnmNew[len++] = FNM_SEP_EXT;
194#endif
195   strcpy(fnmNew + len, ext);
196   return fnmNew;
197}
198
199/* fopen file, found using pth and fnm
200 * pfnmUsed used to return filename used to open file (ignored if NULL)
201 * or NULL if file didn't open
202 */
203extern FILE FAR *
204fopenWithPthAndExt(const char * pth, const char * fnm, const char * szExt,
205                   const char * szMode, char **pfnmUsed)
206{
207   char *fnmFull = NULL;
208   FILE *fh = NULL;
209   bool fAbs;
210
211   /* if no pth treat fnm as absolute */
212   fAbs = (pth == NULL || *pth == '\0' || fAbsoluteFnm(fnm));
213
214   /* if appropriate, try it without pth */
215   if (fAbs) {
216      fh = fopen_not_dir(fnm, szMode);
217      if (fh) {
218         if (pfnmUsed) fnmFull = osstrdup(fnm);
219      } else {
220         if (szExt && *szExt) {
221            /* we've been given an extension so try using it */
222            fnmFull = add_ext(fnm, szExt);
223            fh = fopen_not_dir(fnmFull, szMode);
224         }
225      }
226   } else {
227      /* try using path given - first of all without the extension */
228      fnmFull = use_path(pth, fnm);
229      fh = fopen_not_dir(fnmFull, szMode);
230      if (!fh) {
231         if (szExt && *szExt) {
232            /* we've been given an extension so try using it */
233            char *fnmTmp;
234            fnmTmp = fnmFull;
235            fnmFull = add_ext(fnmFull, szExt);
236            osfree(fnmTmp);
237            fh = fopen_not_dir(fnmFull, szMode);
238         }
239      }
240   }
241
242   /* either it opened or didn't. If not, fh == NULL from fopen_not_dir() */
243
244   /* free name if it didn't open or name isn't wanted */
245   if (fh == NULL || pfnmUsed == NULL) osfree(fnmFull);
246   if (pfnmUsed) *pfnmUsed = (fh ? fnmFull : NULL);
247   return fh;
248}
Note: See TracBrowser for help on using the repository browser.