summaryrefslogtreecommitdiffstats
path: root/main_common.cc
diff options
context:
space:
mode:
Diffstat (limited to 'main_common.cc')
-rw-r--r--main_common.cc36
1 files changed, 19 insertions, 17 deletions
diff --git a/main_common.cc b/main_common.cc
index 1e592c6..dfaccac 100644
--- a/main_common.cc
+++ b/main_common.cc
@@ -1,5 +1,5 @@
/* Lziprecover - Data recovery tool for the lzip format
- Copyright (C) 2009-2023 Antonio Diaz Diaz.
+ Copyright (C) 2009-2024 Antonio Diaz Diaz.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -17,7 +17,7 @@
namespace {
-const char * const program_year = "2023";
+const char * const program_year = "2024";
void show_version()
{
@@ -32,9 +32,9 @@ void show_version()
// separate numbers of 5 or more digits in groups of 3 digits using '_'
const char * format_num3( long long num )
{
- const char * const si_prefix = "kMGTPEZY";
- const char * const binary_prefix = "KMGTPEZY";
- enum { buffers = 8, bufsize = 4 * sizeof num };
+ enum { buffers = 8, bufsize = 4 * sizeof num, n = 10 };
+ const char * const si_prefix = "kMGTPEZYRQ";
+ const char * const binary_prefix = "KMGTPEZYRQ";
static char buffer[buffers][bufsize]; // circle of static buffers for printf
static int current = 0;
@@ -42,19 +42,22 @@ const char * format_num3( long long num )
char * p = buf + bufsize - 1; // fill the buffer backwards
*p = 0; // terminator
const bool negative = num < 0;
- char prefix = 0; // try binary first, then si
- for( int i = 0; i < 8 && num != 0 && ( num / 1024 ) * 1024 == num; ++i )
- { num /= 1024; prefix = binary_prefix[i]; }
- if( prefix ) *(--p) = 'i';
- else
- for( int i = 0; i < 8 && num != 0 && ( num / 1000 ) * 1000 == num; ++i )
- { num /= 1000; prefix = si_prefix[i]; }
- if( prefix ) *(--p) = prefix;
+ if( num > 1024 || num < -1024 )
+ {
+ char prefix = 0; // try binary first, then si
+ for( int i = 0; i < n && num != 0 && num % 1024 == 0; ++i )
+ { num /= 1024; prefix = binary_prefix[i]; }
+ if( prefix ) *(--p) = 'i';
+ else
+ for( int i = 0; i < n && num != 0 && num % 1000 == 0; ++i )
+ { num /= 1000; prefix = si_prefix[i]; }
+ if( prefix ) *(--p) = prefix;
+ }
const bool split = num >= 10000 || num <= -10000;
for( int i = 0; ; )
{
- long long onum = num; num /= 10;
+ const long long onum = num; num /= 10;
*(--p) = llabs( onum - ( 10 * num ) ) + '0'; if( num == 0 ) break;
if( split && ++i >= 3 ) { i = 0; *(--p) = '_'; }
}
@@ -72,8 +75,7 @@ void show_option_error( const char * const arg, const char * const msg,
}
-// Recognized formats: <num>[YZEPTGM][i][Bs], <num>k[Bs], <num>Ki[Bs]
-//
+// Recognized formats: <num>k[Bs], <num>Ki[Bs], <num>[MGTPEZYRQ][i][Bs]
long long getnum( const char * const arg, const char * const option_name,
const int hardbs, const long long llimit = LLONG_MIN,
const long long ulimit = LLONG_MAX,
@@ -107,7 +109,7 @@ long long getnum( const char * const arg, const char * const option_name,
case 'k': if( tail[0] != 'i' ) exponent = 1; break;
case 'B':
case 's': usuf = *p; exponent = 0; break;
- default : if( tailp ) { tail = p; exponent = 0; }
+ default: if( tailp ) { tail = p; exponent = 0; }
}
if( exponent > 1 && tail[0] == 'i' ) { ++tail; factor = 1024; }
if( exponent > 0 && usuf == 0 && ( tail[0] == 'B' || tail[0] == 's' ) )