root/trunk/src/rrdwrap.c @ 27

Revision 27, 4.9 KB (checked in by andreu, 16 years ago)

explicite output to find wrong definition bug - memory leak in renetcolAgg find

  • Property svn:eol-style set to native
Line 
1/*      rrdwrap.c - wrapper functions for the rrd tool */
2
3/*      Contributed by Antony Clothier */
4
5/*  Ligne 152 commented FXA*/
6
7#include "rrdwrap.h"
8
9int wrap_rrd_create(char *argstr)
10{
11  char **argv;
12  int i, argc;
13 
14  if((argv = string_to_argv(argstr, &argc)) != NULL)
15    {
16      optind=0; /* reset gnu getopt */
17      opterr=0; /* no error messages */
18     
19      i = rrd_create(argc, argv);
20     
21      /* free up the memory */
22      Free_argv(argv);
23     
24      return i;
25    }
26  else
27    {
28      /* error */
29      return (-1);
30    }
31}
32
33int wrap_rrd_update(char *argstr)
34{
35  char **argv;
36  int i, argc;
37 
38  if((argv = string_to_argv(argstr, &argc)) != NULL)
39    {
40      optind=0; /* reset gnu getopt */
41      opterr=0; /* no error messages */
42     
43      i = rrd_update(argc, argv);
44     
45      /* free up the memory */
46      Free_argv(argv);
47     
48      return i;
49    }
50  else
51    {
52      /* error */
53      return (-1);
54    }
55}
56
57
58int wrap_rrd_restore(char *argstr)
59{
60  char **argv;
61  int i, argc;
62 
63  if((argv = string_to_argv(argstr, &argc)) != NULL)
64    {
65      optind=0; /* reset gnu getopt */
66      opterr=0; /* no error messages */
67     
68      i = rrd_restore(argc, argv);
69     
70      /* free up the memory */
71      Free_argv(argv);
72     
73      return i;
74    }
75  else
76    {
77      /* error */
78      return (-1);
79    }
80}
81
82
83int wrap_rrd_dump(char *argstr)
84{
85  char **argv;
86  int i, argc;
87
88  if((argv = string_to_argv(argstr, &argc)) != NULL)
89    {
90      optind=0; /* reset gnu getopt */
91      opterr=0; /* no error messages */
92     
93      i = rrd_dump(argc, argv);
94     
95      /* free up the memory */
96      Free_argv(argv);
97     
98      return i;
99    }
100  else
101    {
102      /* error */
103      return (-1);
104    }
105}
106
107
108int wrap_rrd_tune(char *argstr)
109{
110  char **argv;
111  int i, argc;
112 
113  if((argv = string_to_argv(argstr, &argc)) != NULL)
114    {
115      optind=0; /* reset gnu getopt */
116      opterr=0; /* no error messages */
117     
118      i = rrd_tune(argc, argv);
119
120      /*        free up the memory */
121      Free_argv(argv);
122     
123      return i;
124    }
125  else
126    {
127      /* error */
128      return (-1);
129    }
130}
131
132
133int wrap_rrd_graph(char *argstr)
134{
135  char 
136    ***prdata, /* must be passed to the rrd_graph function */
137               /* but does not need to be initialised */
138    **argv;
139 
140  int 
141    i, argc,
142    xsize,   /* must be passed to the rrd_graph function */ 
143             /* but does not need to be initialised*/
144    ysize;   /* must be passed to the rrd_graph function */
145             /* but does not need to be initialised */
146 
147  if((argv = string_to_argv(argstr, &argc)) != NULL)
148    {
149      optind=0; /* reset gnu getopt */
150      opterr=0; /* no error messages */
151     
152/*       i = rrd_graph(argc, argv, prdata, &xsize, &ysize); */
153     
154      /*        free up the memory*/
155      Free_argv(argv);
156     
157      return i;
158    }
159  else
160    {
161      /* error*/
162      return (-1);
163    }
164}
165
166
167
168char** string_to_argv(char *argstring, int *argc)
169{
170  char
171    inquotes = 0,
172    *workstring,
173    **argv;
174 
175  int 
176    i, nchars;
177 
178  if((nchars = strlen(argstring)) > 0)
179    {
180      workstring = (char*)calloc((nchars + 2), sizeof(char*));
181     
182      /*        fill the array with space characters */
183      for(i=0; i < (nchars + 2); i++)
184        workstring[i] = ' ';
185     
186      /* copy the argstring into the workstring
187         (padded with spaces on both ends) */
188      for(i=0; i < nchars; i++)
189        workstring[i+1] = argstring[i];
190     
191      for((*argc) = 1, i=0; i < nchars + 2; i++)
192        {
193          /* count the number of arguements */
194          if(isgraph(workstring[i]) > 0)
195            {
196              /* if current character is not whitespace and previous
197                 character is whitespace, increment (*argc) */
198              if((isgraph(workstring[i-1]) == 0) && (inquotes == 0))
199                (*argc)++;
200             
201              /* check for the quote character */
202              if(workstring[i] == '"')
203                {
204                  /*    toggle inquotes flag */
205                  inquotes = (char)(!inquotes);
206                }
207             
208            }
209          else
210            {
211              /* convert whitespace to null characters */
212              if(!inquotes)
213                workstring[i] = '\0';
214            }
215        }
216     
217      /* no arguements in the string */
218      if((*argc) == 0)
219        {
220          free(workstring);
221          return NULL;
222        }
223      else
224        {
225          inquotes = 0;
226         
227          argv = (char**)calloc((*argc), sizeof(char**));
228         
229          argv[0] = &workstring[0];
230         
231          for((*argc) = 1, i=1; i < nchars + 2; i++)
232                        {
233                          /* count the number of arguements */
234                          if(isgraph(workstring[i]) > 0)
235                            {
236                              /* if current character is not whitespace and
237                                 previous character is whitespace, increment
238                                 (*argc) */
239                              if((isgraph(workstring[i-1]) == 0) && (inquotes == 0))
240                                {
241                                  argv[(*argc)] = &workstring[i];
242                                  (*argc)++;
243                                }
244                             
245                              /* check for the quote character */
246                              if(workstring[i] == '"')
247                                {
248                                  /*    toggle inquotes flag */
249                                  inquotes = (char)(!inquotes);
250                                }
251                            }
252                        }
253         
254          return argv;
255        }
256    }
257  else 
258    {
259      (*argc) = 0;
260      return NULL;
261    }
262}
263
264
265void Free_argv(char** argv)
266{
267  free(argv[0]);
268  free(argv);
269}
Note: See TracBrowser for help on using the browser.