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