Sat, 03 Jun 2000 19:00:43 +0200
trunk: changeset 17
Default include path support
0 | 1 | /* |
2 | * libtu/misc.c | |
3 | * | |
9 | 4 | * Copyright (c) Tuomo Valkonen 1999-2000. |
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){ | |
31 | p=malloc(newsize); | |
32 | ||
33 | if(p==NULL) | |
34 | return NULL; | |
35 | ||
36 | memset(p, 0, newsize); | |
37 | ||
1 | 38 | if(newsize<oldsize) |
39 | oldsize=newsize; | |
40 | ||
41 | if(ptr!=NULL) | |
0 | 42 | memcpy(p, ptr, oldsize); |
43 | } | |
44 | ||
45 | if(ptr!=NULL) | |
46 | free(ptr); | |
47 | ||
48 | return p; | |
49 | } | |
50 | ||
51 | ||
52 | char *scopy(const char *p) | |
53 | { | |
54 | char*pn; | |
55 | size_t l=strlen(p); | |
56 | ||
57 | pn=(char*)malloc(l+1); | |
58 | ||
59 | if(pn==NULL) | |
60 | return NULL; | |
61 | ||
62 | memcpy(pn, p, l+1); | |
63 | ||
64 | return pn; | |
65 | } | |
66 | ||
67 | ||
68 | char *scat(const char *p1, const char *p2) | |
69 | { | |
70 | size_t l1, l2; | |
71 | char*pn; | |
72 | ||
73 | l1=strlen(p1); | |
74 | l2=strlen(p2); | |
75 | ||
76 | pn=(char*)malloc(l1+l2+1); | |
77 | ||
78 | if(pn==NULL) | |
79 | return NULL; | |
80 | ||
81 | memcpy(pn, p1, l1); | |
82 | memcpy(pn+l1, p2, l2+1); | |
83 | ||
84 | return pn; | |
85 | } | |
86 | ||
87 | ||
88 | char *scat3(const char *p1, const char *p2, const char *p3) | |
89 | { | |
90 | size_t l1, l2, l3; | |
91 | char *pn; | |
92 | ||
93 | l1=strlen(p1); | |
94 | l2=strlen(p2); | |
95 | l3=strlen(p3); | |
96 | ||
97 | pn=(char*)malloc(l1+l2+l3+1); | |
98 | ||
99 | if(pn==NULL) | |
100 | return NULL; | |
101 | ||
102 | memcpy(pn, p1, l1); | |
103 | memcpy(pn+l1, p2, l2); | |
104 | memcpy(pn+l1+l2, p3, l3+1); | |
105 | ||
106 | return pn; | |
107 | } | |
108 | ||
109 | ||
1 | 110 | char *scatn(const char *s1, ssize_t l1, const char *s2, ssize_t l2) |
111 | { | |
112 | size_t tlen=1; | |
113 | char *s; | |
114 | ||
115 | if(l1<0) | |
116 | l1=strlen(s1); | |
117 | ||
118 | if(l2<0) | |
119 | l2=strlen(s2); | |
120 | ||
121 | tlen+=l1+l2; | |
122 | ||
123 | s=(char*)malloc(tlen); | |
124 | ||
125 | if(s==NULL) | |
126 | return NULL; | |
127 | ||
128 | memcpy(s, s1, l1); | |
129 | memcpy(s+l1, s2, l2); | |
130 | s[l1+l2]='\0'; | |
131 | ||
132 | return s; | |
133 | } | |
134 | ||
135 | ||
0 | 136 | /* */ |
137 | ||
138 | ||
139 | const char *simple_basename(const char *name) | |
140 | { | |
141 | const char *p; | |
142 | ||
143 | p=name+strlen(name)-1; | |
144 | ||
145 | /* Skip any trailing slashes */ | |
146 | while(*p=='/'){ | |
147 | /* root? */ | |
148 | if(p==name) | |
149 | return name; | |
150 | p--; | |
151 | } | |
152 | ||
153 | while(p!=name){ | |
154 | if(*p=='/') | |
155 | return p+1; | |
156 | p--; | |
157 | } | |
158 | ||
159 | return name; | |
160 | } | |
161 | ||
162 | ||
163 | /* */ | |
164 | ||
165 | ||
166 | bool readf(FILE *f, void *buf, size_t n) | |
167 | { | |
168 | return fread(buf, 1, n, f)!=1; | |
169 | } | |
170 | ||
171 | ||
172 | bool writef(FILE *f, const void *buf, size_t n) | |
173 | { | |
174 | return fwrite(buf, 1, n, f)!=1; | |
175 | } |