Wed, 28 May 2003 23:48:16 +0200
trunk: changeset 57
License changed to Artistic/LGPL dual license.
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> | |
13 | ||
5 | 14 | #include <libtu/misc.h> |
0 | 15 | |
16 | ||
1 | 17 | void *malloczero(size_t size) |
0 | 18 | { |
19 | void *p=malloc(size); | |
20 | ||
21 | if(p!=NULL) | |
22 | memset(p, 0, size); | |
23 | ||
24 | return p; | |
25 | } | |
26 | ||
27 | ||
1 | 28 | void *remalloczero(void *ptr, size_t oldsize, size_t newsize) |
0 | 29 | { |
30 | void *p=NULL; | |
31 | ||
32 | if(newsize!=0){ | |
41 | 33 | p=realloc(ptr, newsize); |
34 | ||
0 | 35 | if(p==NULL) |
36 | return NULL; | |
37 | ||
41 | 38 | if(newsize>oldsize) |
42 | 39 | memset((char*)p+oldsize, 0, newsize-oldsize); |
0 | 40 | } |
41 | ||
42 | return p; | |
43 | } | |
44 | ||
45 | ||
46 | char *scopy(const char *p) | |
47 | { | |
48 | char*pn; | |
49 | size_t l=strlen(p); | |
50 | ||
51 | pn=(char*)malloc(l+1); | |
52 | ||
53 | if(pn==NULL) | |
54 | return NULL; | |
55 | ||
56 | memcpy(pn, p, l+1); | |
57 | ||
58 | return pn; | |
59 | } | |
60 | ||
61 | ||
62 | char *scat(const char *p1, const char *p2) | |
63 | { | |
64 | size_t l1, l2; | |
65 | char*pn; | |
66 | ||
67 | l1=strlen(p1); | |
68 | l2=strlen(p2); | |
69 | ||
70 | pn=(char*)malloc(l1+l2+1); | |
71 | ||
72 | if(pn==NULL) | |
73 | return NULL; | |
74 | ||
75 | memcpy(pn, p1, l1); | |
76 | memcpy(pn+l1, p2, l2+1); | |
77 | ||
78 | return pn; | |
79 | } | |
80 | ||
81 | ||
82 | char *scat3(const char *p1, const char *p2, const char *p3) | |
83 | { | |
84 | size_t l1, l2, l3; | |
85 | char *pn; | |
86 | ||
87 | l1=strlen(p1); | |
88 | l2=strlen(p2); | |
89 | l3=strlen(p3); | |
90 | ||
91 | pn=(char*)malloc(l1+l2+l3+1); | |
92 | ||
93 | if(pn==NULL) | |
94 | return NULL; | |
95 | ||
96 | memcpy(pn, p1, l1); | |
97 | memcpy(pn+l1, p2, l2); | |
98 | memcpy(pn+l1+l2, p3, l3+1); | |
99 | ||
100 | return pn; | |
101 | } | |
102 | ||
103 | ||
1 | 104 | char *scatn(const char *s1, ssize_t l1, const char *s2, ssize_t l2) |
105 | { | |
106 | size_t tlen=1; | |
107 | char *s; | |
108 | ||
109 | if(l1<0) | |
110 | l1=strlen(s1); | |
111 | ||
112 | if(l2<0) | |
113 | l2=strlen(s2); | |
114 | ||
115 | tlen+=l1+l2; | |
116 | ||
117 | s=(char*)malloc(tlen); | |
118 | ||
119 | if(s==NULL) | |
120 | return NULL; | |
121 | ||
122 | memcpy(s, s1, l1); | |
123 | memcpy(s+l1, s2, l2); | |
124 | s[l1+l2]='\0'; | |
125 | ||
126 | return s; | |
127 | } | |
128 | ||
129 | ||
0 | 130 | /* */ |
131 | ||
132 | ||
133 | const char *simple_basename(const char *name) | |
134 | { | |
135 | const char *p; | |
136 | ||
137 | p=name+strlen(name)-1; | |
138 | ||
139 | /* Skip any trailing slashes */ | |
140 | while(*p=='/'){ | |
141 | /* root? */ | |
142 | if(p==name) | |
143 | return name; | |
144 | p--; | |
145 | } | |
146 | ||
147 | while(p!=name){ | |
148 | if(*p=='/') | |
149 | return p+1; | |
150 | p--; | |
151 | } | |
152 | ||
153 | return name; | |
154 | } | |
155 | ||
156 | ||
32 | 157 | void stripws(char *p) |
158 | { | |
159 | int l; | |
160 | ||
161 | l=strspn(p, " "); | |
162 | if(l!=0) | |
163 | strcpy(p, p+l); | |
164 | l=strlen(p); | |
165 | ||
166 | while(--l>=0){ | |
167 | if(*(p+l)!=' ') | |
168 | break; | |
169 | } | |
170 | *(p+l+1)='\0'; | |
171 | } | |
172 | ||
173 | ||
0 | 174 | /* */ |
175 | ||
176 | ||
177 | bool readf(FILE *f, void *buf, size_t n) | |
178 | { | |
179 | return fread(buf, 1, n, f)!=1; | |
180 | } | |
181 | ||
182 | ||
183 | bool writef(FILE *f, const void *buf, size_t n) | |
184 | { | |
185 | return fwrite(buf, 1, n, f)!=1; | |
186 | } |