Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
shorten
[simgrid.git] / tools / tesh / buff.c
1 /* $Id$ */
2
3 /* buff -- buffers as needed by tesh                                        */
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 "buff.h"
17
18 /**
19  ** Buffer code
20  **/
21
22 void buff_empty(buff_t *b) {
23   b->used=0;
24   b->data[0]='\n';
25   b->data[1]='\0';
26 }
27 buff_t *buff_new(void) {
28   buff_t *res=malloc(sizeof(buff_t));
29   res->data=malloc(512);
30   res->size=512;
31   buff_empty(res);
32   return res;
33 }
34 void buff_free(buff_t *b) {
35   if (b) {
36     if (b->data)
37       free(b->data);
38     free(b);
39   }
40 }
41 void buff_append(buff_t *b, char *toadd) {
42   int addlen=strlen(toadd);
43   int needed_space=b->used+addlen+1;
44
45   if (needed_space > b->size) {
46     b->data = realloc(b->data, needed_space);
47     b->size = needed_space;
48   }
49   strcpy(b->data+b->used, toadd);
50   b->used += addlen;  
51 }
52 void buff_chomp(buff_t *b) {
53   while (b->data[b->used] == '\n') {
54     b->data[b->used] = '\0';
55     if (b->used)
56       b->used--;
57   }
58 }
59
60 void buff_trim(buff_t* b) {
61   xbt_str_trim(b->data," ");
62   b->used = strlen(b->data);
63 }