]> git.proxmox.com Git - mirror_smartmontools-debian.git/blobdiff - utility.cpp
Updated changelog
[mirror_smartmontools-debian.git] / utility.cpp
index 62e5845522d7371c5d01e59180b1b134c71d7a39..d7bb7bde3535dc3f7dc7fbf31f3d33ee1564296b 100644 (file)
@@ -3,8 +3,8 @@
  *
  * Home page of code is: http://smartmontools.sourceforge.net
  *
- * Copyright (C) 2002-11 Bruce Allen <smartmontools-support@lists.sourceforge.net>
- * Copyright (C) 2008-11 Christian Franke <smartmontools-support@lists.sourceforge.net>
+ * Copyright (C) 2002-12 Bruce Allen <smartmontools-support@lists.sourceforge.net>
+ * Copyright (C) 2008-12 Christian Franke <smartmontools-support@lists.sourceforge.net>
  * Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
  *
  * This program is free software; you can redistribute it and/or modify
 // BOTH SCSI AND ATA DEVICES, AND THAT MAY BE USED IN SMARTD,
 // SMARTCTL, OR BOTH.
 
+#include "config.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <time.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <ctype.h>
-#include <syslog.h>
 #include <stdarg.h>
 #include <sys/stat.h>
+#ifdef HAVE_LOCALE_H
+#include <locale.h>
+#endif
 #ifdef _WIN32
 #include <mbstring.h> // _mbsinc()
 #endif
 
 #include <stdexcept>
 
-#include "config.h"
 #include "svnversion.h"
 #include "int64.h"
 #include "utility.h"
@@ -50,7 +53,7 @@
 #include "atacmds.h"
 #include "dev_interface.h"
 
-const char * utility_cpp_cvsid = "$Id: utility.cpp 3285 2011-03-04 22:08:49Z chrfranke $"
+const char * utility_cpp_cvsid = "$Id: utility.cpp 3500 2012-01-01 18:03:36Z chrfranke $"
                                  UTILITY_H_CVSID INT64_H_CVSID;
 
 const char * packet_types[] = {
@@ -88,7 +91,7 @@ std::string format_version_info(const char * prog_name, bool full /*= false*/)
       "(build date "__DATE__")" // checkout without expansion of Id keywords
 #endif
       " [%s] "BUILD_INFO"\n"
-    "Copyright (C) 2002-11 by Bruce Allen, http://smartmontools.sourceforge.net\n",
+    "Copyright (C) 2002-12 by Bruce Allen, http://smartmontools.sourceforge.net\n",
     prog_name, smi()->get_os_version_str().c_str()
   );
   if (!full)
@@ -98,7 +101,8 @@ std::string format_version_info(const char * prog_name, bool full /*= false*/)
     "\n"
     "%s comes with ABSOLUTELY NO WARRANTY. This is free\n"
     "software, and you are welcome to redistribute it under\n"
-    "the terms of the GNU General Public License Version 2.\n"
+    "the terms of the GNU General Public License; either\n"
+    "version 2, or (at your option) any later version.\n"
     "See http://www.gnu.org for further details.\n"
     "\n",
     prog_name
@@ -727,6 +731,80 @@ void MsecToText(unsigned int msec, char *txt){
   return;
 }
 
+// Format integer with thousands separator
+const char * format_with_thousands_sep(char * str, int strsize, uint64_t val,
+                                       const char * thousands_sep /* = 0 */)
+{
+  if (!thousands_sep) {
+    thousands_sep = ",";
+#ifdef HAVE_LOCALE_H
+    setlocale(LC_ALL, "");
+    const struct lconv * currentlocale = localeconv();
+    if (*(currentlocale->thousands_sep))
+      thousands_sep = currentlocale->thousands_sep;
+#endif
+  }
+
+  char num[64];
+  snprintf(num, sizeof(num), "%"PRIu64, val);
+  int numlen = strlen(num);
+
+  int i = 0, j = 0;
+  do
+    str[j++] = num[i++];
+  while (i < numlen && (numlen - i) % 3 != 0 && j < strsize-1);
+  str[j] = 0;
+
+  while (i < numlen && j < strsize-1) {
+    j += snprintf(str+j, strsize-j, "%s%.3s", thousands_sep, num+i);
+    i += 3;
+  }
+
+  return str;
+}
+
+// Format capacity with SI prefixes
+const char * format_capacity(char * str, int strsize, uint64_t val,
+                             const char * decimal_point /* = 0 */)
+{
+  if (!decimal_point) {
+    decimal_point = ".";
+#ifdef HAVE_LOCALE_H
+    setlocale(LC_ALL, "");
+    const struct lconv * currentlocale = localeconv();
+    if (*(currentlocale->decimal_point))
+      decimal_point = currentlocale->decimal_point;
+#endif
+  }
+
+  const unsigned factor = 1000; // 1024 for KiB,MiB,...
+  static const char prefixes[] = " KMGTP";
+
+  // Find d with val in [d, d*factor)
+  unsigned i = 0;
+  uint64_t d = 1;
+  for (uint64_t d2 = d * factor; val >= d2; d2 *= factor) {
+    d = d2;
+    if (++i >= sizeof(prefixes)-2)
+      break;
+  }
+
+  // Print 3 digits
+  uint64_t n = val / d;
+  if (i == 0)
+    snprintf(str, strsize, "%u B", (unsigned)n);
+  else if (n >= 100) // "123 xB"
+    snprintf(str, strsize, "%"PRIu64" %cB", n, prefixes[i]);
+  else if (n >= 10)  // "12.3 xB"
+    snprintf(str, strsize, "%"PRIu64"%s%u %cB", n, decimal_point,
+        (unsigned)(((val % d) * 10) / d), prefixes[i]);
+  else               // "1.23 xB"
+    snprintf(str, strsize, "%"PRIu64"%s%02u %cB", n, decimal_point,
+        (unsigned)(((val % d) * 100) / d), prefixes[i]);
+
+  return str;
+}
+
 // return (v)sprintf() formatted std::string
 
 std::string vstrprintf(const char * fmt, va_list ap)