Merge branch 'ci' of https://github.com/sitsofe/fio
[fio.git] / tickmarks.c
index 21f77da0f7d8b61593b1ea45946a5f11cf95ac31..808de676e758120ca6531f9d1328871ef4d9b402 100644 (file)
@@ -3,7 +3,7 @@
 #include <malloc.h>
 #include <string.h>
 
-/* 
+/*
  * adapted from Paul Heckbert's algorithm on p 657-659 of
  * Andrew S. Glassner's book, "Graphics Gems"
  * ISBN 0-12-286166-3
@@ -39,24 +39,56 @@ static double nicenum(double x, int round)
        return 10.0 * pow(10.0, exp);
 }
 
-static void shorten(char *str)
+static void shorten(struct tickmark *tm, int nticks, int *power_of_ten,
+                       int use_KMG_symbols, int base_offset)
 {
-       int l;
-
-       l = strlen(str);
-       if (l > 9 && strcmp(&str[l - 9], "000000000") == 0) {
-               str[l - 9] = 'G';
-               str[l - 8] = '\0';
-       } else if (l > 6 && strcmp(&str[l - 6], "000000") == 0) {
-               str[l - 6] = 'M';
-               str[l - 5] = '\0';
-       } else if (l > 3 && strcmp(&str[l - 3], "000") == 0) {
-               str[l - 3] = 'K';
-               str[l - 2] = '\0';
+       const char shorten_chr[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
+       int i, l, minshorten, shorten_idx = 0;
+       char *str;
+
+       minshorten = 100;
+       for (i = 0; i < nticks; i++) {
+               str = tm[i].string;
+               l = strlen(str);
+
+               if (strcmp(str, "0") == 0)
+                       continue;
+               if (l > 9 && strcmp(&str[l - 9], "000000000") == 0) {
+                       *power_of_ten = 9;
+                       shorten_idx = 3;
+               } else if (6 < minshorten && l > 6 &&
+                               strcmp(&str[l - 6], "000000") == 0) {
+                       *power_of_ten = 6;
+                       shorten_idx = 2;
+               } else if (l > 3 && strcmp(&str[l - 3], "000") == 0) {
+                       *power_of_ten = 3;
+                       shorten_idx = 1;
+               } else {
+                       *power_of_ten = 0;
+               }
+
+               if (*power_of_ten < minshorten)
+                       minshorten = *power_of_ten;
+       }
+
+       if (minshorten == 0)
+               return;
+       if (!use_KMG_symbols)
+               shorten_idx = 0;
+       else if (base_offset)
+               shorten_idx += base_offset;
+
+       for (i = 0; i < nticks; i++) {
+               str = tm[i].string;
+               l = strlen(str);
+               str[l - minshorten] = shorten_chr[shorten_idx];
+               if (shorten_idx)
+                       str[l - minshorten + 1] = '\0';
        }
 }
 
-int calc_tickmarks(double min, double max, int nticks, struct tickmark **tm)
+int calc_tickmarks(double min, double max, int nticks, struct tickmark **tm,
+               int *power_of_ten, int use_KMG_symbols, int base_offset)
 {
        char str[100];
        int nfrac;
@@ -80,9 +112,9 @@ int calc_tickmarks(double min, double max, int nticks, struct tickmark **tm)
        for (x = graphmin; x < graphmax + 0.5 * d; x += d) {
                (*tm)[i].value = x;
                sprintf((*tm)[i].string, str, x);
-               shorten((*tm)[i].string);
                i++;
        }
+       shorten(*tm, i, power_of_ten, use_KMG_symbols, base_offset);
        return i;
 }
 
@@ -96,20 +128,20 @@ static void test_range(double x, double y)
        printf("Testing range %g - %g\n", x, y);
        nticks = calc_tickmarks(x, y, 10, &tm);
 
-       for (i = 0; i < nticks; i++) {
+       for (i = 0; i < nticks; i++)
                printf("   (%s) %g\n", tm[i].string, tm[i].value);
-       }
+
        printf("\n\n");
        free(tm);
 }
 
 int main(int argc, char *argv[])
 {
-       test_range(0.0005, 0.008);      
-       test_range(0.5, 0.8);   
-       test_range(5.5, 8.8);   
-       test_range(50.5, 80.8); 
-       test_range(-20, 20.8);  
-       test_range(-30, 700.8); 
+       test_range(0.0005, 0.008);
+       test_range(0.5, 0.8);
+       test_range(5.5, 8.8);
+       test_range(50.5, 80.8);
+       test_range(-20, 20.8);
+       test_range(-30, 700.8);
 }
 #endif