root/trunk/src/rrdwrap.c @ 17

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

*** empty log message ***

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