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