]> andersk Git - splint.git/blame - src/fileLib.c
Fixed inclusion problems with osd.h.
[splint.git] / src / fileLib.c
CommitLineData
28bf4b0b 1/*
11db3170 2** Splint - annotation-assisted static program checker
c59f5181 3** Copyright (C) 1994-2003 University of Virginia,
28bf4b0b 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**
155af98d 20** For information on splint: info@splint.org
21** To report a bug: splint-bug@splint.org
11db3170 22** For more information: http://www.splint.org
28bf4b0b 23*/
24/*
25** fileLib.c
26*/
27
1b8ae690 28# include "splintMacros.nf"
28bf4b0b 29# include "basic.h"
8b63eb37 30# include "osd.h"
28bf4b0b 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
28bf4b0b 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}
28bf4b0b 91
92/*@only@*/ cstring
93fileLib_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
128fileLib_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
157fileLib_addExtension (/*@temp@*/ cstring s, cstring suffix)
158{
159 /*@access cstring@*/
160 llassert (cstring_isDefined (s));
161
162 if (strrchr (s, '.') == (char *) 0)
163 {
1b8ae690 164 /* <<< was mstring_concatFree1 --- bug detected by splint >>> */
28bf4b0b 165 return (cstring_concat (s, suffix));
166 }
167 else
168 {
169 return cstring_copy (s);
170 }
171}
172
173bool 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
187cstring 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
210bool 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
219cstring fileLib_cleanName (cstring s)
220{
68de3f33 221 if (cstring_equalPrefixLit (s, "./"))
28bf4b0b 222 {
b73d1009 223 cstring res = cstring_copySegment (s, 2, cstring_length (s) - 1);
224 cstring_free (s);
225 return res;
28bf4b0b 226 }
227
b73d1009 228 return s;
28bf4b0b 229}
This page took 0.087072 seconds and 5 git commands to generate.