]> andersk Git - splint.git/blame - src/fileLib.c
Merged code tree with Dave Evans's version. Many changes to numberous to list....
[splint.git] / src / fileLib.c
CommitLineData
28bf4b0b 1/*
2** LCLint - annotation-assisted static program checker
3** Copyright (C) 1994-2001 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 lclint: lclint-request@cs.virginia.edu
21** To report a bug: lclint-bug@cs.virginia.edu
22** For more information: http://lclint.cs.virginia.edu
23*/
24/*
25** fileLib.c
26*/
27
28# include "lclintMacros.nf"
29# include "basic.h"
30# include "portab.h"
31
32bool
33fileLib_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
45bool
46fileLib_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# ifndef NOLCL
77/*@only@*/ cstring fileLib_removePath (cstring s)
78{
79 /*@access cstring@*/
80 char *t;
81
82 if (cstring_isUndefined (s)) {
83 return cstring_undefined;
84 }
85
86 t = strrchr (s, CONNECTCHAR);
87
88 if (t == NULL) return (mstring_copy (s));
89 else return (mstring_copy (t + 1));
90 /*@noaccess cstring@*/
91}
92# endif
93
94/*@only@*/ cstring
95fileLib_removePathFree (/*@only@*/ cstring s)
96{
97 /*@access cstring@*/
98 char *t;
99
100
101 if (cstring_isUndefined (s)) {
102 return cstring_undefined;
103 }
104
105 t = strrchr (s, CONNECTCHAR);
106
107# ifdef ALTCONNECTCHAR
108 {
109 char *at = strrchr (s, ALTCONNECTCHAR);
110 if (t == NULL || (at > t)) {
111 t = at;
112 }
113 }
114# endif
115
116 if (t == NULL)
117 {
118 return (s);
119 }
120 else
121 {
122 char *res = mstring_copy (t + 1);
123 mstring_free (s);
124 return res;
125 }
126 /*@noaccess cstring@*/
127}
128
129/*@only@*/ cstring
130fileLib_removeAnyExtension (cstring s)
131{
132 /*@access cstring@*/
133 char *ret;
134 char *t;
135
136
137 if (cstring_isUndefined (s)) {
138 return cstring_undefined;
139 }
140
141 t = strrchr (s, '.');
142
143 if (t == (char *) 0)
144 {
145 return mstring_copy (s);
146 }
147
148 /*@-mods@*/
149 *t = '\0';
150 ret = mstring_copy (s);
151 *t = '.';
152 /*@=mods@*/ /* modification is undone */
153
154 return ret;
155 /*@noaccess cstring@*/
156}
157
158/*@only@*/ cstring
159fileLib_addExtension (/*@temp@*/ cstring s, cstring suffix)
160{
161 /*@access cstring@*/
162 llassert (cstring_isDefined (s));
163
164 if (strrchr (s, '.') == (char *) 0)
165 {
166 /* <<< was mstring_concatFree1 --- bug detected by lclint >>> */
167 return (cstring_concat (s, suffix));
168 }
169 else
170 {
171 return cstring_copy (s);
172 }
173}
174
175bool fileLib_hasExtension (cstring s, cstring ext)
176{
177 return cstring_equal (fileLib_getExtension (s), ext);
178}
179
180/*@observer@*/ cstring fileLib_getExtension (/*@returned@*/ cstring s)
181{
182 llassert (cstring_isDefined (s));
183
184 /*@access cstring@*/
185 return (strrchr(s, '.'));
186 /*@noaccess cstring@*/
187}
188
189cstring removePreDirs (cstring s)
190{
191 /*@access cstring@*/
192
193 llassert (cstring_isDefined (s));
194
195 while (*s == '.' && *(s + 1) == CONNECTCHAR)
196 {
197 s += 2;
198 }
199
200# if defined(OS2) || defined(MSDOS)
201 /* remove remainders from double path delimiters... */
202 while (*s == CONNECTCHAR)
203 {
204 ++s;
205 }
206# endif /* !defined(OS2) && !defined(MSDOS) */
207
208 return s;
209 /*@noaccess cstring@*/
210}
211
212bool isHeaderFile (cstring fname)
213{
214 cstring ext = fileLib_getExtension (fname);
215
216 return (cstring_equalLit (ext, ".h")
217 || cstring_equalLit (ext, ".H")
218 || cstring_equal (ext, LH_EXTENSION));
219}
220
221cstring fileLib_cleanName (cstring s)
222{
223 if (cstring_equalPrefix (s, "./"))
224 {
225 return cstring_copySegment (s, 2, cstring_length (s) - 1);
226 }
227
228
229
230 return cstring_copy (s);
231}
This page took 0.105048 seconds and 5 git commands to generate.