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};
385 /* if we write a file, then open it */
386 if (strlen(im->graphfile)) {
387 buffer.file=fopen(im->graphfile,"w");
390 /* do the data processing */
391 if (rrd_xport_fn(im,&start,&end,&step,&col_cnt,&legend_v,&data,1)) { return -1;}
393 /* fill in some data */
396 grinfo_push(im, sprintf_alloc("graph_start"), RD_I_CNT, info);
398 grinfo_push(im, sprintf_alloc("graph_end"), RD_I_CNT, info);
400 grinfo_push(im, sprintf_alloc("graph_step"), RD_I_CNT, info);
403 char *old_locale = setlocale(LC_NUMERIC,NULL);
404 setlocale(LC_NUMERIC, "C");
406 /* format it for output */
408 switch(im->imgformat) {
410 r=rrd_xport_format_xmljson(2,&buffer,im, start, end, step, col_cnt, legend_v, data);
413 r=rrd_xport_format_xmljson(6,&buffer,im, start, end, step, col_cnt, legend_v, data);
416 r=rrd_xport_format_xmljson(1,&buffer,im, start, end, step, col_cnt, legend_v, data);
419 r=rrd_xport_format_xmljson(3,&buffer,im, start, end, step, col_cnt, legend_v, data);
422 r=rrd_xport_format_sv(',',&buffer,im, start, end, step, col_cnt, legend_v, data);
425 r=rrd_xport_format_sv('\t',&buffer,im, start, end, step, col_cnt, legend_v, data);
428 r=rrd_xport_format_sv(';',&buffer,im, start, end, step, col_cnt, legend_v, data);
434 setlocale(LC_NUMERIC, old_locale);
438 for (unsigned long j = 0; j < col_cnt; j++) {
445 if (buffer.data) {free(buffer.data);}
447 if (buffer.file) {fclose(buffer.file);}
448 /* and return with error */
452 /* now do the cleanup */
454 fclose(buffer.file); buffer.file=NULL;
455 im->rendered_image_size=0;
456 im->rendered_image=NULL;
458 im->rendered_image_size=buffer.len;
459 im->rendered_image=buffer.data;
462 /* and print stuff */
463 return print_calc(im);
466 int addToBuffer(stringbuffer_t * sb,char* data,size_t len) {
467 /* if len <= 0 we assume a string and calculate the length ourself */
468 if (len<=0) { len=strlen(data); }
469 /* if we have got a file, then take the shortcut */
472 fwrite(data,len,1,sb->file);
475 /* if buffer is 0, then initialize */
477 /* make buffer a multiple of 8192 */
479 sb->allocated-=(sb->allocated%8192);
480 /* and allocate it */
481 sb->data=malloc(sb->allocated);
483 rrd_set_error("malloc issue");
486 /* and initialize the buffer */
490 /* and figure out if we need to extend the buffer */
491 if (sb->len+len+1>=sb->allocated) {
492 /* add so many pages until we have a buffer big enough */
493 while(sb->len+len+1>=sb->allocated) {
496 /* try to resize it */
497 unsigned char* resized=(unsigned char*)realloc(sb->data,sb->allocated);
504 rrd_set_error("realloc issue");
508 /* and finally add to the buffer */
509 memcpy(sb->data+sb->len,data,len);
511 /* and 0 terminate it */
517 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) {
518 /* define the time format */
520 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
523 unsigned long row_cnt=(end-start)/step;
525 /* estimate buffer size (to avoid multiple allocations) */
527 1024 /* bytes of overhead /header/footer */
528 +(12+19*col_cnt) /* 12 bytes overhead/line plus 19 bytes per column*/
529 *(1+row_cnt) /* number of columns + 1 (for header) */
534 /* now start writing the header*/
535 if (addToBuffer(buffer,"\"time\"",6)) { return 1; }
536 for(unsigned long i=0;i<col_cnt;i++) {
537 /* strip leading spaces */
538 char *t=legend_v[i]; while (isspace(*t)) { t++;}
540 snprintf(buf,255,"%c\"%s\"",sep,t);
541 if (addToBuffer(buffer,buf,0)) { return 1;}
543 if (addToBuffer(buffer,"\r\n",2)) { return 1; }
544 /* and now write the data */
545 rrd_value_t *ptr=data;
546 for(time_t ti=start+step;ti<end;ti+=step) {
550 localtime_r(&ti,&loc);
551 strftime(buf,254,timefmt,&loc);
553 snprintf(buf,254,"%lld",(long long int)ti);
555 if (addToBuffer(buffer,buf,0)) { return 1; }
556 /* write the columns */
557 for(unsigned long i=0;i<col_cnt;i++) {
559 rrd_value_t v=*ptr;ptr++;
562 snprintf(buf,255,"%c\"NaN\"",sep);
564 snprintf(buf,255,"%c\"%0.10e\"",sep,v);
566 if (addToBuffer(buffer,buf,0)) { return 1;}
568 /* and add a newline */
569 if (addToBuffer(buffer,"\r\n",2)) { return 1; }
576 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) {
578 /* define some other stuff based on flags */
580 if (flags &1) { json=1; }
582 if (flags &2) { showtime=1;}
584 if (flags &4) { enumds=1;}
586 /* define the time format */
588 /* unfortunatley we have to do it this way,
589 as when no --x-graph argument is given,
590 then the xlab_user is not in a clean state (e.g. zero-filled) */
591 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
594 unsigned long row_cnt=(end-start)/step;
596 /* estimate buffer size (to avoid multiple allocations) */
597 /* better estimates are needed here */
599 1024 /* bytes of overhead /header/footer */
600 +(12+19*col_cnt) /* 12 bytes overhead/line plus 19 bytes per column*/
601 *(1+row_cnt) /* number of columns + 1 (for header) */
606 rrd_value_t *ptr = data;
608 snprintf(buf,sizeof(buf),
609 "<?xml version=\"1.0\" encoding=\"%s\"?>\n\n<%s>\n <%s>\n",
610 XML_ENCODING,ROOT_TAG,META_TAG);
611 addToBuffer(buffer,buf,0);
614 addToBuffer(buffer,"{ \"about\": \"RRDtool graph JSON output\",\n \"meta\": {\n",0);
617 /* calculate start time */
620 time_t ti=start+step;
621 localtime_r(&ti,&loc);
622 strftime(dbuf,sizeof(dbuf),timefmt,&loc);
624 snprintf(buf,sizeof(buf)," \"%s\": \"%s\",\n",META_START_TAG,dbuf);
626 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n",META_START_TAG,dbuf,META_START_TAG);
630 snprintf(buf,sizeof(buf)," \"%s\": %lld,\n",META_START_TAG,(long long int)start+step);
632 snprintf(buf,sizeof(buf)," <%s>%lld</%s>\n",META_START_TAG,(long long int)start+step,META_START_TAG);
635 addToBuffer(buffer,buf,0);
637 /* calculate end time */
641 localtime_r(&ti,&loc);
642 strftime(dbuf,sizeof(dbuf),timefmt,&loc);
644 snprintf(buf,sizeof(buf)," \"%s\": \"%s\",\n",META_END_TAG,dbuf);
646 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n",META_END_TAG,dbuf,META_END_TAG);
650 snprintf(buf,sizeof(buf)," \"%s\": %lld,\n",META_END_TAG,(long long int)end);
652 snprintf(buf,sizeof(buf)," <%s>%lld</%s>\n",META_END_TAG,(long long int)end,META_END_TAG);
655 addToBuffer(buffer,buf,0);
656 /* print other info */
658 snprintf(buf,sizeof(buf)," \"%s\": %lld,\n",META_STEP_TAG,(long long int)step);
659 addToBuffer(buffer,buf,0);
661 snprintf(buf,sizeof(buf)," <%s>%lld</%s>\n",META_STEP_TAG,(long long int)step,META_STEP_TAG);
662 addToBuffer(buffer,buf,0);
663 snprintf(buf,sizeof(buf)," <%s>%lu</%s>\n",META_ROWS_TAG,row_cnt,META_ROWS_TAG);
664 addToBuffer(buffer,buf,0);
665 snprintf(buf,sizeof(buf)," <%s>%lu</%s>\n",META_COLS_TAG,col_cnt,META_COLS_TAG);
666 addToBuffer(buffer,buf,0);
671 snprintf(buf,sizeof(buf)," \"%s\": [\n", LEGEND_TAG);
674 snprintf(buf,sizeof(buf)," <%s>\n", LEGEND_TAG);
676 addToBuffer(buffer,buf,0);
677 /* add legend entries */
678 for (unsigned long j = 0; j < col_cnt; j++) {
679 char *entry = legend_v[j];
680 /* I do not know why the legend is "spaced", but let us skip it */
681 while(isspace(*entry)){entry++;}
684 snprintf(buf,sizeof(buf)," \"%s\"", entry);
685 addToBuffer(buffer,buf,0);
687 addToBuffer(buffer,",",1);
689 addToBuffer(buffer,"\n",1);
692 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n", LEGEND_ENTRY_TAG, entry,LEGEND_ENTRY_TAG);
693 addToBuffer(buffer,buf,0);
698 snprintf(buf,sizeof(buf)," ],\n");
701 snprintf(buf,sizeof(buf)," </%s>\n", LEGEND_TAG);
703 addToBuffer(buffer,buf,0);
705 /* add graphs and prints */
706 if (rrd_xport_format_addprints(json,buffer,im)) {return -1;}
708 /* if we have got a trailing , then kill it */
710 if (buffer->data[buffer->len-2]==',') {
711 buffer->data[buffer->len-2]=buffer->data[buffer->len-1];
718 snprintf(buf,sizeof(buf)," },\n");
720 snprintf(buf,sizeof(buf)," </%s>\n", META_TAG);
722 addToBuffer(buffer,buf,0);
727 snprintf(buf,sizeof(buf)," \"%s\": [\n",DATA_TAG);
729 snprintf(buf,sizeof(buf)," <%s>\n", DATA_TAG);
731 addToBuffer(buffer,buf,0);
732 /* iterate over data */
733 for (time_t ti = start + step; ti <= end; ti += step) {
736 localtime_r(&ti,&loc);
737 strftime(dbuf,sizeof(dbuf),timefmt,&loc);
739 snprintf(dbuf,sizeof(dbuf),"%lld",(long long int)ti);
742 addToBuffer(buffer," [ ",0);
744 addToBuffer(buffer,"\"",1);
745 addToBuffer(buffer,dbuf,0);
746 addToBuffer(buffer,"\",",2);
751 snprintf(buf,sizeof(buf),
752 " <%s><%s>%s</%s>", DATA_ROW_TAG,COL_TIME_TAG, dbuf, COL_TIME_TAG);
754 snprintf(buf,sizeof(buf),
755 " <%s>", DATA_ROW_TAG);
757 addToBuffer(buffer,buf,0);
759 for (unsigned long j = 0; j < col_cnt; j++) {
760 rrd_value_t newval = DNAN;
764 addToBuffer(buffer,"null",0);
766 snprintf(buf,sizeof(buf),"%0.10e",newval);
767 addToBuffer(buffer,buf,0);
770 addToBuffer(buffer,", ",0);
776 snprintf(buf,sizeof(buf),"<%s%lu>NaN</%s%lu>", COL_DATA_TAG,j,COL_DATA_TAG,j);
778 snprintf(buf,sizeof(buf),"<%s>NaN</%s>", COL_DATA_TAG,COL_DATA_TAG);
782 snprintf(buf,sizeof(buf),"<%s%lu>%0.10e</%s%lu>", COL_DATA_TAG,j,newval,COL_DATA_TAG,j);
784 snprintf(buf,sizeof(buf),"<%s>%0.10e</%s>", COL_DATA_TAG,newval,COL_DATA_TAG);
787 addToBuffer(buffer,buf,0);
792 addToBuffer(buffer,(ti < end ? " ],\n" : " ]\n"),0);
795 snprintf(buf,sizeof(buf),"</%s>\n", DATA_ROW_TAG);
796 addToBuffer(buffer,buf,0);
801 addToBuffer(buffer," ]\n",0);
804 snprintf(buf,sizeof(buf)," </%s>\n",DATA_TAG);
805 addToBuffer(buffer,buf,0);
810 addToBuffer(buffer,"}\n",0);
812 snprintf(buf,sizeof(buf),"</%s>\n", ROOT_TAG);
813 addToBuffer(buffer,buf,0);
818 void escapeJSON(char* txt,size_t len) {
819 char *tmp=(char*)malloc(len+2);
820 size_t l=strlen(txt);
822 /* now iterate over the chars */
823 for(size_t i=0;(i<l)&&(pos<len);i++,pos++) {
837 /* and copy back over txt */
838 strncpy(txt,tmp,len);
839 /* and release tmp */
843 int rrd_xport_format_addprints(int flags,stringbuffer_t *buffer,image_desc_t *im) {
844 /* initialize buffer */
845 stringbuffer_t prints={1024,0,NULL,NULL};
846 stringbuffer_t rules={1024,0,NULL,NULL};
847 stringbuffer_t gprints={4096,0,NULL,NULL};
852 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
854 /* define some other stuff based on flags */
856 if (flags &1) { json=1; }
858 if (flags &2) { showtime=1;}
860 if (flags &4) { enumds=1;}
862 /* define some values */
863 time_t now = time(NULL);
865 localtime_r(&now, &tmvdef);
866 double printval=DNAN;
868 /* iterate over all fields and start the work writing to the correct buffers */
869 for (long i = 0; i < im->gdes_c; i++) {
870 long vidx = im->gdes[i].vidx;
872 switch (im->gdes[i].gf) {
875 /* PRINT and GPRINT can now print VDEF generated values.
876 * There's no need to do any calculations on them as these
877 * calculations were already made.
878 * we do not support the depreciated version here
881 /* decide to which buffer we print to*/
882 stringbuffer_t *usebuffer=&gprints;
883 char* usetag="gprint";
884 if (im->gdes[i].gf==GF_PRINT) {
889 if (im->gdes[vidx].gf == GF_VDEF) { /* simply use vals */
890 printval = im->gdes[vidx].vf.val;
891 localtime_r(&im->gdes[vidx].vf.when, &tmvdef);
893 int max_ii = ((im->gdes[vidx].end - im->gdes[vidx].start)
894 / im->gdes[vidx].step * im->gdes[vidx].ds_cnt);
897 for (long ii = im->gdes[vidx].ds;
898 ii < max_ii; ii += im->gdes[vidx].ds_cnt) {
899 if (!finite(im->gdes[vidx].data[ii]))
901 if (isnan(printval)) {
902 printval = im->gdes[vidx].data[ii];
907 switch (im->gdes[i].cf) {
915 printval += im->gdes[vidx].data[ii];
918 printval = min(printval, im->gdes[vidx].data[ii]);
922 printval = max(printval, im->gdes[vidx].data[ii]);
925 printval = im->gdes[vidx].data[ii];
928 if (im->gdes[i].cf == CF_AVERAGE || im->gdes[i].cf > CF_LAST) {
929 if (validsteps > 1) {
930 printval = (printval / validsteps);
934 /* we handle PRINT and GPRINT the same - format now*/
935 if (im->gdes[i].strftm) {
936 if (im->gdes[vidx].vf.never == 1) {
937 time_clean(buf, im->gdes[i].format);
939 strftime(dbuf,sizeof(dbuf), im->gdes[i].format, &tmvdef);
941 } else if (bad_format(im->gdes[i].format)) {
943 ("bad format for PRINT in \"%s'", im->gdes[i].format);
946 snprintf(dbuf,sizeof(dbuf), im->gdes[i].format, printval,"");
950 escapeJSON(dbuf,sizeof(dbuf));
951 snprintf(buf,sizeof(buf),",\n { \"%s\": \"%s\" }",usetag,dbuf);
953 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n",usetag,dbuf,usetag);
955 addToBuffer(usebuffer,buf,0);
959 strncpy(dbuf,im->gdes[i].legend,sizeof(dbuf));
960 escapeJSON(dbuf,sizeof(dbuf));
961 snprintf(buf,sizeof(buf),",\n { \"comment\": \"%s\" }",dbuf);
963 snprintf(buf,sizeof(buf)," <comment>%s</comment>\n",im->gdes[i].legend);
965 addToBuffer(&gprints,buf,0);
968 entry = im->gdes[i].legend;
969 /* I do not know why the legend is "spaced", but let us skip it */
970 while(isspace(*entry)){entry++;}
972 snprintf(buf,sizeof(buf),",\n { \"line\": \"%s\" }",entry);
974 snprintf(buf,sizeof(buf)," <line>%s</line>\n",entry);
976 addToBuffer(&gprints,buf,0);
980 snprintf(buf,sizeof(buf),",\n { \"area\": \"%s\" }",im->gdes[i].legend);
982 snprintf(buf,sizeof(buf)," <area>%s</area>\n",im->gdes[i].legend);
984 addToBuffer(&gprints,buf,0);
988 snprintf(buf,sizeof(buf),",\n { \"stack\": \"%s\" }",im->gdes[i].legend);
990 snprintf(buf,sizeof(buf)," <stack>%s</stack>\n",im->gdes[i].legend);
992 addToBuffer(&gprints,buf,0);
996 switch (im->gdes[i].txtalign) {
997 case TXA_LEFT: val="left"; break;
998 case TXA_RIGHT: val="right"; break;
999 case TXA_CENTER: val="center"; break;
1000 case TXA_JUSTIFIED: val="justified"; break;
1003 snprintf(buf,sizeof(buf),",\n { \"align\": \"%s\" }",val);
1005 snprintf(buf,sizeof(buf)," <align>%s</align>\n",val);
1007 addToBuffer(&gprints,buf,0);
1010 /* This does not work as expected - Tobi please help!!! */
1011 snprintf(dbuf,sizeof(dbuf),"%0.10e",im->gdes[i].vf.val);
1014 snprintf(buf,sizeof(buf),",\n { \"hrule\": \"%s\" }",dbuf);
1016 snprintf(buf,sizeof(buf)," <hrule>%s</hrule>\n",dbuf);
1018 addToBuffer(&rules,buf,0);
1023 localtime_r(&im->gdes[i].xrule,&loc);
1024 strftime(dbuf,254,timefmt,&loc);
1026 snprintf(dbuf,254,"%lld",(long long int)im->gdes[i].vf.when);
1030 snprintf(buf,sizeof(buf),",\n { \"vrule\": \"%s\" }",dbuf);
1032 snprintf(buf,sizeof(buf)," <vrule>%s</vrule>\n",dbuf);
1034 addToBuffer(&rules,buf,0);
1040 /* now add prints */
1043 snprintf(buf,sizeof(buf)," \"%s\": [\n","prints");
1044 addToBuffer(buffer,buf,0);
1045 addToBuffer(buffer,(char*)prints.data+2,prints.len-2);
1046 addToBuffer(buffer,"\n ],\n",0);
1048 snprintf(buf,sizeof(buf)," <%s>\n", "prints");
1049 addToBuffer(buffer,buf,0);
1050 addToBuffer(buffer,(char*)prints.data,prints.len);
1051 snprintf(buf,sizeof(buf)," </%s>\n", "prints");
1052 addToBuffer(buffer,buf,0);
1056 /* now add gprints */
1059 snprintf(buf,sizeof(buf)," \"%s\": [\n","gprints");
1060 addToBuffer(buffer,buf,0);
1061 addToBuffer(buffer,(char*)gprints.data+2,gprints.len-2);
1062 addToBuffer(buffer,"\n ],\n",0);
1064 snprintf(buf,sizeof(buf)," <%s>\n", "gprints");
1065 addToBuffer(buffer,buf,0);
1066 addToBuffer(buffer,(char*)gprints.data,gprints.len);
1067 snprintf(buf,sizeof(buf)," </%s>\n", "gprints");
1068 addToBuffer(buffer,buf,0);
1075 snprintf(buf,sizeof(buf)," \"%s\": [\n","rules");
1076 addToBuffer(buffer,buf,0);
1077 addToBuffer(buffer,(char*)rules.data+2,rules.len-2);
1078 addToBuffer(buffer,"\n ],\n",0);
1080 snprintf(buf,sizeof(buf)," <%s>\n", "rules");
1081 addToBuffer(buffer,buf,0);
1082 addToBuffer(buffer,(char*)rules.data,rules.len);
1083 snprintf(buf,sizeof(buf)," </%s>\n", "rules");
1084 addToBuffer(buffer,buf,0);