"Fossies" - the Fresh Open Source Software Archive

Member "bonnie++-2.00a/bon_add.cpp" (23 Dec 2008, 2828 Bytes) of package /linux/privat/bonnie++-2.00a.tgz:


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 "bon_add.cpp" see the Fossies "Dox" file reference documentation.

    1 #include "bonnie.h"
    2 #include <stdio.h>
    3 #include <vector>
    4 #include <string.h>
    5 #include <math.h>
    6 
    7 // Maximum number of items expected on a csv line
    8 #define MAX_ITEMS 45
    9 typedef vector<PCCHAR> STR_VEC;
   10 
   11 vector<STR_VEC> data;
   12 typedef PCCHAR * PPCCHAR;
   13 PPCCHAR * props;
   14 
   15 // Splits a line of text (CSV format) by commas and adds it to the list to
   16 // process later.  Doesn't keep any pointers to the buf...
   17 void read_in(CPCCHAR buf);
   18 // print line in the specified line from columns start..end as a line of a
   19 // HTML table
   20 void print_a_line(int num, int start, int end);
   21 // 0 means don't do colors, 1 means speed, 2 means CPU, 3 means latency
   22 const int vals[MAX_ITEMS] =
   23   { 0,0,0,0,0,1,2,1,2,1,2,1,2,1,2,1,2,
   24     0,0,0,0,1,2,1,2,1,2,1,2,1,2,1,2,
   25     3,3,3,3,3,3,3,3,3,3,3,3 };
   26 
   27 void usage()
   28 {
   29   exit(1);
   30 }
   31 
   32 int main(int argc, char **argv)
   33 {
   34   char buf[1024];
   35 
   36   FILE *fp = NULL;
   37   if(argc > 1)
   38   {
   39     fp = fopen(argv[1], "r");
   40     if(!fp)
   41       usage();
   42   }
   43   while(fgets(buf, sizeof(buf), fp ? fp : stdin))
   44   {
   45     buf[sizeof(buf) - 1] = '\0';
   46     strtok(buf, "\r\n");
   47     read_in(buf);
   48   }
   49 
   50   printf("%s", data[0][0]);
   51   int i;
   52   for(i = 1; i < MAX_ITEMS; i++)
   53   {
   54     switch(vals[i])
   55     {
   56     case 0:
   57       printf(",%s", data[0][i]);
   58     break;
   59     case 1:
   60     case 2:
   61       {
   62         int sum = 0, tmp = 0;
   63         for(int j = 0; j < data.size(); j++)
   64         {
   65           if(sscanf(data[j][i], "%d", &tmp) != 1)
   66           {
   67             sum = 0;
   68             j = data.size();
   69           }
   70           sum += tmp;
   71         }
   72         if(sum > 0)
   73           printf(",%d", sum);
   74         else
   75           printf(",");
   76       }
   77     break;
   78     case 3:
   79       {
   80         double max = 0.0;
   81         int tmp = 0;
   82         for(int j = 0; j < data.size(); j++)
   83         {
   84           if(sscanf(data[j][i], "%d", &tmp) != 1)
   85           {
   86             max = 0.0;
   87             j = data.size();
   88           }
   89           double dtmp = double(tmp);
   90           if(strstr(data[j][i], "ms"))
   91             dtmp *= 1000.0;
   92           else if(!strstr(data[j][i], "us"))
   93             dtmp *= 1000000.0;
   94           if(dtmp > max)
   95             max = dtmp;
   96         }
   97         if(max > 99999999.0)
   98           printf(",%ds", int(max / 1000000.0));
   99         else if(max > 99999.0)
  100           printf(",%dms", int(max / 1000.0));
  101         else
  102           printf(",%dus", int(max));
  103       }
  104     break;
  105     }
  106   }
  107   printf("\n");
  108   return 0;
  109 }
  110 
  111 STR_VEC split(CPCCHAR delim, CPCCHAR buf)
  112 {
  113   STR_VEC arr;
  114   char *tmp = strdup(buf);
  115   while(1)
  116   {
  117     arr.push_back(tmp);
  118     tmp = strstr(tmp, delim);
  119     if(!tmp)
  120       break;
  121     *tmp = '\0';
  122     tmp += strlen(delim);
  123   }
  124   return arr;
  125 }
  126 
  127 void read_in(CPCCHAR buf)
  128 {
  129   STR_VEC arr = split(",", buf);
  130   if(strcmp(arr[0], "2") )
  131   {
  132     fprintf(stderr, "Can't process: %s\n", buf);
  133     free((void *)arr[0]);
  134     return;
  135   }
  136 
  137   data.push_back(arr);
  138 }
  139