]> andersk Git - splint.git/blob - src/fileLib.c
Fixed inclusion problems with osd.h.
[splint.git] / src / fileLib.c
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 **         Massachusetts Institute of Technology
5 **
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
10 ** 
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ** General Public License for more details.
15 ** 
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
19 **
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
23 */
24 /*
25 ** fileLib.c
26 */
27
28 # include "splintMacros.nf"
29 # include "basic.h"
30 # include "osd.h"
31
32 bool
33 fileLib_isCExtension (cstring ext)
34 {
35   return (cstring_equalLit (ext, ".c") 
36           || cstring_equalLit (ext, ".C") 
37           || cstring_equalLit (ext, ".h")
38           || cstring_equalLit (ext, ".lh")
39           || cstring_equalLit (ext, ".xh")
40           || cstring_equalLit (ext, ".H")
41           || cstring_equalLit (ext, ".y")
42           || cstring_equalLit (ext, ".l"));
43 }
44
45 bool
46 fileLib_isLCLFile (cstring s)
47 {
48   return (fileLib_hasExtension (s, LCL_EXTENSION));
49 }
50
51 /*@only@*/ cstring fileLib_withoutExtension (/*@temp@*/ cstring s, cstring suffix)
52 {
53   /*@access cstring@*/
54   char *t;
55   char *s2;
56
57   if (cstring_isUndefined (s)) {
58     return cstring_undefined;
59   }
60
61   t = strrchr (s, '.');
62   if (t == (char *) 0 || !mstring_equal (t, suffix))
63     {
64       return mstring_copy (s);
65     }
66
67   /*@-mods@*/ 
68   *t = '\0';
69   s2 = mstring_copy (s);
70   *t = '.';
71   /*@=mods@*/  /* Modification is undone. */
72   return s2;
73   /*@noaccess cstring@*/
74 }
75
76 /*@only@*/ cstring fileLib_removePath (cstring s)
77 {
78   /*@access cstring@*/
79   char *t;
80
81   if (cstring_isUndefined (s)) {
82     return cstring_undefined;
83   }
84
85   t = strrchr (s, CONNECTCHAR);
86
87   if (t == NULL) return (mstring_copy (s));
88   else return (mstring_copy (t + 1));
89   /*@noaccess cstring@*/
90 }
91
92 /*@only@*/ cstring
93 fileLib_removePathFree (/*@only@*/ cstring s)
94 {
95   /*@access cstring@*/
96   char *t;
97
98
99   if (cstring_isUndefined (s)) {
100     return cstring_undefined;
101   }
102
103   t = strrchr (s, CONNECTCHAR);
104
105 # ifdef ALTCONNECTCHAR
106   {
107     char *at = strrchr (s, ALTCONNECTCHAR);
108     if (t == NULL || (at > t)) {
109       t = at;
110     }
111   }
112 # endif
113
114   if (t == NULL) 
115     {
116       return (s);
117     }
118   else
119     {
120       char *res = mstring_copy (t + 1);
121       mstring_free (s);
122       return res;
123     }
124   /*@noaccess cstring@*/
125 }
126
127 /*@only@*/ cstring
128 fileLib_removeAnyExtension (cstring s)
129 {
130   /*@access cstring@*/
131   char *ret;
132   char *t;
133
134
135   if (cstring_isUndefined (s)) {
136     return cstring_undefined;
137   } 
138
139   t = strrchr (s, '.');
140
141   if (t == (char *) 0)
142     {
143       return mstring_copy (s);
144     }
145
146   /*@-mods@*/
147   *t = '\0';
148   ret = mstring_copy (s);
149   *t = '.';
150   /*@=mods@*/ /* modification is undone */
151
152   return ret;
153   /*@noaccess cstring@*/
154 }
155
156 /*@only@*/ cstring
157 fileLib_addExtension (/*@temp@*/ cstring s, cstring suffix)
158 {
159   /*@access cstring@*/
160   llassert (cstring_isDefined (s));
161
162   if (strrchr (s, '.') == (char *) 0)
163     {
164       /* <<< was mstring_concatFree1 --- bug detected by splint >>> */
165       return (cstring_concat (s, suffix));
166     }
167   else
168     {
169       return cstring_copy (s);
170     }
171 }
172
173 bool fileLib_hasExtension (cstring s, cstring ext)
174 {
175   return cstring_equal (fileLib_getExtension (s), ext);
176 }
177
178 /*@observer@*/ cstring fileLib_getExtension (/*@returned@*/ cstring s)
179 {
180   llassert (cstring_isDefined (s));
181
182   /*@access cstring@*/
183   return (strrchr(s, '.'));
184   /*@noaccess cstring@*/
185 }
186
187 cstring removePreDirs (cstring s)
188 {
189   /*@access cstring@*/
190
191   llassert (cstring_isDefined (s));
192
193   while (*s == '.' && *(s + 1) == CONNECTCHAR) 
194     {
195       s += 2;
196     }
197
198 # if defined(OS2) || defined(MSDOS)
199   /* remove remainders from double path delimiters... */
200   while (*s == CONNECTCHAR) 
201     {
202       ++s;
203     }
204 # endif /* !defined(OS2) && !defined(MSDOS) */
205
206   return s;
207   /*@noaccess cstring@*/
208 }
209
210 bool isHeaderFile (cstring fname)
211 {
212   cstring ext = fileLib_getExtension (fname);
213   
214   return (cstring_equalLit (ext, ".h")
215           || cstring_equalLit (ext, ".H")
216           || cstring_equal (ext, LH_EXTENSION));
217 }
218
219 cstring fileLib_cleanName (cstring s)
220 {
221   if (cstring_equalPrefixLit (s, "./")) 
222     {
223       cstring res = cstring_copySegment (s, 2, cstring_length (s) - 1);
224       cstring_free (s);
225       return res;
226     }
227
228   return s;
229 }
This page took 0.124465 seconds and 5 git commands to generate.