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); */
312 /* reserve room for one legend entry */
313 if ((legend_list[j] = strdup(im->gdes[i].legend)) == NULL) {
317 free(legend_list[j]);
320 rrd_set_error("malloc xport legend entry");
324 if (im->gdes[i].legend == 0)
325 legend_list[j][0] = '\0';
330 /* find a common step */
331 *step = lcd(step_list);
332 /* printf("step: %lu\n",*step); */
335 *start = im->start - im->start % (*step);
336 *end = im->end - im->end % (*step) + (*step);
339 /* room for rearranged data */
340 /* this is a return value! */
341 row_cnt = ((*end) - (*start)) / (*step);
343 (rrd_value_t*)malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
346 rrd_set_error("malloc xport data area");
351 /* fill data structure */
352 for (dst_row = 0; (int) dst_row < (int) row_cnt; dst_row++) {
353 for (i = 0; i < (int) (*col_cnt); i++) {
354 long vidx = im->gdes[ref_list[i]].vidx;
355 time_t now = *start + dst_row * *step;
356 (*dstptr++) = im->gdes[vidx].data[(unsigned long)
358 (now - im->gdes[vidx].start)
359 /im->gdes[vidx].step)
360 * im->gdes[vidx].ds_cnt +
366 *legend_v = legend_list;
372 int rrd_graph_xport(image_desc_t *im) {
373 /* prepare the data for processing */
374 unsigned long col_cnt=0;
375 time_t start=im->start;
377 unsigned long step=im->step;
378 char **legend_v=NULL;
379 rrd_value_t *data=NULL;
380 /* initialize buffer */
381 stringbuffer_t buffer={0,0,NULL,NULL};
383 /* check if we have a supported ggraph format */
384 switch (im->graph_type) {
385 /* allow the following to pass */
390 rrd_set_error("Not supported graph type");
394 /* if we write a file, then open it */
395 if (strlen(im->graphfile)) {
396 buffer.file=fopen(im->graphfile,"w");
399 /* do the data processing */
400 if (rrd_xport_fn(im,&start,&end,&step,&col_cnt,&legend_v,&data,1)) { return -1;}
402 /* fill in some data */
405 grinfo_push(im, sprintf_alloc("graph_start"), RD_I_CNT, info);
407 grinfo_push(im, sprintf_alloc("graph_end"), RD_I_CNT, info);
409 grinfo_push(im, sprintf_alloc("graph_step"), RD_I_CNT, info);
412 char *old_locale = setlocale(LC_NUMERIC,NULL);
413 setlocale(LC_NUMERIC, "C");
415 /* format it for output */
417 switch(im->imgformat) {
419 r=rrd_xport_format_xmljson(2,&buffer,im, start, end, step, col_cnt, legend_v, data);
422 r=rrd_xport_format_xmljson(6,&buffer,im, start, end, step, col_cnt, legend_v, data);
425 r=rrd_xport_format_xmljson(1,&buffer,im, start, end, step, col_cnt, legend_v, data);
428 r=rrd_xport_format_xmljson(3,&buffer,im, start, end, step, col_cnt, legend_v, data);
431 r=rrd_xport_format_sv(',',&buffer,im, start, end, step, col_cnt, legend_v, data);
434 r=rrd_xport_format_sv('\t',&buffer,im, start, end, step, col_cnt, legend_v, data);
437 r=rrd_xport_format_sv(';',&buffer,im, start, end, step, col_cnt, legend_v, data);
443 setlocale(LC_NUMERIC, old_locale);
447 for (unsigned long j = 0; j < col_cnt; j++) {
454 if (buffer.data) {free(buffer.data);}
456 if (buffer.file) {fclose(buffer.file);}
457 /* and return with error */
461 /* now do the cleanup */
463 fclose(buffer.file); buffer.file=NULL;
464 im->rendered_image_size=0;
465 im->rendered_image=NULL;
467 im->rendered_image_size=buffer.len;
468 im->rendered_image=buffer.data;
471 /* and print stuff */
472 return print_calc(im);
475 int addToBuffer(stringbuffer_t * sb,char* data,size_t len) {
476 /* if len <= 0 we assume a string and calculate the length ourself */
477 if (len<=0) { len=strlen(data); }
478 /* if we have got a file, then take the shortcut */
481 fwrite(data,len,1,sb->file);
484 /* if buffer is 0, then initialize */
486 /* make buffer a multiple of 8192 */
488 sb->allocated-=(sb->allocated%8192);
489 /* and allocate it */
490 sb->data=malloc(sb->allocated);
492 rrd_set_error("malloc issue");
495 /* and initialize the buffer */
499 /* and figure out if we need to extend the buffer */
500 if (sb->len+len+1>=sb->allocated) {
501 /* add so many pages until we have a buffer big enough */
502 while(sb->len+len+1>=sb->allocated) {
505 /* try to resize it */
506 unsigned char* resized=(unsigned char*)realloc(sb->data,sb->allocated);
513 rrd_set_error("realloc issue");
517 /* and finally add to the buffer */
518 memcpy(sb->data+sb->len,data,len);
520 /* and 0 terminate it */
526 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) {
527 /* define the time format */
529 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
532 unsigned long row_cnt=(end-start)/step;
534 /* estimate buffer size (to avoid multiple allocations) */
536 1024 /* bytes of overhead /header/footer */
537 +(12+19*col_cnt) /* 12 bytes overhead/line plus 19 bytes per column*/
538 *(1+row_cnt) /* number of columns + 1 (for header) */
543 /* now start writing the header*/
544 if (addToBuffer(buffer,"\"time\"",6)) { return 1; }
545 for(unsigned long i=0;i<col_cnt;i++) {
546 /* strip leading spaces */
547 char *t=legend_v[i]; while (isspace(*t)) { t++;}
549 snprintf(buf,255,"%c\"%s\"",sep,t);
550 if (addToBuffer(buffer,buf,0)) { return 1;}
552 if (addToBuffer(buffer,"\r\n",2)) { return 1; }
553 /* and now write the data */
554 rrd_value_t *ptr=data;
555 for(time_t ti=start+step;ti<end;ti+=step) {
559 localtime_r(&ti,&loc);
560 strftime(buf,254,timefmt,&loc);
562 snprintf(buf,254,"%lld",(long long int)ti);
564 if (addToBuffer(buffer,buf,0)) { return 1; }
565 /* write the columns */
566 for(unsigned long i=0;i<col_cnt;i++) {
568 rrd_value_t v=*ptr;ptr++;
571 snprintf(buf,255,"%c\"NaN\"",sep);
573 snprintf(buf,255,"%c\"%0.10e\"",sep,v);
575 if (addToBuffer(buffer,buf,0)) { return 1;}
577 /* and add a newline */
578 if (addToBuffer(buffer,"\r\n",2)) { return 1; }
585 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) {
587 /* define some other stuff based on flags */
589 if (flags &1) { json=1; }
591 if (flags &2) { showtime=1;}
593 if (flags &4) { enumds=1;}
595 /* define the time format */
597 /* unfortunatley we have to do it this way,
598 as when no --x-graph argument is given,
599 then the xlab_user is not in a clean state (e.g. zero-filled) */
600 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
603 unsigned long row_cnt=(end-start)/step;
605 /* estimate buffer size (to avoid multiple allocations) */
606 /* better estimates are needed here */
608 1024 /* bytes of overhead /header/footer */
609 +(12+19*col_cnt) /* 12 bytes overhead/line plus 19 bytes per column*/
610 *(1+row_cnt) /* number of columns + 1 (for header) */
615 rrd_value_t *ptr = data;
617 snprintf(buf,sizeof(buf),
618 "<?xml version=\"1.0\" encoding=\"%s\"?>\n\n<%s>\n <%s>\n",
619 XML_ENCODING,ROOT_TAG,META_TAG);
620 addToBuffer(buffer,buf,0);
623 addToBuffer(buffer,"{ \"about\": \"RRDtool graph JSON output\",\n \"meta\": {\n",0);
626 /* calculate start time */
629 time_t ti=start+step;
630 localtime_r(&ti,&loc);
631 strftime(dbuf,sizeof(dbuf),timefmt,&loc);
633 snprintf(buf,sizeof(buf)," \"%s\": \"%s\",\n",META_START_TAG,dbuf);
635 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n",META_START_TAG,dbuf,META_START_TAG);
639 snprintf(buf,sizeof(buf)," \"%s\": %lld,\n",META_START_TAG,(long long int)start+step);
641 snprintf(buf,sizeof(buf)," <%s>%lld</%s>\n",META_START_TAG,(long long int)start+step,META_START_TAG);
644 addToBuffer(buffer,buf,0);
646 /* calculate end time */
650 localtime_r(&ti,&loc);
651 strftime(dbuf,sizeof(dbuf),timefmt,&loc);
653 snprintf(buf,sizeof(buf)," \"%s\": \"%s\",\n",META_END_TAG,dbuf);
655 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n",META_END_TAG,dbuf,META_END_TAG);
659 snprintf(buf,sizeof(buf)," \"%s\": %lld,\n",META_END_TAG,(long long int)end);
661 snprintf(buf,sizeof(buf)," <%s>%lld</%s>\n",META_END_TAG,(long long int)end,META_END_TAG);
664 addToBuffer(buffer,buf,0);
665 /* print other info */
667 snprintf(buf,sizeof(buf)," \"%s\": %lld,\n",META_STEP_TAG,(long long int)step);
668 addToBuffer(buffer,buf,0);
670 snprintf(buf,sizeof(buf)," <%s>%lld</%s>\n",META_STEP_TAG,(long long int)step,META_STEP_TAG);
671 addToBuffer(buffer,buf,0);
672 snprintf(buf,sizeof(buf)," <%s>%lu</%s>\n",META_ROWS_TAG,row_cnt,META_ROWS_TAG);
673 addToBuffer(buffer,buf,0);
674 snprintf(buf,sizeof(buf)," <%s>%lu</%s>\n",META_COLS_TAG,col_cnt,META_COLS_TAG);
675 addToBuffer(buffer,buf,0);
680 snprintf(buf,sizeof(buf)," \"%s\": [\n", LEGEND_TAG);
683 snprintf(buf,sizeof(buf)," <%s>\n", LEGEND_TAG);
685 addToBuffer(buffer,buf,0);
686 /* add legend entries */
687 for (unsigned long j = 0; j < col_cnt; j++) {
688 char *entry = legend_v[j];
689 /* I do not know why the legend is "spaced", but let us skip it */
690 while(isspace(*entry)){entry++;}
693 snprintf(buf,sizeof(buf)," \"%s\"", entry);
694 addToBuffer(buffer,buf,0);
696 addToBuffer(buffer,",",1);
698 addToBuffer(buffer,"\n",1);
701 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n", LEGEND_ENTRY_TAG, entry,LEGEND_ENTRY_TAG);
702 addToBuffer(buffer,buf,0);
707 snprintf(buf,sizeof(buf)," ],\n");
710 snprintf(buf,sizeof(buf)," </%s>\n", LEGEND_TAG);
712 addToBuffer(buffer,buf,0);
714 /* add graphs and prints */
715 if (rrd_xport_format_addprints(json,buffer,im)) {return -1;}
717 /* if we have got a trailing , then kill it */
719 if (buffer->data[buffer->len-2]==',') {
720 buffer->data[buffer->len-2]=buffer->data[buffer->len-1];
727 snprintf(buf,sizeof(buf)," },\n");
729 snprintf(buf,sizeof(buf)," </%s>\n", META_TAG);
731 addToBuffer(buffer,buf,0);
736 snprintf(buf,sizeof(buf)," \"%s\": [\n",DATA_TAG);
738 snprintf(buf,sizeof(buf)," <%s>\n", DATA_TAG);
740 addToBuffer(buffer,buf,0);
741 /* iterate over data */
742 for (time_t ti = start + step; ti <= end; ti += step) {
745 localtime_r(&ti,&loc);
746 strftime(dbuf,sizeof(dbuf),timefmt,&loc);
748 snprintf(dbuf,sizeof(dbuf),"%lld",(long long int)ti);
751 addToBuffer(buffer," [ ",0);
753 addToBuffer(buffer,"\"",1);
754 addToBuffer(buffer,dbuf,0);
755 addToBuffer(buffer,"\",",2);
760 snprintf(buf,sizeof(buf),
761 " <%s><%s>%s</%s>", DATA_ROW_TAG,COL_TIME_TAG, dbuf, COL_TIME_TAG);
763 snprintf(buf,sizeof(buf),
764 " <%s>", DATA_ROW_TAG);
766 addToBuffer(buffer,buf,0);
768 for (unsigned long j = 0; j < col_cnt; j++) {
769 rrd_value_t newval = DNAN;
773 addToBuffer(buffer,"null",0);
775 snprintf(buf,sizeof(buf),"%0.10e",newval);
776 addToBuffer(buffer,buf,0);
779 addToBuffer(buffer,", ",0);
785 snprintf(buf,sizeof(buf),"<%s%lu>NaN</%s%lu>", COL_DATA_TAG,j,COL_DATA_TAG,j);
787 snprintf(buf,sizeof(buf),"<%s>NaN</%s>", COL_DATA_TAG,COL_DATA_TAG);
791 snprintf(buf,sizeof(buf),"<%s%lu>%0.10e</%s%lu>", COL_DATA_TAG,j,newval,COL_DATA_TAG,j);
793 snprintf(buf,sizeof(buf),"<%s>%0.10e</%s>", COL_DATA_TAG,newval,COL_DATA_TAG);
796 addToBuffer(buffer,buf,0);
801 addToBuffer(buffer,(ti < end ? " ],\n" : " ]\n"),0);
804 snprintf(buf,sizeof(buf),"</%s>\n", DATA_ROW_TAG);
805 addToBuffer(buffer,buf,0);
810 addToBuffer(buffer," ]\n",0);
813 snprintf(buf,sizeof(buf)," </%s>\n",DATA_TAG);
814 addToBuffer(buffer,buf,0);
819 addToBuffer(buffer,"}\n",0);
821 snprintf(buf,sizeof(buf),"</%s>\n", ROOT_TAG);
822 addToBuffer(buffer,buf,0);
827 void escapeJSON(char* txt,size_t len) {
828 char *tmp=(char*)malloc(len+2);
829 size_t l=strlen(txt);
831 /* now iterate over the chars */
832 for(size_t i=0;(i<l)&&(pos<len);i++,pos++) {
846 /* and copy back over txt */
847 strncpy(txt,tmp,len);
848 /* and release tmp */
852 int rrd_xport_format_addprints(int flags,stringbuffer_t *buffer,image_desc_t *im) {
853 /* initialize buffer */
854 stringbuffer_t prints={1024,0,NULL,NULL};
855 stringbuffer_t rules={1024,0,NULL,NULL};
856 stringbuffer_t gprints={4096,0,NULL,NULL};
861 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
863 /* define some other stuff based on flags */
865 if (flags &1) { json=1; }
867 if (flags &2) { showtime=1;}
869 if (flags &4) { enumds=1;}
871 /* define some values */
872 time_t now = time(NULL);
874 localtime_r(&now, &tmvdef);
875 double printval=DNAN;
877 /* iterate over all fields and start the work writing to the correct buffers */
878 for (long i = 0; i < im->gdes_c; i++) {
879 long vidx = im->gdes[i].vidx;
881 switch (im->gdes[i].gf) {
884 /* PRINT and GPRINT can now print VDEF generated values.
885 * There's no need to do any calculations on them as these
886 * calculations were already made.
887 * we do not support the depreciated version here
890 /* decide to which buffer we print to*/
891 stringbuffer_t *usebuffer=&gprints;
892 char* usetag="gprint";
893 if (im->gdes[i].gf==GF_PRINT) {
898 if (im->gdes[vidx].gf == GF_VDEF) { /* simply use vals */
899 printval = im->gdes[vidx].vf.val;
900 localtime_r(&im->gdes[vidx].vf.when, &tmvdef);
902 int max_ii = ((im->gdes[vidx].end - im->gdes[vidx].start)
903 / im->gdes[vidx].step * im->gdes[vidx].ds_cnt);
906 for (long ii = im->gdes[vidx].ds;
907 ii < max_ii; ii += im->gdes[vidx].ds_cnt) {
908 if (!finite(im->gdes[vidx].data[ii]))
910 if (isnan(printval)) {
911 printval = im->gdes[vidx].data[ii];
916 switch (im->gdes[i].cf) {
924 printval += im->gdes[vidx].data[ii];
927 printval = min(printval, im->gdes[vidx].data[ii]);
931 printval = max(printval, im->gdes[vidx].data[ii]);
934 printval = im->gdes[vidx].data[ii];
937 if (im->gdes[i].cf == CF_AVERAGE || im->gdes[i].cf > CF_LAST) {
938 if (validsteps > 1) {
939 printval = (printval / validsteps);
943 /* we handle PRINT and GPRINT the same - format now*/
944 if (im->gdes[i].strftm) {
945 if (im->gdes[vidx].vf.never == 1) {
946 time_clean(buf, im->gdes[i].format);
948 strftime(dbuf,sizeof(dbuf), im->gdes[i].format, &tmvdef);
950 } else if (bad_format(im->gdes[i].format)) {
952 ("bad format for PRINT in \"%s'", im->gdes[i].format);
955 snprintf(dbuf,sizeof(dbuf), im->gdes[i].format, printval,"");
959 escapeJSON(dbuf,sizeof(dbuf));
960 snprintf(buf,sizeof(buf),",\n { \"%s\": \"%s\" }",usetag,dbuf);
962 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n",usetag,dbuf,usetag);
964 addToBuffer(usebuffer,buf,0);
968 strncpy(dbuf,im->gdes[i].legend,sizeof(dbuf));
969 escapeJSON(dbuf,sizeof(dbuf));
970 snprintf(buf,sizeof(buf),",\n { \"comment\": \"%s\" }",dbuf);
972 snprintf(buf,sizeof(buf)," <comment>%s</comment>\n",im->gdes[i].legend);
974 addToBuffer(&gprints,buf,0);
977 entry = im->gdes[i].legend;
978 /* I do not know why the legend is "spaced", but let us skip it */
979 while(isspace(*entry)){entry++;}
981 snprintf(buf,sizeof(buf),",\n { \"line\": \"%s\" }",entry);
983 snprintf(buf,sizeof(buf)," <line>%s</line>\n",entry);
985 addToBuffer(&gprints,buf,0);
989 snprintf(buf,sizeof(buf),",\n { \"area\": \"%s\" }",im->gdes[i].legend);
991 snprintf(buf,sizeof(buf)," <area>%s</area>\n",im->gdes[i].legend);
993 addToBuffer(&gprints,buf,0);
997 snprintf(buf,sizeof(buf),",\n { \"stack\": \"%s\" }",im->gdes[i].legend);
999 snprintf(buf,sizeof(buf)," <stack>%s</stack>\n",im->gdes[i].legend);
1001 addToBuffer(&gprints,buf,0);
1005 switch (im->gdes[i].txtalign) {
1006 case TXA_LEFT: val="left"; break;
1007 case TXA_RIGHT: val="right"; break;
1008 case TXA_CENTER: val="center"; break;
1009 case TXA_JUSTIFIED: val="justified"; break;
1012 snprintf(buf,sizeof(buf),",\n { \"align\": \"%s\" }",val);
1014 snprintf(buf,sizeof(buf)," <align>%s</align>\n",val);
1016 addToBuffer(&gprints,buf,0);
1019 /* This does not work as expected - Tobi please help!!! */
1020 snprintf(dbuf,sizeof(dbuf),"%0.10e",im->gdes[i].vf.val);
1023 snprintf(buf,sizeof(buf),",\n { \"hrule\": \"%s\" }",dbuf);
1025 snprintf(buf,sizeof(buf)," <hrule>%s</hrule>\n",dbuf);
1027 addToBuffer(&rules,buf,0);
1032 localtime_r(&im->gdes[i].xrule,&loc);
1033 strftime(dbuf,254,timefmt,&loc);
1035 snprintf(dbuf,254,"%lld",(long long int)im->gdes[i].vf.when);
1039 snprintf(buf,sizeof(buf),",\n { \"vrule\": \"%s\" }",dbuf);
1041 snprintf(buf,sizeof(buf)," <vrule>%s</vrule>\n",dbuf);
1043 addToBuffer(&rules,buf,0);
1049 /* now add prints */
1052 snprintf(buf,sizeof(buf)," \"%s\": [\n","prints");
1053 addToBuffer(buffer,buf,0);
1054 addToBuffer(buffer,(char*)prints.data+2,prints.len-2);
1055 addToBuffer(buffer,"\n ],\n",0);
1057 snprintf(buf,sizeof(buf)," <%s>\n", "prints");
1058 addToBuffer(buffer,buf,0);
1059 addToBuffer(buffer,(char*)prints.data,prints.len);
1060 snprintf(buf,sizeof(buf)," </%s>\n", "prints");
1061 addToBuffer(buffer,buf,0);
1065 /* now add gprints */
1068 snprintf(buf,sizeof(buf)," \"%s\": [\n","gprints");
1069 addToBuffer(buffer,buf,0);
1070 addToBuffer(buffer,(char*)gprints.data+2,gprints.len-2);
1071 addToBuffer(buffer,"\n ],\n",0);
1073 snprintf(buf,sizeof(buf)," <%s>\n", "gprints");
1074 addToBuffer(buffer,buf,0);
1075 addToBuffer(buffer,(char*)gprints.data,gprints.len);
1076 snprintf(buf,sizeof(buf)," </%s>\n", "gprints");
1077 addToBuffer(buffer,buf,0);
1084 snprintf(buf,sizeof(buf)," \"%s\": [\n","rules");
1085 addToBuffer(buffer,buf,0);
1086 addToBuffer(buffer,(char*)rules.data+2,rules.len-2);
1087 addToBuffer(buffer,"\n ],\n",0);
1089 snprintf(buf,sizeof(buf)," <%s>\n", "rules");
1090 addToBuffer(buffer,buf,0);
1091 addToBuffer(buffer,(char*)rules.data,rules.len);
1092 snprintf(buf,sizeof(buf)," </%s>\n", "rules");
1093 addToBuffer(buffer,buf,0);