Mercurial > hg > xmms-sid
comparison src/xs_support.c @ 895:b6e069c9c000
Move stuff around a bit.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 09 Nov 2012 08:22:41 +0200 |
parents | 4697c5bd7025 |
children | b928b8a9c5bf |
comparison
equal
deleted
inserted
replaced
894:4697c5bd7025 | 895:b6e069c9c000 |
---|---|
189 | 189 |
190 res = xs_fload_buffer(filename, pbuf, bufSize, maxSize, failMaxSize); | 190 res = xs_fload_buffer(filename, pbuf, bufSize, maxSize, failMaxSize); |
191 | 191 |
192 g_free(filename); | 192 g_free(filename); |
193 return res; | 193 return res; |
194 } | |
195 | |
196 | |
197 /* Copy a given string over in *result. | |
198 */ | |
199 gint xs_pstrcpy(gchar **result, const gchar *str) | |
200 { | |
201 /* Check the string pointers */ | |
202 if (!result || !str) | |
203 return -1; | |
204 | |
205 /* Allocate memory for destination */ | |
206 g_free(*result); | |
207 | |
208 *result = (gchar *) g_malloc(strlen(str) + 1); | |
209 if (!*result) | |
210 return -2; | |
211 | |
212 /* Copy to the destination */ | |
213 strcpy(*result, str); | |
214 | |
215 return 0; | |
216 } | |
217 | |
218 | |
219 /* Concatenates a given string into string pointed by *result. | |
220 */ | |
221 gint xs_pstrcat(gchar **result, const gchar *str) | |
222 { | |
223 /* Check the string pointers */ | |
224 if (!result || !str) | |
225 return -1; | |
226 | |
227 if (*result != NULL) | |
228 { | |
229 *result = (gchar *) g_realloc(*result, strlen(*result) + strlen(str) + 1); | |
230 if (*result == NULL) | |
231 return -1; | |
232 strcat(*result, str); | |
233 } | |
234 else | |
235 { | |
236 *result = (gchar *) g_malloc(strlen(str) + 1); | |
237 if (*result == NULL) | |
238 return -1; | |
239 strcpy(*result, str); | |
240 } | |
241 | |
242 return 0; | |
243 } | |
244 | |
245 | |
246 /* Concatenate a given string up to given dest size or \n. | |
247 * If size max is reached, change the end to "..." | |
248 */ | |
249 void xs_pnstrcat(gchar *dest, const size_t size, const gchar *str) | |
250 { | |
251 size_t i, n; | |
252 const gchar *s; | |
253 gchar *d; | |
254 | |
255 for (d = dest, i = 0; *d && i < size; i++, d++); | |
256 | |
257 s = str; | |
258 while (*s && *s != '\n' && i < size) | |
259 { | |
260 *d = *s; | |
261 d++; | |
262 s++; | |
263 i++; | |
264 } | |
265 | |
266 *d = 0; | |
267 | |
268 if (i >= size) | |
269 { | |
270 i--; | |
271 d--; | |
272 for (n = 3; i > 0 && n > 0; d--, i--, n--) | |
273 *d = '.'; | |
274 } | |
275 } | |
276 | |
277 | |
278 /* Locate character in string | |
279 */ | |
280 void xs_findnext(const gchar *str, size_t *pos) | |
281 { | |
282 while (str[*pos] && isspace(str[*pos])) | |
283 (*pos)++; | |
284 } | |
285 | |
286 | |
287 void xs_findeol(const gchar *str, size_t *pos) | |
288 { | |
289 while (str[*pos] && (str[*pos] != '\n') && (str[*pos] != '\r')) | |
290 (*pos)++; | |
291 } | |
292 | |
293 | |
294 void xs_findnum(const gchar *str, size_t *pos) | |
295 { | |
296 while (str[*pos] && isdigit(str[*pos])) | |
297 (*pos)++; | |
298 } | 194 } |
299 | 195 |
300 | 196 |
301 /* | 197 /* |
302 * MD5 implementation, modified for XMMS-SID from | 198 * MD5 implementation, modified for XMMS-SID from |
525 memcpy(((guint32 *) ctx->in) + 15, &ctx->bits[1], sizeof(guint32)); | 421 memcpy(((guint32 *) ctx->in) + 15, &ctx->bits[1], sizeof(guint32)); |
526 | 422 |
527 xs_md5_transform(ctx->buf, (guint32 *) ctx->in); | 423 xs_md5_transform(ctx->buf, (guint32 *) ctx->in); |
528 xs_md5_bytereverse((guint8 *) ctx->buf, 4); | 424 xs_md5_bytereverse((guint8 *) ctx->buf, 4); |
529 memcpy(digest, ctx->buf, 16); | 425 memcpy(digest, ctx->buf, 16); |
530 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ | 426 memset(ctx, 0, sizeof(ctx)); |
531 } | 427 } |
428 | |
429 | |
430 /* Copy a given string over in *result. | |
431 */ | |
432 gint xs_pstrcpy(gchar **result, const gchar *str) | |
433 { | |
434 /* Check the string pointers */ | |
435 if (!result || !str) | |
436 return -1; | |
437 | |
438 /* Allocate memory for destination */ | |
439 g_free(*result); | |
440 | |
441 *result = (gchar *) g_malloc(strlen(str) + 1); | |
442 if (!*result) | |
443 return -2; | |
444 | |
445 /* Copy to the destination */ | |
446 strcpy(*result, str); | |
447 | |
448 return 0; | |
449 } | |
450 | |
451 | |
452 /* Concatenates a given string into string pointed by *result. | |
453 */ | |
454 gint xs_pstrcat(gchar **result, const gchar *str) | |
455 { | |
456 /* Check the string pointers */ | |
457 if (!result || !str) | |
458 return -1; | |
459 | |
460 if (*result != NULL) | |
461 { | |
462 *result = (gchar *) g_realloc(*result, strlen(*result) + strlen(str) + 1); | |
463 if (*result == NULL) | |
464 return -1; | |
465 strcat(*result, str); | |
466 } | |
467 else | |
468 { | |
469 *result = (gchar *) g_malloc(strlen(str) + 1); | |
470 if (*result == NULL) | |
471 return -1; | |
472 strcpy(*result, str); | |
473 } | |
474 | |
475 return 0; | |
476 } | |
477 | |
478 | |
479 /* Concatenate a given string up to given dest size or \n. | |
480 * If size max is reached, change the end to "..." | |
481 */ | |
482 void xs_pnstrcat(gchar *dest, const size_t size, const gchar *str) | |
483 { | |
484 size_t i, n; | |
485 const gchar *s; | |
486 gchar *d; | |
487 | |
488 for (d = dest, i = 0; *d && i < size; i++, d++); | |
489 | |
490 s = str; | |
491 while (*s && *s != '\n' && i < size) | |
492 { | |
493 *d = *s; | |
494 d++; | |
495 s++; | |
496 i++; | |
497 } | |
498 | |
499 *d = 0; | |
500 | |
501 if (i >= size) | |
502 { | |
503 i--; | |
504 d--; | |
505 for (n = 3; i > 0 && n > 0; d--, i--, n--) | |
506 *d = '.'; | |
507 } | |
508 } | |
509 | |
510 | |
511 /* Locate character in string | |
512 */ | |
513 void xs_findnext(const gchar *str, size_t *pos) | |
514 { | |
515 while (str[*pos] && isspace(str[*pos])) | |
516 (*pos)++; | |
517 } | |
518 | |
519 | |
520 void xs_findeol(const gchar *str, size_t *pos) | |
521 { | |
522 while (str[*pos] && (str[*pos] != '\n') && (str[*pos] != '\r')) | |
523 (*pos)++; | |
524 } | |
525 | |
526 | |
527 void xs_findnum(const gchar *str, size_t *pos) | |
528 { | |
529 while (str[*pos] && isdigit(str[*pos])) | |
530 (*pos)++; | |
531 } | |
532 | |
533 |