Tue, 15 Feb 2005 19:06:37 +0100
TAG libtu-3-svn2darcs
0 | 1 | /* |
2 | * libtu/misc.c | |
3 | * | |
36 | 4 | * Copyright (c) Tuomo Valkonen 1999-2002. |
53 | 5 | * |
6 | * You may distribute and modify this library under the terms of either | |
7 | * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. | |
0 | 8 | */ |
9 | ||
10 | #include <stdlib.h> | |
11 | #include <stdio.h> | |
12 | #include <string.h> | |
64 | 13 | #include <ctype.h> |
0 | 14 | |
70 | 15 | #include "misc.h" |
80 | 16 | #include "output.h" |
0 | 17 | |
18 | ||
1 | 19 | void *malloczero(size_t size) |
0 | 20 | { |
62 | 21 | void *p=malloc(size); |
22 | ||
23 | if(p!=NULL) | |
24 | memset(p, 0, size); | |
80 | 25 | else |
26 | warn_err(); | |
62 | 27 | |
28 | return p; | |
0 | 29 | } |
30 | ||
31 | ||
1 | 32 | void *remalloczero(void *ptr, size_t oldsize, size_t newsize) |
0 | 33 | { |
62 | 34 | void *p=NULL; |
35 | ||
36 | if(newsize!=0){ | |
37 | p=realloc(ptr, newsize); | |
38 | ||
80 | 39 | if(p==NULL){ |
40 | warn_err(); | |
62 | 41 | return NULL; |
80 | 42 | } |
62 | 43 | |
44 | if(newsize>oldsize) | |
45 | memset((char*)p+oldsize, 0, newsize-oldsize); | |
46 | } | |
0 | 47 | |
62 | 48 | return p; |
0 | 49 | } |
50 | ||
51 | ||
63 | 52 | char *scopyn(const char *p, size_t l) |
0 | 53 | { |
80 | 54 | char *pn=ALLOC_N(char, l+1); |
62 | 55 | |
56 | if(pn==NULL) | |
57 | return NULL; | |
58 | ||
64 | 59 | memcpy(pn, p, l); |
60 | pn[l]='\0'; | |
62 | 61 | |
62 | return pn; | |
0 | 63 | } |
63 | 64 | |
65 | ||
66 | char *scopy(const char *p) | |
67 | { | |
68 | size_t l=strlen(p); | |
69 | return scopyn(p, l); | |
70 | } | |
62 | 71 | |
72 | ||
0 | 73 | char *scat(const char *p1, const char *p2) |
74 | { | |
62 | 75 | size_t l1, l2; |
76 | char*pn; | |
77 | ||
78 | l1=strlen(p1); | |
79 | l2=strlen(p2); | |
80 | ||
80 | 81 | pn=ALLOC_N(char, l1+l2+1); |
62 | 82 | |
83 | if(pn==NULL) | |
84 | return NULL; | |
85 | ||
86 | memcpy(pn, p1, l1); | |
87 | memcpy(pn+l1, p2, l2+1); | |
88 | ||
89 | return pn; | |
0 | 90 | } |
91 | ||
92 | ||
93 | char *scat3(const char *p1, const char *p2, const char *p3) | |
94 | { | |
62 | 95 | size_t l1, l2, l3; |
96 | char *pn; | |
97 | ||
98 | l1=strlen(p1); | |
99 | l2=strlen(p2); | |
100 | l3=strlen(p3); | |
101 | ||
80 | 102 | pn=ALLOC_N(char, l1+l2+l3+1); |
62 | 103 | |
104 | if(pn==NULL) | |
105 | return NULL; | |
106 | ||
107 | memcpy(pn, p1, l1); | |
108 | memcpy(pn+l1, p2, l2); | |
109 | memcpy(pn+l1+l2, p3, l3+1); | |
110 | ||
111 | return pn; | |
0 | 112 | } |
113 | ||
114 | ||
1 | 115 | char *scatn(const char *s1, ssize_t l1, const char *s2, ssize_t l2) |
116 | { | |
62 | 117 | size_t tlen=1; |
118 | char *s; | |
119 | ||
120 | if(l1<0) | |
121 | l1=strlen(s1); | |
122 | ||
123 | if(l2<0) | |
124 | l2=strlen(s2); | |
125 | ||
126 | tlen+=l1+l2; | |
127 | ||
80 | 128 | s=ALLOC_N(char, tlen); |
62 | 129 | |
130 | if(s==NULL) | |
131 | return NULL; | |
132 | ||
133 | memcpy(s, s1, l1); | |
134 | memcpy(s+l1, s2, l2); | |
135 | s[l1+l2]='\0'; | |
136 | ||
137 | return s; | |
1 | 138 | } |
139 | ||
140 | ||
0 | 141 | /* */ |
142 | ||
143 | ||
144 | const char *simple_basename(const char *name) | |
145 | { | |
62 | 146 | const char *p; |
147 | ||
148 | p=name+strlen(name)-1; | |
149 | ||
150 | /* Skip any trailing slashes */ | |
151 | while(*p=='/'){ | |
152 | /* root? */ | |
153 | if(p==name) | |
154 | return name; | |
155 | p--; | |
156 | } | |
157 | ||
158 | while(p!=name){ | |
159 | if(*p=='/') | |
160 | return p+1; | |
161 | p--; | |
162 | } | |
163 | ||
164 | return name; | |
0 | 165 | } |
166 | ||
167 | ||
32 | 168 | void stripws(char *p) |
169 | { | |
62 | 170 | int l; |
171 | ||
172 | l=strspn(p, " "); | |
173 | if(l!=0) | |
174 | strcpy(p, p+l); | |
175 | l=strlen(p); | |
176 | ||
177 | while(--l>=0){ | |
178 | if(*(p+l)!=' ') | |
179 | break; | |
180 | } | |
181 | *(p+l+1)='\0'; | |
32 | 182 | } |
183 | ||
184 | ||
64 | 185 | const char *libtu_strcasestr(const char *str, const char *ptn) |
186 | { | |
187 | const char *s2, *p2; | |
188 | for(; *str; str++) { | |
189 | for(s2=str, p2=ptn; ; s2++, p2++) { | |
190 | if(!*p2) | |
191 | return str; | |
192 | if(toupper(*s2)!=toupper(*p2)) | |
193 | break; | |
194 | } | |
195 | } | |
196 | return NULL; | |
197 | } | |
198 | ||
0 | 199 | /* */ |
200 | ||
201 | ||
202 | bool readf(FILE *f, void *buf, size_t n) | |
203 | { | |
62 | 204 | return fread(buf, 1, n, f)!=1; |
0 | 205 | } |
206 | ||
207 | ||
208 | bool writef(FILE *f, const void *buf, size_t n) | |
209 | { | |
62 | 210 | return fwrite(buf, 1, n, f)!=1; |
0 | 211 | } |