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__)
50 time_t *end, /* which time frame do you want ?
51 * will be changed to represent reality */
52 unsigned long *step, /* which stepsize do you want?
53 * will be changed to represent reality */
54 unsigned long *col_cnt, /* number of data columns in the result */
55 char ***legend_v, /* legend entries */
57 { /* two dimensional array containing the data */
59 time_t start_tmp = 0, end_tmp = 0;
60 rrd_time_value_t start_tv, end_tv;
61 char *parsetime_error = NULL;
63 struct option long_options[] = {
64 {"start", required_argument, 0, 's'},
65 {"end", required_argument, 0, 'e'},
66 {"maxrows", required_argument, 0, 'm'},
67 {"step", required_argument, 0, 261},
68 {"enumds", no_argument, 0, 262}, /* these are handled in the frontend ... */
69 {"json", no_argument, 0, 263}, /* these are handled in the frontend ... */
70 {"daemon", required_argument, 0, 'd'},
75 opterr = 0; /* initialize getopt */
79 rrd_parsetime("end-24h", &start_tv);
80 rrd_parsetime("now", &end_tv);
86 opt = getopt_long(argc, argv, "s:e:m:d:", long_options, &option_index);
93 im.step = atoi(optarg);
98 if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) {
99 rrd_set_error("start time: %s", parsetime_error);
104 if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) {
105 rrd_set_error("end time: %s", parsetime_error);
110 im.xsize = atol(optarg);
112 rrd_set_error("maxrows below 10 rows");
118 if (im.daemon_addr != NULL)
120 rrd_set_error ("You cannot specify --daemon "
125 im.daemon_addr = strdup(optarg);
126 if (im.daemon_addr == NULL)
128 rrd_set_error("strdup error");
135 rrd_set_error("unknown option '%s'", argv[optind - 1]);
140 if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
144 if (start_tmp < 3600 * 24 * 365 * 10) {
145 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",
150 if (end_tmp < start_tmp) {
151 rrd_set_error("start (%ld) should be less than end (%ld)",
156 im.start = start_tmp;
158 im.step = max((long) im.step, (im.end - im.start) / im.xsize);
160 rrd_graph_script(argc, argv, &im, 0);
161 if (rrd_test_error()) {
166 if (im.gdes_c == 0) {
167 rrd_set_error("can't make an xport without contents");
172 { /* try to connect to rrdcached */
173 int status = rrdc_connect(im.daemon_addr);
174 if (status != 0) return status;
177 if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data,0) == -1) {
191 time_t *end, /* which time frame do you want ?
192 * will be changed to represent reality */
193 unsigned long *step, /* which stepsize do you want?
194 * will be changed to represent reality */
195 unsigned long *col_cnt, /* number of data columns in the result */
196 char ***legend_v, /* legend entries */
199 { /* two dimensional array containing the data */
202 unsigned long dst_row, row_cnt;
205 unsigned long xport_counter = 0;
212 /* pull the data from the rrd files ... */
213 if (data_fetch(im) == -1)
216 /* evaluate CDEF operations ... */
217 if (data_calc(im) == -1)
220 /* how many xports or lines/AREA/STACK ? */
222 for (i = 0; i < im->gdes_c; i++) {
223 switch (im->gdes[i].gf) {
236 if ((*col_cnt) == 0) {
237 rrd_set_error("no XPORT found, nothing to do");
241 /* a list of referenced gdes */
242 ref_list = (int*)malloc(sizeof(int) * (*col_cnt));
243 if (ref_list == NULL)
246 /* a list to save pointers to the column's legend entry */
247 /* this is a return value! */
248 legend_list = (char**)malloc(sizeof(char *) * (*col_cnt));
249 if (legend_list == NULL) {
254 /* lets find the step size we have to use for xport */
255 step_list = (long*)malloc(sizeof(long)*((*col_cnt)+1));
256 step_list_ptr = step_list;
258 for (i = 0; i < im->gdes_c; i++) {
259 /* decide if we need to handle the output */
261 switch (im->gdes[i].gf) {
274 /* and now do the real work */
276 ref_list[xport_counter++] = i;
277 *step_list_ptr = im->gdes[im->gdes[i].vidx].step;
278 /* printf("%s:%lu\n",im->gdes[i].legend,*step_list_ptr); */
280 /* reserve room for one legend entry */
281 /* is FMT_LEG_LEN + 5 the correct size? */
282 if ((legend_list[j] =
283 (char*)malloc(sizeof(char) * (FMT_LEG_LEN + 5))) == NULL) {
287 free(legend_list[j]);
290 rrd_set_error("malloc xport legend entry");
294 if (im->gdes[i].legend)
295 /* omit bounds check, should have the same size */
296 strcpy(legend_list[j++], im->gdes[i].legend);
298 legend_list[j++][0] = '\0';
302 /* find a common step */
303 *step = lcd(step_list);
304 /* printf("step: %lu\n",*step); */
307 *start = im->start - im->start % (*step);
308 *end = im->end - im->end % (*step) + (*step);
311 /* room for rearranged data */
312 /* this is a return value! */
313 row_cnt = ((*end) - (*start)) / (*step);
315 (rrd_value_t*)malloc((*col_cnt) * row_cnt * sizeof(rrd_value_t))) == NULL) {
318 rrd_set_error("malloc xport data area");
323 /* fill data structure */
324 for (dst_row = 0; (int) dst_row < (int) row_cnt; dst_row++) {
325 for (i = 0; i < (int) (*col_cnt); i++) {
326 long vidx = im->gdes[ref_list[i]].vidx;
327 time_t now = *start + dst_row * *step;
328 (*dstptr++) = im->gdes[vidx].data[(unsigned long)
330 (now - im->gdes[vidx].start)
331 /im->gdes[vidx].step)
332 * im->gdes[vidx].ds_cnt +
338 *legend_v = legend_list;
344 /* helper function for buffer handling */
345 typedef struct stringbuffer_t {
351 int addToBuffer(stringbuffer_t *,char*,size_t);
352 void escapeJSON(char*,size_t);
354 int rrd_graph_xport(image_desc_t *);
355 int rrd_xport_format_xmljson(int,stringbuffer_t *,image_desc_t*,time_t, time_t, unsigned long, unsigned long, char**, rrd_value_t*);
356 int rrd_xport_format_sv(char,stringbuffer_t *,image_desc_t*,time_t, time_t, unsigned long, unsigned long, char**, rrd_value_t*);
357 int rrd_xport_format_addprints(int,stringbuffer_t *,image_desc_t *);
359 int rrd_graph_xport(image_desc_t *im) {
360 /* prepare the data for processing */
361 unsigned long col_cnt=0;
362 time_t start=im->start;
364 unsigned long step=im->step;
365 char **legend_v=NULL;
366 rrd_value_t *data=NULL;
367 /* initialize buffer */
368 stringbuffer_t buffer={0,0,NULL,NULL};
369 /* if we write a file, then open it */
370 if (strlen(im->graphfile)) {
371 buffer.file=fopen(im->graphfile,"w");
374 /* do the data processing */
375 if (rrd_xport_fn(im,&start,&end,&step,&col_cnt,&legend_v,&data,1)) { return -1;}
377 /* fill in some data */
380 grinfo_push(im, sprintf_alloc("graph_start"), RD_I_CNT, info);
382 grinfo_push(im, sprintf_alloc("graph_end"), RD_I_CNT, info);
384 grinfo_push(im, sprintf_alloc("graph_step"), RD_I_CNT, info);
387 char *old_locale = setlocale(LC_NUMERIC,NULL);
388 setlocale(LC_NUMERIC, "C");
390 /* format it for output */
392 switch(im->imgformat) {
394 r=rrd_xport_format_xmljson(2,&buffer,im, start, end, step, col_cnt, legend_v, data);
397 r=rrd_xport_format_xmljson(6,&buffer,im, start, end, step, col_cnt, legend_v, data);
400 r=rrd_xport_format_xmljson(1,&buffer,im, start, end, step, col_cnt, legend_v, data);
403 r=rrd_xport_format_xmljson(3,&buffer,im, start, end, step, col_cnt, legend_v, data);
406 r=rrd_xport_format_sv(',',&buffer,im, start, end, step, col_cnt, legend_v, data);
409 r=rrd_xport_format_sv('\t',&buffer,im, start, end, step, col_cnt, legend_v, data);
412 r=rrd_xport_format_sv(';',&buffer,im, start, end, step, col_cnt, legend_v, data);
418 setlocale(LC_NUMERIC, old_locale);
422 for (unsigned long j = 0; j < col_cnt; j++) {
429 if (buffer.data) {free(buffer.data);}
431 if (buffer.file) {fclose(buffer.file);}
432 /* and return with error */
436 /* now do the cleanup */
438 fclose(buffer.file); buffer.file=NULL;
439 im->rendered_image_size=0;
440 im->rendered_image=NULL;
442 im->rendered_image_size=buffer.len;
443 im->rendered_image=buffer.data;
446 /* and print stuff */
447 return print_calc(im);
450 int addToBuffer(stringbuffer_t * sb,char* data,size_t len) {
451 /* if len <= 0 we assume a string and calculate the length ourself */
452 if (len<=0) { len=strlen(data); }
453 /* if we have got a file, then take the shortcut */
456 fwrite(data,len,1,sb->file);
459 /* if buffer is 0, then initialize */
461 /* make buffer a multiple of 8192 */
463 sb->allocated-=(sb->allocated%8192);
464 /* and allocate it */
465 sb->data=malloc(sb->allocated);
467 rrd_set_error("malloc issue");
470 /* and initialize the buffer */
474 /* and figure out if we need to extend the buffer */
475 if (sb->len+len+1>=sb->allocated) {
476 /* add so many pages until we have a buffer big enough */
477 while(sb->len+len+1>=sb->allocated) {
480 /* try to resize it */
481 unsigned char* resized=(unsigned char*)realloc(sb->data,sb->allocated);
488 rrd_set_error("realloc issue");
492 /* and finally add to the buffer */
493 memcpy(sb->data+sb->len,data,len);
495 /* and 0 terminate it */
501 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) {
502 /* define the time format */
504 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
507 unsigned long row_cnt=(end-start)/step;
509 /* estimate buffer size (to avoid multiple allocations) */
511 1024 /* bytes of overhead /header/footer */
512 +(12+19*col_cnt) /* 12 bytes overhead/line plus 19 bytes per column*/
513 *(1+row_cnt) /* number of columns + 1 (for header) */
518 /* now start writing the header*/
519 if (addToBuffer(buffer,"\"time\"",6)) { return 1; }
520 for(unsigned long i=0;i<col_cnt;i++) {
521 /* strip leading spaces */
522 char *t=legend_v[i]; while (isspace(*t)) { t++;}
524 snprintf(buf,255,"%c\"%s\"",sep,t);
525 if (addToBuffer(buffer,buf,0)) { return 1;}
527 if (addToBuffer(buffer,"\r\n",2)) { return 1; }
528 /* and now write the data */
529 rrd_value_t *ptr=data;
530 for(time_t ti=start+step;ti<end;ti+=step) {
534 localtime_r(&ti,&loc);
535 strftime(buf,254,timefmt,&loc);
537 snprintf(buf,254,"%lld",(long long int)ti);
539 if (addToBuffer(buffer,buf,0)) { return 1; }
540 /* write the columns */
541 for(unsigned long i=0;i<col_cnt;i++) {
543 rrd_value_t v=*ptr;ptr++;
546 snprintf(buf,255,"%c\"NaN\"",sep);
548 snprintf(buf,255,"%c\"%0.10e\"",sep,v);
550 if (addToBuffer(buffer,buf,0)) { return 1;}
552 /* and add a newline */
553 if (addToBuffer(buffer,"\r\n",2)) { return 1; }
560 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) {
562 /* define some other stuff based on flags */
564 if (flags &1) { json=1; }
566 if (flags &2) { showtime=1;}
568 if (flags &4) { enumds=1;}
570 /* define the time format */
572 /* unfortunatley we have to do it this way,
573 as when no --x-graph argument is given,
574 then the xlab_user is not in a clean state (e.g. zero-filled) */
575 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
578 unsigned long row_cnt=(end-start)/step;
580 /* estimate buffer size (to avoid multiple allocations) */
581 /* better estimates are needed here */
583 1024 /* bytes of overhead /header/footer */
584 +(12+19*col_cnt) /* 12 bytes overhead/line plus 19 bytes per column*/
585 *(1+row_cnt) /* number of columns + 1 (for header) */
590 rrd_value_t *ptr = data;
592 snprintf(buf,sizeof(buf),
593 "<?xml version=\"1.0\" encoding=\"%s\"?>\n\n<%s>\n <%s>\n",
594 XML_ENCODING,ROOT_TAG,META_TAG);
595 addToBuffer(buffer,buf,0);
598 addToBuffer(buffer,"{ \"about\": \"RRDtool graph JSON output\",\n \"meta\": {\n",0);
601 /* calculate start time */
604 time_t ti=start+step;
605 localtime_r(&ti,&loc);
606 strftime(dbuf,sizeof(dbuf),timefmt,&loc);
608 snprintf(buf,sizeof(buf)," \"%s\": \"%s\",\n",META_START_TAG,dbuf);
610 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n",META_START_TAG,dbuf,META_START_TAG);
614 snprintf(buf,sizeof(buf)," \"%s\": %lld,\n",META_START_TAG,(long long int)start+step);
616 snprintf(buf,sizeof(buf)," <%s>%lld</%s>\n",META_START_TAG,(long long int)start+step,META_START_TAG);
619 addToBuffer(buffer,buf,0);
621 /* calculate end time */
625 localtime_r(&ti,&loc);
626 strftime(dbuf,sizeof(dbuf),timefmt,&loc);
628 snprintf(buf,sizeof(buf)," \"%s\": \"%s\",\n",META_END_TAG,dbuf);
630 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n",META_END_TAG,dbuf,META_END_TAG);
634 snprintf(buf,sizeof(buf)," \"%s\": %lld,\n",META_END_TAG,(long long int)end);
636 snprintf(buf,sizeof(buf)," <%s>%lld</%s>\n",META_END_TAG,(long long int)end,META_END_TAG);
639 addToBuffer(buffer,buf,0);
640 /* print other info */
642 snprintf(buf,sizeof(buf)," \"%s\": %lld,\n",META_STEP_TAG,(long long int)step);
643 addToBuffer(buffer,buf,0);
645 snprintf(buf,sizeof(buf)," <%s>%lld</%s>\n",META_STEP_TAG,(long long int)step,META_STEP_TAG);
646 addToBuffer(buffer,buf,0);
647 snprintf(buf,sizeof(buf)," <%s>%lu</%s>\n",META_ROWS_TAG,row_cnt,META_ROWS_TAG);
648 addToBuffer(buffer,buf,0);
649 snprintf(buf,sizeof(buf)," <%s>%lu</%s>\n",META_COLS_TAG,col_cnt,META_COLS_TAG);
650 addToBuffer(buffer,buf,0);
655 snprintf(buf,sizeof(buf)," \"%s\": [\n", LEGEND_TAG);
658 snprintf(buf,sizeof(buf)," <%s>\n", LEGEND_TAG);
660 addToBuffer(buffer,buf,0);
661 /* add legend entries */
662 for (unsigned long j = 0; j < col_cnt; j++) {
663 char *entry = legend_v[j];
664 /* I do not know why the legend is "spaced", but let us skip it */
665 while(isspace(*entry)){entry++;}
668 snprintf(buf,sizeof(buf)," \"%s\"", entry);
669 addToBuffer(buffer,buf,0);
671 addToBuffer(buffer,",",1);
673 addToBuffer(buffer,"\n",1);
676 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n", LEGEND_ENTRY_TAG, entry,LEGEND_ENTRY_TAG);
677 addToBuffer(buffer,buf,0);
682 snprintf(buf,sizeof(buf)," ],\n");
685 snprintf(buf,sizeof(buf)," </%s>\n", LEGEND_TAG);
687 addToBuffer(buffer,buf,0);
689 /* add graphs and prints */
690 if (rrd_xport_format_addprints(json,buffer,im)) {return -1;}
692 /* if we have got a trailing , then kill it */
693 if (buffer->data[buffer->len-2]==',') {
694 buffer->data[buffer->len-2]=buffer->data[buffer->len-1];
700 snprintf(buf,sizeof(buf)," },\n");
702 snprintf(buf,sizeof(buf)," </%s>\n", META_TAG);
704 addToBuffer(buffer,buf,0);
709 snprintf(buf,sizeof(buf)," \"%s\": [\n",DATA_TAG);
711 snprintf(buf,sizeof(buf)," <%s>\n", DATA_TAG);
713 addToBuffer(buffer,buf,0);
714 /* iterate over data */
715 for (time_t ti = start + step; ti <= end; ti += step) {
718 localtime_r(&ti,&loc);
719 strftime(dbuf,sizeof(dbuf),timefmt,&loc);
721 snprintf(dbuf,sizeof(dbuf),"%lld",(long long int)ti);
724 addToBuffer(buffer," [ ",0);
726 addToBuffer(buffer,"\"",1);
727 addToBuffer(buffer,dbuf,0);
728 addToBuffer(buffer,"\",",2);
733 snprintf(buf,sizeof(buf),
734 " <%s><%s>%s</%s>", DATA_ROW_TAG,COL_TIME_TAG, dbuf, COL_TIME_TAG);
736 snprintf(buf,sizeof(buf),
737 " <%s>", DATA_ROW_TAG);
739 addToBuffer(buffer,buf,0);
741 for (unsigned long j = 0; j < col_cnt; j++) {
742 rrd_value_t newval = DNAN;
746 addToBuffer(buffer,"null",0);
748 snprintf(buf,sizeof(buf),"%0.10e",newval);
749 addToBuffer(buffer,buf,0);
752 addToBuffer(buffer,", ",0);
758 snprintf(buf,sizeof(buf),"<%s%lu>NaN</%s%lu>", COL_DATA_TAG,j,COL_DATA_TAG,j);
760 snprintf(buf,sizeof(buf),"<%s>NaN</%s>", COL_DATA_TAG,COL_DATA_TAG);
764 snprintf(buf,sizeof(buf),"<%s%lu>%0.10e</%s%lu>", COL_DATA_TAG,j,newval,COL_DATA_TAG,j);
766 snprintf(buf,sizeof(buf),"<%s>%0.10e</%s>", COL_DATA_TAG,newval,COL_DATA_TAG);
769 addToBuffer(buffer,buf,0);
774 addToBuffer(buffer,(ti < end ? " ],\n" : " ]\n"),0);
777 snprintf(buf,sizeof(buf),"</%s>\n", DATA_ROW_TAG);
778 addToBuffer(buffer,buf,0);
783 addToBuffer(buffer," ]\n",0);
786 snprintf(buf,sizeof(buf)," </%s>\n",DATA_TAG);
787 addToBuffer(buffer,buf,0);
792 addToBuffer(buffer,"}\n",0);
794 snprintf(buf,sizeof(buf),"</%s>\n", ROOT_TAG);
795 addToBuffer(buffer,buf,0);
800 void escapeJSON(char* txt,size_t len) {
801 char *tmp=(char*)malloc(len+2);
802 size_t l=strlen(txt);
804 /* now iterate over the chars */
805 for(size_t i=0;(i<l)&&(pos<len);i++,pos++) {
819 /* and copy back over txt */
820 strncpy(txt,tmp,len);
821 /* and release tmp */
825 int rrd_xport_format_addprints(int flags,stringbuffer_t *buffer,image_desc_t *im) {
826 /* initialize buffer */
827 stringbuffer_t prints={1024,0,NULL,NULL};
828 stringbuffer_t rules={1024,0,NULL,NULL};
829 stringbuffer_t gprints={4096,0,NULL,NULL};
834 if (im->xlab_user.minsec!=-1) { timefmt=im->xlab_user.stst; }
836 /* define some other stuff based on flags */
838 if (flags &1) { json=1; }
840 if (flags &2) { showtime=1;}
842 if (flags &4) { enumds=1;}
844 /* define some values */
845 time_t now = time(NULL);
847 localtime_r(&now, &tmvdef);
848 double printval=DNAN;
850 /* iterate over all fields and start the work writing to the correct buffers */
851 for (long i = 0; i < im->gdes_c; i++) {
852 long vidx = im->gdes[i].vidx;
854 switch (im->gdes[i].gf) {
857 /* PRINT and GPRINT can now print VDEF generated values.
858 * There's no need to do any calculations on them as these
859 * calculations were already made.
860 * we do not support the depreciated version here
863 /* decide to which buffer we print to*/
864 stringbuffer_t *usebuffer=&gprints;
865 char* usetag="gprint";
866 if (im->gdes[i].gf==GF_PRINT) {
871 if (im->gdes[vidx].gf == GF_VDEF) { /* simply use vals */
872 printval = im->gdes[vidx].vf.val;
873 localtime_r(&im->gdes[vidx].vf.when, &tmvdef);
875 int max_ii = ((im->gdes[vidx].end - im->gdes[vidx].start)
876 / im->gdes[vidx].step * im->gdes[vidx].ds_cnt);
879 for (long ii = im->gdes[vidx].ds;
880 ii < max_ii; ii += im->gdes[vidx].ds_cnt) {
881 if (!finite(im->gdes[vidx].data[ii]))
883 if (isnan(printval)) {
884 printval = im->gdes[vidx].data[ii];
889 switch (im->gdes[i].cf) {
897 printval += im->gdes[vidx].data[ii];
900 printval = min(printval, im->gdes[vidx].data[ii]);
904 printval = max(printval, im->gdes[vidx].data[ii]);
907 printval = im->gdes[vidx].data[ii];
910 if (im->gdes[i].cf == CF_AVERAGE || im->gdes[i].cf > CF_LAST) {
911 if (validsteps > 1) {
912 printval = (printval / validsteps);
916 /* we handle PRINT and GPRINT the same - format now*/
917 if (im->gdes[i].strftm) {
918 if (im->gdes[vidx].vf.never == 1) {
919 time_clean(buf, im->gdes[i].format);
921 strftime(dbuf,sizeof(dbuf), im->gdes[i].format, &tmvdef);
923 } else if (bad_format(im->gdes[i].format)) {
925 ("bad format for PRINT in \"%s'", im->gdes[i].format);
928 snprintf(dbuf,sizeof(dbuf), im->gdes[i].format, printval,"");
932 escapeJSON(dbuf,sizeof(dbuf));
933 snprintf(buf,sizeof(buf),",\n { \"%s\": \"%s\" }",usetag,dbuf);
935 snprintf(buf,sizeof(buf)," <%s>%s</%s>\n",usetag,dbuf,usetag);
937 addToBuffer(usebuffer,buf,0);
941 strncpy(dbuf,im->gdes[i].legend,sizeof(dbuf));
942 escapeJSON(dbuf,sizeof(dbuf));
943 snprintf(buf,sizeof(buf),",\n { \"comment\": \"%s\" }",dbuf);
945 snprintf(buf,sizeof(buf)," <comment>%s</comment>\n",im->gdes[i].legend);
947 addToBuffer(&gprints,buf,0);
950 entry = im->gdes[i].legend;
951 /* I do not know why the legend is "spaced", but let us skip it */
952 while(isspace(*entry)){entry++;}
954 snprintf(buf,sizeof(buf),",\n { \"line\": \"%s\" }",entry);
956 snprintf(buf,sizeof(buf)," <line>%s</line>\n",entry);
958 addToBuffer(&gprints,buf,0);
962 snprintf(buf,sizeof(buf),",\n { \"area\": \"%s\" }",im->gdes[i].legend);
964 snprintf(buf,sizeof(buf)," <area>%s</area>\n",im->gdes[i].legend);
966 addToBuffer(&gprints,buf,0);
970 snprintf(buf,sizeof(buf),",\n { \"stack\": \"%s\" }",im->gdes[i].legend);
972 snprintf(buf,sizeof(buf)," <stack>%s</stack>\n",im->gdes[i].legend);
974 addToBuffer(&gprints,buf,0);
978 switch (im->gdes[i].txtalign) {
979 case TXA_LEFT: val="left"; break;
980 case TXA_RIGHT: val="right"; break;
981 case TXA_CENTER: val="center"; break;
982 case TXA_JUSTIFIED: val="justified"; break;
985 snprintf(buf,sizeof(buf),",\n { \"align\": \"%s\" }",val);
987 snprintf(buf,sizeof(buf)," <align>%s</align>\n",val);
989 addToBuffer(&gprints,buf,0);
992 /* This does not work as expected - Tobi please help!!! */
993 snprintf(dbuf,sizeof(dbuf),"%0.10e",im->gdes[i].vf.val);
996 snprintf(buf,sizeof(buf),",\n { \"hrule\": \"%s\" }",dbuf);
998 snprintf(buf,sizeof(buf)," <hrule>%s</hrule>\n",dbuf);
1000 addToBuffer(&rules,buf,0);
1005 localtime_r(&im->gdes[i].xrule,&loc);
1006 strftime(dbuf,254,timefmt,&loc);
1008 snprintf(dbuf,254,"%lld",(long long int)im->gdes[i].vf.when);
1012 snprintf(buf,sizeof(buf),",\n { \"vrule\": \"%s\" }",dbuf);
1014 snprintf(buf,sizeof(buf)," <vrule>%s</vrule>\n",dbuf);
1016 addToBuffer(&rules,buf,0);
1022 /* now add prints */
1025 snprintf(buf,sizeof(buf)," \"%s\": [\n","prints");
1026 addToBuffer(buffer,buf,0);
1027 addToBuffer(buffer,(char*)prints.data+2,prints.len-2);
1028 addToBuffer(buffer,"\n ],\n",0);
1030 snprintf(buf,sizeof(buf)," <%s>\n", "prints");
1031 addToBuffer(buffer,buf,0);
1032 addToBuffer(buffer,(char*)prints.data,prints.len);
1033 snprintf(buf,sizeof(buf)," </%s>\n", "prints");
1034 addToBuffer(buffer,buf,0);
1038 /* now add gprints */
1041 snprintf(buf,sizeof(buf)," \"%s\": [\n","gprints");
1042 addToBuffer(buffer,buf,0);
1043 addToBuffer(buffer,(char*)gprints.data+2,gprints.len-2);
1044 addToBuffer(buffer,"\n ],\n",0);
1046 snprintf(buf,sizeof(buf)," <%s>\n", "gprints");
1047 addToBuffer(buffer,buf,0);
1048 addToBuffer(buffer,(char*)gprints.data,gprints.len);
1049 snprintf(buf,sizeof(buf)," </%s>\n", "gprints");
1050 addToBuffer(buffer,buf,0);
1057 snprintf(buf,sizeof(buf)," \"%s\": [\n","rules");
1058 addToBuffer(buffer,buf,0);
1059 addToBuffer(buffer,(char*)rules.data+2,rules.len-2);
1060 addToBuffer(buffer,"\n ],\n",0);
1062 snprintf(buf,sizeof(buf)," <%s>\n", "rules");
1063 addToBuffer(buffer,buf,0);
1064 addToBuffer(buffer,(char*)rules.data,rules.len);
1065 snprintf(buf,sizeof(buf)," </%s>\n", "rules");
1066 addToBuffer(buffer,buf,0);