1 /****************************************************************************
2 * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010
3 ****************************************************************************
4 * rrd_xport.c export RRD data
5 ****************************************************************************/
11 #include "rrd_graph.h"
12 #include "rrd_xport.h"
14 #include "rrd_client.h"
16 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
42 /* helper function for buffer handling */
43 typedef struct stringbuffer_t {
49 int addToBuffer(stringbuffer_t *,char*,size_t);
50 void escapeJSON(char*,size_t);
52 int rrd_graph_xport(image_desc_t *);
53 int rrd_xport_format_xmljson(int,stringbuffer_t *,image_desc_t*,time_t, time_t, unsigned long, unsigned long, char**, rrd_value_t*);
54 int rrd_xport_format_sv(char,stringbuffer_t *,image_desc_t*,time_t, time_t, unsigned long, unsigned long, char**, rrd_value_t*);
55 int rrd_xport_format_addprints(int,stringbuffer_t *,image_desc_t *);
62 time_t *end, /* which time frame do you want ?
63 * will be changed to represent reality */
64 unsigned long *step, /* which stepsize do you want?
65 * will be changed to represent reality */
66 unsigned long *col_cnt, /* number of data columns in the result */
67 char ***legend_v, /* legend entries */
69 { /* two dimensional array containing the data */
71 time_t start_tmp = 0, end_tmp = 0;
72 rrd_time_value_t start_tv, end_tv;
73 char *parsetime_error = NULL;
75 struct option long_options[] = {
76 {"start", required_argument, 0, 's'},
77 {"end", required_argument, 0, 'e'},
78 {"maxrows", required_argument, 0, 'm'},
79 {"step", required_argument, 0, 261},
80 {"enumds", no_argument, 0, 262}, /* these are handled in the frontend ... */
81 {"json", no_argument, 0, 263}, /* these are handled in the frontend ... */
82 {"daemon", required_argument, 0, 'd'},
87 opterr = 0; /* initialize getopt */
91 rrd_parsetime("end-24h", &start_tv);
92 rrd_parsetime("now", &end_tv);
101 opt = getopt_long(argc, argv, "s:e:m:d:", long_options, &option_index);
108 im.step = atoi(optarg);
117 if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) {
118 rrd_set_error("start time: %s", parsetime_error);
123 if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) {
124 rrd_set_error("end time: %s", parsetime_error);
129 im.xsize = atol(optarg);
131 rrd_set_error("maxrows below 10 rows");
137 if (im.daemon_addr != NULL)
139 rrd_set_error ("You cannot specify --daemon "
144 im.daemon_addr = strdup(optarg);
145 if (im.daemon_addr == NULL)
147 rrd_set_error("strdup error");
154 rrd_set_error("unknown option '%s'", argv[optind - 1]);
159 if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
163 if (start_tmp < 3600 * 24 * 365 * 10) {
164 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",
169 if (end_tmp < start_tmp) {
170 rrd_set_error("start (%ld) should be less than end (%ld)",
175 im.start = start_tmp;
177 im.step = max((long) im.step, (im.end - im.start) / im.xsize);
179 rrd_graph_script(argc, argv, &im, 0);
180 if (rrd_test_error()) {
185 if (im.gdes_c == 0) {
186 rrd_set_error("can't make an xport without contents");
191 { /* try to connect to rrdcached */
192 int status = rrdc_connect(im.daemon_addr);
193 if (status != 0) return status;
196 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data,0) == -1) {
201 /* and create the export */
204 if (json) { flags|=1; }
205 if (enumds) { flags|=4; }
206 stringbuffer_t buffer={0,0,NULL,stdout};
207 rrd_xport_format_xmljson(flags,&buffer,&im,
222 time_t *end, /* which time frame do you want ?
223 * will be changed to represent reality */
224 unsigned long *step, /* which stepsize do you want?
225 * will be changed to represent reality */
226 unsigned long *col_cnt, /* number of data columns in the result */
227 char ***legend_v, /* legend entries */
230 { /* two dimensional array containing the data */
233 unsigned long dst_row, row_cnt;
236 unsigned long xport_counter = 0;
243 /* pull the data from the rrd files ... */
244 if (data_fetch(im) == -1)
247 /* evaluate CDEF operations ... */
248 if (data_calc(im) == -1)
251 /* how many xports or lines/AREA/STACK ? */
253 for (i = 0; i < im->gdes_c; i++) {
254 switch (im->gdes[i].gf) {
267 if ((*col_cnt) == 0) {
268 rrd_set_error("no XPORT found, nothing to do");
272 /* a list of referenced gdes */
273 ref_list = (int*)malloc(sizeof(int) * (*col_cnt));
274 if (ref_list == NULL)
277 /* a list to save pointers to the column's legend entry */
278 /* this is a return value! */
279 legend_list = (char**)malloc(sizeof(char *) * (*col_cnt));
280 if (legend_list == NULL) {
285 /* lets find the step size we have to use for xport */
286 step_list = (long*)malloc(sizeof(long)*((*col_cnt)+1));
287 step_list_ptr = step_list;
289 for (i = 0; i < im->gdes_c; i++) {
290 /* decide if we need to handle the output */
292 switch (im->gdes[i].gf) {
305 /* and now do the real work */
307 ref_list[xport_counter++] = i;
308 *step_list_ptr = im->gdes[im->gdes[i].vidx].step;
309 /* printf("%s:%lu\n",im->gdes[i].legend,*step_list_ptr); */
311 /* reserve room for one legend entry */
312 /* is FMT_LEG_LEN + 5 the correct size? */
313 if ((legend_list[j] =
314 (char*)malloc(sizeof(char) * (FMT_LEG_LEN + 5))) == NULL) {
318 free(legend_list[j]);
321 rrd_set_error("malloc xport legend entry");
325 if (im->gdes[i].legend)
326 /* omit bounds check, should have the same size */
327 strcpy(legend_list[j++], im->gdes[i].legend);
329 legend_list[j++][0] = '\0';
333 /* find a common step */
334 *step = lcd(step_list);
335 /* printf("step: %lu\n",*step); */
338 *start = im->start - im->start % (*step);
339 *end = im->end - im->end % (*step) + (*step);
342 /* room for rearranged data */
343 /* this is a return value! */
344 row_cnt = ((*end) - (*start)) / (*step);
346 (rrd_value_t*)malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
349 rrd_set_error("malloc xport data area");
354 /* fill data structure */
355 for (dst_row = 0; (int) dst_row < (int) row_cnt; dst_row++) {
356 for (i = 0; i < (int) (*col_cnt); i++) {
357 long vidx = im->gdes[ref_list[i]].vidx;
358 time_t now = *start + dst_row * *step;
359 (*dstptr++) = im->gdes[vidx].data[(unsigned long)
361 (now - im->gdes[vidx].start)
362 /im->gdes[vidx].step)
363 * im->gdes[vidx].ds_cnt +
369 *legend_v = legend_list;
375 int rrd_graph_xport(image_desc_t *im) {
376 /* prepare the data for processing */
377 unsigned long col_cnt=0;
378 time_t start=im->start;
380 unsigned long step=im->step;
381 char **legend_v=NULL;
382 rrd_value_t *data=NULL;
383 /* initialize buffer */
384 stringbuffer_t buffer={0,0,NULL,NULL};
386 /* check if we have a supported ggraph format */
387 switch (im->graph_type) {
388 /* allow the following to pass */
393 rrd_set_error("Not supported graph type");
397 /* if we write a file, then open it */
398 if (strlen(im->graphfile)) {
399 buffer.file=fopen(im->graphfile,"w");
402 /* do the data processing */
403 if (rrd_xport_fn(im,&start,&end,&step,&col_cnt,&legend_v,&data,1)) { return -1;}
405 /* fill in some data */
408 grinfo_push(im, sprintf_alloc("graph_start"), RD_I_CNT, info);
410 grinfo_push(im, sprintf_alloc("graph_end"), RD_I_CNT, info);
412 grinfo_push(im, sprintf_alloc("graph_step"), RD_I_CNT, info);
415 char *old_locale = setlocale(LC_NUMERIC,NULL);
416 setlocale(LC_NUMERIC, "C");
418 /* format it for output */
420 switch(im->imgformat) {
422 r=rrd_xport_format_xmljson(2,&buffer,im, start, end, step, col_cnt, legend_v, data);
425 r=rrd_xport_format_xmljson(6,&buffer,im, start, end, step, col_cnt, legend_v, data);
428 r=rrd_xport_format_xmljson(1,&buffer,im, start, end, step, col_cnt, legend_v, data);
431 r=rrd_xport_format_xmljson(3,&buffer,im, start, end, step, col_cnt, legend_v, data);
434 r=rrd_xport_format_sv(',',&buffer,im, start, end, step, col_cnt, legend_v, data);
437 r=rrd_xport_format_sv('\t',&buffer,im, start, end, step, col_cnt, legend_v, data);
440 r=rrd_xport_format_sv(';',&buffer,im, start, end, step, col_cnt, legend_v, data);
446 setlocale(LC_NUMERIC, old_locale);
450 for (unsigned long j = 0; j < col_cnt; j++) {
457 if (buffer.data) {free(buffer.data);}
459 if (buffer.file) {fclose(buffer.file);}
460 /* and return with error */
464 /* now do the cleanup */
466 fclose(buffer.file); buffer.file=NULL;
467 im->rendered_image_size=0;
468 im->rendered_image=NULL;
470 im->rendered_image_size=buffer.len;
471 im->rendered_image=buffer.data;
474 /* and print stuff */
475 return print_calc(im);
478 int addToBuffer(stringbuffer_t * sb,char* data,size_t len) {
479 /* if len <= 0 we assume a string and calculate the length ourself */
480 if (len<=0) { len=strlen(data); }
481 /* if we have got a file, then take the shortcut */
484 fwrite(data,len,1,sb->file);
487 /* if buffer is 0, then initialize */
489 /* make buffer a multiple of 8192 */
491 sb->allocated-=(sb->allocated%8192);
492 /* and allocate it */
493 sb->data=malloc(sb->allocated);
495 rrd_set_error("malloc issue");
498 /* and initialize the buffer */
502 /* and figure out if we need to extend the buffer */
503 if (sb->len+len+1>=sb->allocated) {
504 /* add so many pages until we have a buffer big enough */
505 while(sb->len+len+1>=sb->allocated) {
508 /* try to resize it */
509 unsigned char* resized=(unsigned char*)realloc(sb->data,sb->allocated);
516 rrd_set_error("realloc issue");
520 /* and finally add to the buffer */
521 memcpy(sb->data+sb->len,data,len);
523 /* and 0 terminate it */
529 int rrd_xport_format_sv(char sep, stringbuffer_t *buffer,image_desc_t *im,time_t start, time_t end, unsigned long step, unsigned long col_cnt, char **legend_v, rrd_value_t* data) {
530 /* define the time format */
532 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
535 unsigned long row_cnt=(end-start)/step;
537 /* estimate buffer size (to avoid multiple allocations) */
539 1024 /* bytes of overhead /header/footer */
540 +(12+19*col_cnt) /* 12 bytes overhead/line plus 19 bytes per column*/
541 *(1+row_cnt) /* number of columns + 1 (for header) */
546 /* now start writing the header*/
547 if (addToBuffer(buffer,"\"time\"",6)) { return 1; }
548 for(unsigned long i=0;i<col_cnt;i++) {
549 /* strip leading spaces */
550 char *t=legend_v[i]; while (isspace(*t)) { t++;}
552 snprintf(buf,255,"%c\"%s\"",sep,t);
553 if (addToBuffer(buffer,buf,0)) { return 1;}
555 if (addToBuffer(buffer,"\r\n",2)) { return 1; }
556 /* and now write the data */
557 rrd_value_t *ptr=data;
558 for(time_t ti=start+step;ti<end;ti+=step) {
562 localtime_r(&ti,&loc);
563 strftime(buf,254,timefmt,&loc);
565 snprintf(buf,254,"%lld",(long long int)ti);
567 if (addToBuffer(buffer,buf,0)) { return 1; }
568 /* write the columns */
569 for(unsigned long i=0;i<col_cnt;i++) {
571 rrd_value_t v=*ptr;ptr++;
574 snprintf(buf,255,"%c\"NaN\"",sep);
576 snprintf(buf,255,"%c\"%0.10e\"",sep,v);
578 if (addToBuffer(buffer,buf,0)) { return 1;}
580 /* and add a newline */
581 if (addToBuffer(buffer,"\r\n",2)) { return 1; }
588 int rrd_xport_format_xmljson(int flags,stringbuffer_t *buffer,image_desc_t *im,time_t start, time_t end, unsigned long step, unsigned long col_cnt, char **legend_v, rrd_value_t* data) {
590 /* define some other stuff based on flags */
592 if (flags &1) { json=1; }
594 if (flags &2) { showtime=1;}
596 if (flags &4) { enumds=1;}
598 /* define the time format */
600 /* unfortunatley we have to do it this way,
601 as when no --x-graph argument is given,
602 then the xlab_user is not in a clean state (e.g. zero-filled) */
603 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
606 unsigned long row_cnt=(end-start)/step;
608 /* estimate buffer size (to avoid multiple allocations) */
609 /* better estimates are needed here */
611 1024 /* bytes of overhead /header/footer */
612 +(12+19*col_cnt) /* 12 bytes overhead/line plus 19 bytes per column*/
613 *(1+row_cnt) /* number of columns + 1 (for header) */
618 rrd_value_t *ptr = data;
620 snprintf(buf,sizeof(buf),
621 "<?xml version=\"1.0\" encoding=\"%s\"?>\n\n<%s>\n <%s>\n",
622 XML_ENCODING,ROOT_TAG,META_TAG);
623 addToBuffer(buffer,buf,0);
626 addToBuffer(buffer,"{ \"about\": \"RRDtool graph JSON output\",\n \"meta\": {\n",0);
629 /* calculate start time */
632 time_t ti=start+step;
633 localtime_r(&ti,&loc);
634 strftime(dbuf,sizeof(dbuf),timefmt,&loc);
636 snprintf(buf,sizeof(buf)," \"%s\": \"%s\",\n",META_START_TAG,dbuf);
638 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n",META_START_TAG,dbuf,META_START_TAG);
642 snprintf(buf,sizeof(buf)," \"%s\": %lld,\n",META_START_TAG,(long long int)start+step);
644 snprintf(buf,sizeof(buf)," <%s>%lld</%s>\n",META_START_TAG,(long long int)start+step,META_START_TAG);
647 addToBuffer(buffer,buf,0);
649 /* calculate end time */
653 localtime_r(&ti,&loc);
654 strftime(dbuf,sizeof(dbuf),timefmt,&loc);
656 snprintf(buf,sizeof(buf)," \"%s\": \"%s\",\n",META_END_TAG,dbuf);
658 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n",META_END_TAG,dbuf,META_END_TAG);
662 snprintf(buf,sizeof(buf)," \"%s\": %lld,\n",META_END_TAG,(long long int)end);
664 snprintf(buf,sizeof(buf)," <%s>%lld</%s>\n",META_END_TAG,(long long int)end,META_END_TAG);
667 addToBuffer(buffer,buf,0);
668 /* print other info */
670 snprintf(buf,sizeof(buf)," \"%s\": %lld,\n",META_STEP_TAG,(long long int)step);
671 addToBuffer(buffer,buf,0);
673 snprintf(buf,sizeof(buf)," <%s>%lld</%s>\n",META_STEP_TAG,(long long int)step,META_STEP_TAG);
674 addToBuffer(buffer,buf,0);
675 snprintf(buf,sizeof(buf)," <%s>%lu</%s>\n",META_ROWS_TAG,row_cnt,META_ROWS_TAG);
676 addToBuffer(buffer,buf,0);
677 snprintf(buf,sizeof(buf)," <%s>%lu</%s>\n",META_COLS_TAG,col_cnt,META_COLS_TAG);
678 addToBuffer(buffer,buf,0);
683 snprintf(buf,sizeof(buf)," \"%s\": [\n", LEGEND_TAG);
686 snprintf(buf,sizeof(buf)," <%s>\n", LEGEND_TAG);
688 addToBuffer(buffer,buf,0);
689 /* add legend entries */
690 for (unsigned long j = 0; j < col_cnt; j++) {
691 char *entry = legend_v[j];
692 /* I do not know why the legend is "spaced", but let us skip it */
693 while(isspace(*entry)){entry++;}
696 snprintf(buf,sizeof(buf)," \"%s\"", entry);
697 addToBuffer(buffer,buf,0);
699 addToBuffer(buffer,",",1);
701 addToBuffer(buffer,"\n",1);
704 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n", LEGEND_ENTRY_TAG, entry,LEGEND_ENTRY_TAG);
705 addToBuffer(buffer,buf,0);
710 snprintf(buf,sizeof(buf)," ],\n");
713 snprintf(buf,sizeof(buf)," </%s>\n", LEGEND_TAG);
715 addToBuffer(buffer,buf,0);
717 /* add graphs and prints */
718 if (rrd_xport_format_addprints(json,buffer,im)) {return -1;}
720 /* if we have got a trailing , then kill it */
722 if (buffer->data[buffer->len-2]==',') {
723 buffer->data[buffer->len-2]=buffer->data[buffer->len-1];
730 snprintf(buf,sizeof(buf)," },\n");
732 snprintf(buf,sizeof(buf)," </%s>\n", META_TAG);
734 addToBuffer(buffer,buf,0);
739 snprintf(buf,sizeof(buf)," \"%s\": [\n",DATA_TAG);
741 snprintf(buf,sizeof(buf)," <%s>\n", DATA_TAG);
743 addToBuffer(buffer,buf,0);
744 /* iterate over data */
745 for (time_t ti = start + step; ti <= end; ti += step) {
748 localtime_r(&ti,&loc);
749 strftime(dbuf,sizeof(dbuf),timefmt,&loc);
751 snprintf(dbuf,sizeof(dbuf),"%lld",(long long int)ti);
754 addToBuffer(buffer," [ ",0);
756 addToBuffer(buffer,"\"",1);
757 addToBuffer(buffer,dbuf,0);
758 addToBuffer(buffer,"\",",2);
763 snprintf(buf,sizeof(buf),
764 " <%s><%s>%s</%s>", DATA_ROW_TAG,COL_TIME_TAG, dbuf, COL_TIME_TAG);
766 snprintf(buf,sizeof(buf),
767 " <%s>", DATA_ROW_TAG);
769 addToBuffer(buffer,buf,0);
771 for (unsigned long j = 0; j < col_cnt; j++) {
772 rrd_value_t newval = DNAN;
776 addToBuffer(buffer,"null",0);
778 snprintf(buf,sizeof(buf),"%0.10e",newval);
779 addToBuffer(buffer,buf,0);
782 addToBuffer(buffer,", ",0);
788 snprintf(buf,sizeof(buf),"<%s%lu>NaN</%s%lu>", COL_DATA_TAG,j,COL_DATA_TAG,j);
790 snprintf(buf,sizeof(buf),"<%s>NaN</%s>", COL_DATA_TAG,COL_DATA_TAG);
794 snprintf(buf,sizeof(buf),"<%s%lu>%0.10e</%s%lu>", COL_DATA_TAG,j,newval,COL_DATA_TAG,j);
796 snprintf(buf,sizeof(buf),"<%s>%0.10e</%s>", COL_DATA_TAG,newval,COL_DATA_TAG);
799 addToBuffer(buffer,buf,0);
804 addToBuffer(buffer,(ti < end ? " ],\n" : " ]\n"),0);
807 snprintf(buf,sizeof(buf),"</%s>\n", DATA_ROW_TAG);
808 addToBuffer(buffer,buf,0);
813 addToBuffer(buffer," ]\n",0);
816 snprintf(buf,sizeof(buf)," </%s>\n",DATA_TAG);
817 addToBuffer(buffer,buf,0);
822 addToBuffer(buffer,"}\n",0);
824 snprintf(buf,sizeof(buf),"</%s>\n", ROOT_TAG);
825 addToBuffer(buffer,buf,0);
830 void escapeJSON(char* txt,size_t len) {
831 char *tmp=(char*)malloc(len+2);
832 size_t l=strlen(txt);
834 /* now iterate over the chars */
835 for(size_t i=0;(i<l)&&(pos<len);i++,pos++) {
849 /* and copy back over txt */
850 strncpy(txt,tmp,len);
851 /* and release tmp */
855 int rrd_xport_format_addprints(int flags,stringbuffer_t *buffer,image_desc_t *im) {
856 /* initialize buffer */
857 stringbuffer_t prints={1024,0,NULL,NULL};
858 stringbuffer_t rules={1024,0,NULL,NULL};
859 stringbuffer_t gprints={4096,0,NULL,NULL};
864 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
866 /* define some other stuff based on flags */
868 if (flags &1) { json=1; }
870 if (flags &2) { showtime=1;}
872 if (flags &4) { enumds=1;}
874 /* define some values */
875 time_t now = time(NULL);
877 localtime_r(&now, &tmvdef);
878 double printval=DNAN;
880 /* iterate over all fields and start the work writing to the correct buffers */
881 for (long i = 0; i < im->gdes_c; i++) {
882 long vidx = im->gdes[i].vidx;
884 switch (im->gdes[i].gf) {
887 /* PRINT and GPRINT can now print VDEF generated values.
888 * There's no need to do any calculations on them as these
889 * calculations were already made.
890 * we do not support the depreciated version here
893 /* decide to which buffer we print to*/
894 stringbuffer_t *usebuffer=&gprints;
895 char* usetag="gprint";
896 if (im->gdes[i].gf==GF_PRINT) {
901 if (im->gdes[vidx].gf == GF_VDEF) { /* simply use vals */
902 printval = im->gdes[vidx].vf.val;
903 localtime_r(&im->gdes[vidx].vf.when, &tmvdef);
905 int max_ii = ((im->gdes[vidx].end - im->gdes[vidx].start)
906 / im->gdes[vidx].step * im->gdes[vidx].ds_cnt);
909 for (long ii = im->gdes[vidx].ds;
910 ii < max_ii; ii += im->gdes[vidx].ds_cnt) {
911 if (!finite(im->gdes[vidx].data[ii]))
913 if (isnan(printval)) {
914 printval = im->gdes[vidx].data[ii];
919 switch (im->gdes[i].cf) {
927 printval += im->gdes[vidx].data[ii];
930 printval = min(printval, im->gdes[vidx].data[ii]);
934 printval = max(printval, im->gdes[vidx].data[ii]);
937 printval = im->gdes[vidx].data[ii];
940 if (im->gdes[i].cf == CF_AVERAGE || im->gdes[i].cf > CF_LAST) {
941 if (validsteps > 1) {
942 printval = (printval / validsteps);
946 /* we handle PRINT and GPRINT the same - format now*/
947 if (im->gdes[i].strftm) {
948 if (im->gdes[vidx].vf.never == 1) {
949 time_clean(buf, im->gdes[i].format);
951 strftime(dbuf,sizeof(dbuf), im->gdes[i].format, &tmvdef);
953 } else if (bad_format(im->gdes[i].format)) {
955 ("bad format for PRINT in \"%s'", im->gdes[i].format);
958 snprintf(dbuf,sizeof(dbuf), im->gdes[i].format, printval,"");
962 escapeJSON(dbuf,sizeof(dbuf));
963 snprintf(buf,sizeof(buf),",\n { \"%s\": \"%s\" }",usetag,dbuf);
965 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n",usetag,dbuf,usetag);
967 addToBuffer(usebuffer,buf,0);
971 strncpy(dbuf,im->gdes[i].legend,sizeof(dbuf));
972 escapeJSON(dbuf,sizeof(dbuf));
973 snprintf(buf,sizeof(buf),",\n { \"comment\": \"%s\" }",dbuf);
975 snprintf(buf,sizeof(buf)," <comment>%s</comment>\n",im->gdes[i].legend);
977 addToBuffer(&gprints,buf,0);
980 entry = im->gdes[i].legend;
981 /* I do not know why the legend is "spaced", but let us skip it */
982 while(isspace(*entry)){entry++;}
984 snprintf(buf,sizeof(buf),",\n { \"line\": \"%s\" }",entry);
986 snprintf(buf,sizeof(buf)," <line>%s</line>\n",entry);
988 addToBuffer(&gprints,buf,0);
992 snprintf(buf,sizeof(buf),",\n { \"area\": \"%s\" }",im->gdes[i].legend);
994 snprintf(buf,sizeof(buf)," <area>%s</area>\n",im->gdes[i].legend);
996 addToBuffer(&gprints,buf,0);
1000 snprintf(buf,sizeof(buf),",\n { \"stack\": \"%s\" }",im->gdes[i].legend);
1002 snprintf(buf,sizeof(buf)," <stack>%s</stack>\n",im->gdes[i].legend);
1004 addToBuffer(&gprints,buf,0);
1008 switch (im->gdes[i].txtalign) {
1009 case TXA_LEFT: val="left"; break;
1010 case TXA_RIGHT: val="right"; break;
1011 case TXA_CENTER: val="center"; break;
1012 case TXA_JUSTIFIED: val="justified"; break;
1015 snprintf(buf,sizeof(buf),",\n { \"align\": \"%s\" }",val);
1017 snprintf(buf,sizeof(buf)," <align>%s</align>\n",val);
1019 addToBuffer(&gprints,buf,0);
1022 /* This does not work as expected - Tobi please help!!! */
1023 snprintf(dbuf,sizeof(dbuf),"%0.10e",im->gdes[i].vf.val);
1026 snprintf(buf,sizeof(buf),",\n { \"hrule\": \"%s\" }",dbuf);
1028 snprintf(buf,sizeof(buf)," <hrule>%s</hrule>\n",dbuf);
1030 addToBuffer(&rules,buf,0);
1035 localtime_r(&im->gdes[i].xrule,&loc);
1036 strftime(dbuf,254,timefmt,&loc);
1038 snprintf(dbuf,254,"%lld",(long long int)im->gdes[i].vf.when);
1042 snprintf(buf,sizeof(buf),",\n { \"vrule\": \"%s\" }",dbuf);
1044 snprintf(buf,sizeof(buf)," <vrule>%s</vrule>\n",dbuf);
1046 addToBuffer(&rules,buf,0);
1052 /* now add prints */
1055 snprintf(buf,sizeof(buf)," \"%s\": [\n","prints");
1056 addToBuffer(buffer,buf,0);
1057 addToBuffer(buffer,(char*)prints.data+2,prints.len-2);
1058 addToBuffer(buffer,"\n ],\n",0);
1060 snprintf(buf,sizeof(buf)," <%s>\n", "prints");
1061 addToBuffer(buffer,buf,0);
1062 addToBuffer(buffer,(char*)prints.data,prints.len);
1063 snprintf(buf,sizeof(buf)," </%s>\n", "prints");
1064 addToBuffer(buffer,buf,0);
1068 /* now add gprints */
1071 snprintf(buf,sizeof(buf)," \"%s\": [\n","gprints");
1072 addToBuffer(buffer,buf,0);
1073 addToBuffer(buffer,(char*)gprints.data+2,gprints.len-2);
1074 addToBuffer(buffer,"\n ],\n",0);
1076 snprintf(buf,sizeof(buf)," <%s>\n", "gprints");
1077 addToBuffer(buffer,buf,0);
1078 addToBuffer(buffer,(char*)gprints.data,gprints.len);
1079 snprintf(buf,sizeof(buf)," </%s>\n", "gprints");
1080 addToBuffer(buffer,buf,0);
1087 snprintf(buf,sizeof(buf)," \"%s\": [\n","rules");
1088 addToBuffer(buffer,buf,0);
1089 addToBuffer(buffer,(char*)rules.data+2,rules.len-2);
1090 addToBuffer(buffer,"\n ],\n",0);
1092 snprintf(buf,sizeof(buf)," <%s>\n", "rules");
1093 addToBuffer(buffer,buf,0);
1094 addToBuffer(buffer,(char*)rules.data,rules.len);
1095 snprintf(buf,sizeof(buf)," </%s>\n", "rules");
1096 addToBuffer(buffer,buf,0);