Ticket #268: kilobyte-git.patch

File kilobyte-git.patch, 4.8 KB (added by bilbo, 16 years ago)

Patch to allow using SI units for byte size prefixes

  • ChangeLog

    diff --git a/ChangeLog b/ChangeLog
    index e395d88..bcc8586 100644
    a b  
     12009-02-07  Martin Petricek  <tux@centrum.cz>  
     2 
     3        * doc/mc.1.in: Added help about config file option to use SI or binary 
     4        byte size prefixes (powers of 1000 or 1024) 
     5 
  • doc/mc.1.in

    diff --git a/doc/mc.1.in b/doc/mc.1.in
    index 9e908e6..f65ba14 100644
    a b This value is the number of seconds the Midnight Commander will wait 
    31253125before attempting to reconnect to an FTP server that has denied the 
    31263126login.  If the value is zero, the login will no be retried. 
    31273127.TP 
     3128.I kilobyte_si 
     3129If this option is set (kilobyte_si=1), Midnight Commander will use SI units 
     3130(powers of 1000) when displaying any byte sizes. The suffixes (k, m ...) 
     3131are shown in lowercase. 
     3132If unset (default), Midnight Commander will use binary units (powers of 1024) 
     3133and the suffixes are shown in upper case (K, M ...) 
     3134.TP 
    31283135.I max_dirt_limit 
    31293136Specifies how many screen updates can be skipped at most in the internal 
    31303137file viewer.  Normally this value is not significant, because the code 
  • src/ChangeLog

    diff --git a/src/ChangeLog b/src/ChangeLog
    index d17dd69..1a48495 100644
    a b  
     12009-02-07  Martin Petricek  <tux@centrum.cz> 
     2 
     3        * setup.c: added option to modify size format used to display 
     4        byte sizes - in adition to current binary size prefixes, SI 
     5        (1000 based) size prefixes are now also possible. 
     6        * util.c: modified size_trunc() and size_trunc_len() to 
     7        allow using SI size prefixes. 
  • src/setup.c

    diff --git a/src/setup.c b/src/setup.c
    index eaa86a7..312b17e 100644
    a b  
    6565 
    6666extern char *find_ignore_dirs; 
    6767 
     68extern bool kilobyte_si; /* util.c */ 
     69 
    6870extern int num_history_items_recorded; 
    6971 
    7072char *profile_name;             /* .mc/ini */ 
    load_setup (void) 
    566568    load_string ("Colors", "color_terminals", "", 
    567569                             color_terminal_string, sizeof (color_terminal_string)); 
    568570 
     571    kilobyte_si = load_int ("Misc", "kilobyte_si", 0); 
    569572    /* Load the directory history */ 
    570573/*    directory_history_load (); */ 
    571574    /* Remove the temporal entries */ 
  • src/util.c

    diff --git a/src/util.c b/src/util.c
    index dc03468..d3d34bb 100644
    a b path_trunc (const char *path, size_t trunc_len) { 
    276276    return ret; 
    277277} 
    278278 
     279/* 
     280 * If true, SI units (1000 based) will be used for 
     281 * larger units (kilobyte, megabyte, ...). 
     282 * If false binary units (1024 based) will be used. 
     283 */ 
     284bool kilobyte_si = 0; 
     285 
    279286const char * 
    280287size_trunc (double size) 
    281288{ 
    size_trunc (double size) 
    284291    const char *xtra = ""; 
    285292     
    286293    if (size > 999999999L){ 
    287         divisor = 1024; 
    288         xtra = "K"; 
     294        divisor = kilobyte_si?1000:1024; 
     295        xtra = kilobyte_si?"k":"K"; 
    289296        if (size/divisor > 999999999L){ 
    290             divisor = 1024*1024; 
    291             xtra = "M"; 
     297            divisor = kilobyte_si?(1000*1000):(1024*1024); 
     298            xtra = kilobyte_si?"m":"M"; 
    292299        } 
    293300    } 
    294301    snprintf (x, sizeof (x), "%.0f%s", (size/divisor), xtra); 
    size_trunc_len (char *buffer, int len, off_t size, int units) 
    340347         1000000000}; 
    341348    static const char * const suffix [] = 
    342349        {"", "K", "M", "G", "T", "P", "E", "Z", "Y", NULL}; 
     350    static const char * const suffix_lc [] = 
     351        {"", "k", "m", "g", "t", "p", "e", "z", "y", NULL}; 
    343352    int j = 0; 
     353    int size_remain; 
    344354 
    345355    /* Don't print more than 9 digits - use suffix.  */ 
    346356    if (len == 0 || len > 9) 
    347357        len = 9; 
    348358 
     359    /* 
     360     * recalculate from 1024 base to 1000 base if units>0 
     361     * We can't just multiply by 1024 - that might cause overflow 
     362     * if off_t type is too small 
     363     */ 
     364    if (units && kilobyte_si) { 
     365     for (j = 0; j < units; j++) { 
     366      size_remain=((size % 125)*1024)/1000; /* size mod 125, recalculated */ 
     367      size = size / 125; /* 128/125 = 1024/1000 */ 
     368      size = size * 128; /* This will convert size from multiple of 1024 to multiple of 1000 */ 
     369      size += size_remain; /* Re-add remainder lost by division/multiplication */ 
     370     } 
     371    } 
     372 
    349373    for (j = units; suffix [j] != NULL; j++) { 
    350374        if (size == 0) { 
    351375            if (j == units) { 
    size_trunc_len (char *buffer, int len, off_t size, int units) 
    356380 
    357381            /* Use "~K" or just "K" if len is 1.  Use "B" for bytes.  */ 
    358382            snprintf (buffer, len + 1, (len > 1) ? "~%s" : "%s", 
    359                         (j > 1) ? suffix[j - 1] : "B"); 
     383                        (j > 1) ? (kilobyte_si ? suffix_lc[j - 1] : suffix[j - 1]) : "B"); 
    360384            break; 
    361385        } 
    362386 
    363387        if (size < power10 [len - (j > 0)]) { 
    364             snprintf (buffer, len + 1, "%lu%s", (unsigned long) size, suffix[j]); 
     388            snprintf (buffer, len + 1, "%lu%s", (unsigned long) size, kilobyte_si ? suffix_lc[j] : suffix[j]); 
    365389            break; 
    366390        } 
    367391 
    368         /* Powers of 1024, with rounding.  */ 
    369         size = (size + 512) >> 10; 
     392        /* Powers of 1000 or 1024, with rounding.  */ 
     393        if (kilobyte_si) { 
     394            size = (size + 512) >> 10; 
     395        } else { 
     396            size = (size + 500) / 1000; 
     397        } 
    370398    } 
    371399} 
    372400