"Fossies" - the Fresh Open Source Software Archive

Member "vnstat-2.9/src/dbxml.c" (15 May 2019, 3335 Bytes) of package /linux/misc/vnstat-2.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "dbxml.c" see the Fossies "Dox" file reference documentation.

    1 #include "common.h"
    2 #include "dbsql.h"
    3 #include "dbxml.h"
    4 
    5 void showxml(const char *interface, const char mode, const char *databegin, const char *dataend)
    6 {
    7     interfaceinfo info;
    8 
    9     timeused_debug(__func__, 1);
   10 
   11     if (!db_getinterfacecountbyname(interface)) {
   12         return;
   13     }
   14 
   15     if (!db_getinterfaceinfo(interface, &info)) {
   16         return;
   17     }
   18 
   19     printf(" <interface name=\"%s\">\n", info.name);
   20 
   21     printf("  <name>%s</name>\n", info.name);
   22     printf("  <alias>%s</alias>\n", info.alias);
   23 
   24     printf("  <created>");
   25     xmldate(&info.created, 1);
   26     printf("</created>\n");
   27     printf("  <updated>");
   28     xmldate(&info.updated, 2);
   29     printf("</updated>\n");
   30 
   31     printf("  <traffic>\n");
   32     printf("   <total><rx>%" PRIu64 "</rx><tx>%" PRIu64 "</tx></total>\n", info.rxtotal, info.txtotal);
   33 
   34     switch (mode) {
   35         case 'd':
   36             xmldump(&info, "day", 1, databegin, dataend);
   37             break;
   38         case 'm':
   39             xmldump(&info, "month", 3, databegin, dataend);
   40             break;
   41         case 't':
   42             xmldump(&info, "top", 1, databegin, dataend);
   43             break;
   44         case 'h':
   45             xmldump(&info, "hour", 2, databegin, dataend);
   46             break;
   47         case 'y':
   48             xmldump(&info, "year", 4, databegin, dataend);
   49             break;
   50         case 'f':
   51             xmldump(&info, "fiveminute", 2, databegin, dataend);
   52             break;
   53         case 'a':
   54         default:
   55             xmldump(&info, "fiveminute", 2, databegin, dataend);
   56             xmldump(&info, "hour", 2, databegin, dataend);
   57             xmldump(&info, "day", 1, databegin, dataend);
   58             xmldump(&info, "month", 3, databegin, dataend);
   59             xmldump(&info, "year", 2, databegin, dataend);
   60             xmldump(&info, "top", 1, databegin, dataend);
   61             break;
   62     }
   63 
   64     printf("  </traffic>\n");
   65     printf(" </interface>\n");
   66 
   67     timeused_debug(__func__, 0);
   68 }
   69 
   70 void xmldump(const interfaceinfo *interface, const char *tablename, const int datetype, const char *databegin, const char *dataend)
   71 {
   72     dbdatalist *datalist = NULL, *datalist_i = NULL;
   73     dbdatalistinfo datainfo;
   74 
   75     if (!db_getdata_range(&datalist, &datainfo, interface->name, tablename, (uint32_t)cfg.listjsonxml, databegin, dataend)) {
   76         printf("Error: Failed to fetch %s data.\n", tablename);
   77         return;
   78     }
   79 
   80     printf("   <%ss>\n", tablename);
   81     datalist_i = datalist;
   82     while (datalist_i != NULL) {
   83         printf("    <%s id=\"%" PRId64 "\">", tablename, datalist_i->rowid);
   84         xmldate(&datalist_i->timestamp, datetype);
   85         printf("<rx>%" PRIu64 "</rx><tx>%" PRIu64 "</tx></%s>\n", datalist_i->rx, datalist_i->tx, tablename);
   86         datalist_i = datalist_i->next;
   87     }
   88     dbdatalistfree(&datalist);
   89     printf("   </%ss>\n", tablename);
   90 }
   91 
   92 void xmldate(const time_t *date, const int type)
   93 {
   94     struct tm *d;
   95 
   96     d = localtime(date);
   97 
   98     switch (type) {
   99         case 1:
  100             printf("<date><year>%d</year><month>%02d</month><day>%02d</day></date>",
  101                    1900 + d->tm_year, 1 + d->tm_mon, d->tm_mday);
  102             break;
  103         case 2:
  104             printf("<date><year>%d</year><month>%02d</month><day>%02d</day></date><time><hour>%02d</hour><minute>%02d</minute></time>",
  105                    1900 + d->tm_year, 1 + d->tm_mon, d->tm_mday, d->tm_hour, d->tm_min);
  106             break;
  107         case 3:
  108             printf("<date><year>%d</year><month>%02d</month></date>",
  109                    1900 + d->tm_year, 1 + d->tm_mon);
  110             break;
  111         case 4:
  112             printf("<date><year>%d</year></date>",
  113                    1900 + d->tm_year);
  114             break;
  115         default:
  116             break;
  117     }
  118 }
  119 
  120 void xmlheader(void)
  121 {
  122     printf("<vnstat version=\"%s\" xmlversion=\"%d\">\n", getversion(), XMLVERSION);
  123 }
  124 
  125 void xmlfooter(void)
  126 {
  127     printf("</vnstat>\n");
  128 }