]> git.proxmox.com Git - mirror_smartmontools-debian.git/blobdiff - utility.h
Closes #831504
[mirror_smartmontools-debian.git] / utility.h
index 346e0f33297a9c321d8a66e9168e7d8fc87df534..bd56a24933aae4efd55ca2bdf522714d0dbcd874 100644 (file)
--- a/utility.h
+++ b/utility.h
@@ -1,10 +1,10 @@
 /*
  * utility.h
  *
- * Home page of code is: http://smartmontools.sourceforge.net
+ * Home page of code is: http://www.smartmontools.org
  *
- * Copyright (C) 2002-11 Bruce Allen <smartmontools-support@lists.sourceforge.net>
- * Copyright (C) 2008-14 Christian Franke <smartmontools-support@lists.sourceforge.net>
+ * Copyright (C) 2002-11 Bruce Allen
+ * Copyright (C) 2008-16 Christian Franke
  * Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
  *
  * This program is free software; you can redistribute it and/or modify
@@ -25,7 +25,7 @@
 #ifndef UTILITY_H_
 #define UTILITY_H_
 
-#define UTILITY_H_CVSID "$Id: utility.h 4028 2014-12-13 14:59:48Z chrfranke $"
+#define UTILITY_H_CVSID "$Id: utility.h 4309 2016-04-24 14:59:15Z chrfranke $"
 
 #include <time.h>
 #include <sys/types.h> // for regex.h (according to POSIX)
@@ -92,9 +92,6 @@ void pout(const char *fmt, ...)
 // replacement for perror() with redirected output.
 void syserror(const char *message);
 
-// Function for processing -r option in smartctl and smartd
-int split_report_arg(char *s, int *i);
-
 // Function for processing -t selective... option in smartctl
 int split_selective_arg(char *s, uint64_t *start, uint64_t *stop, int *mode);
 
@@ -102,19 +99,6 @@ int split_selective_arg(char *s, uint64_t *start, uint64_t *stop, int *mode);
 // (exit is not compatible with C++ destructors)
 #define EXIT(status) { throw (int)(status); }
 
-
-#ifdef OLD_INTERFACE
-
-// Utility function to free memory
-void *FreeNonZero(void* address, int size, int whatline, const char* file);
-
-// A custom version of strdup() that keeps track of how much memory is
-// being allocated. If mustexist is set, it also throws an error if we
-// try to duplicate a NULL string.
-char *CustomStrDup(const char *ptr, int mustexist, int whatline, const char* file);
-
-#endif // OLD_INTERFACE
-
 // Compile time check of byte ordering
 // (inline const function allows compiler to remove dead code)
 inline bool isbigendian()
@@ -140,6 +124,15 @@ bool nonempty(const void * data, int size);
 // needed to fix glibc bug
 void FixGlibcTimeZoneBug();
 
+// Copy not null terminated char array to null terminated string.
+// Replace non-ascii characters.  Remove leading and trailing blanks.
+const char * format_char_array(char * str, int strsize, const char * chr, int chrsize);
+
+// Version for fixed size buffers.
+template<size_t STRSIZE, size_t CHRSIZE>
+inline const char * format_char_array(char (& str)[STRSIZE], const char (& chr)[CHRSIZE])
+  { return format_char_array(str, (int)STRSIZE, chr, (int)CHRSIZE); }
+
 // Format integer with thousands separator
 const char * format_with_thousands_sep(char * str, int strsize, uint64_t val,
                                        const char * thousands_sep = 0);
@@ -194,6 +187,8 @@ public:
 
   bool open(const char * name, const char * mode)
     {
+      if (m_file && m_owner)
+        fclose(m_file);
       m_file = fopen(name, mode);
       m_owner = true;
       return !!m_file;
@@ -201,6 +196,8 @@ public:
 
   void open(FILE * f, bool owner = false)
     {
+      if (m_file && m_owner)
+        fclose(m_file);
       m_file = f;
       m_owner = owner;
     }