Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do throw an exception only when the requested factory was not found, not all the...
[simgrid.git] / src / xbt / xbt_strbuff.c
1 /* $Id: buff.c 3483 2007-05-07 11:18:56Z mquinson $ */
2
3 /* strbuff -- string buffers                                                */
4
5 /* Copyright (c) 2007 Martin Quinson.                                       */
6 /* All rights reserved.                                                     */
7
8 /* This program is free software; you can redistribute it and/or modify it
9  * under the terms of the license (GNU LGPL) which comes with this package. */
10
11 /* specific to Borland Compiler */
12 #ifdef __BORLANDDC__
13 #pragma hdrstop
14 #endif
15
16 #include "xbt/strbuff.h"
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(strbuff,xbt,"String buffers");
19
20 /**
21  ** Buffer code
22  **/
23
24 void xbt_strbuff_empty(xbt_strbuff_t b) {
25   b->used=0;
26   b->data[0]='\n';
27   b->data[1]='\0';
28 }
29 xbt_strbuff_t xbt_strbuff_new(void) {
30   xbt_strbuff_t res=malloc(sizeof(s_xbt_strbuff_t));
31   res->data=malloc(512);
32   res->size=512;
33   xbt_strbuff_empty(res);
34   return res;
35 }
36 void xbt_strbuff_free(xbt_strbuff_t b) {
37   if (b) {
38     if (b->data)
39       free(b->data);
40     free(b);
41   }
42 }
43 void xbt_strbuff_append(xbt_strbuff_t b, const char *toadd) {
44   int addlen;
45   int needed_space;
46
47   if (!b)
48     THROW0(arg_error,0,"Asked to append stuff to NULL buffer");
49
50   addlen = strlen(toadd);
51   needed_space=b->used+addlen+1;
52
53   if (needed_space > b->size) {
54     b->data = realloc(b->data, needed_space);
55     b->size = needed_space;
56   }
57   strcpy(b->data+b->used, toadd);
58   b->used += addlen;  
59 }
60 void xbt_strbuff_chomp(xbt_strbuff_t b) {
61   while (b->data[b->used] == '\n') {
62     b->data[b->used] = '\0';
63     if (b->used)
64       b->used--;
65   }
66 }
67
68 void xbt_strbuff_trim(xbt_strbuff_t b) {
69   xbt_str_trim(b->data," ");
70   b->used = strlen(b->data);
71 }