Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
691d53df08bac27bc56f3778eac9c0e993683fb5
[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 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
19
20 /**
21  ** Buffer code
22  **/
23
24 void buff_empty(buff_t b) {
25   b->used=0;
26   b->data[0]='\n';
27   b->data[1]='\0';
28 }
29 buff_t buff_new(void) {
30   buff_t res=malloc(sizeof(s_buff_t));
31   res->data=malloc(512);
32   res->size=512;
33   buff_empty(res);
34   return res;
35 }
36 void buff_free(buff_t b) {
37   if (b) {
38     if (b->data)
39       free(b->data);
40     free(b);
41   }
42 }
43 void buff_append(buff_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 buff_chomp(buff_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 buff_trim(buff_t b) {
69   xbt_str_trim(b->data," ");
70   b->used = strlen(b->data);
71 }