misc.c

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

mercurial