X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=utility.h;h=13d5ac9a424062338cf0a7cd136b61e08a5c2e7c;hb=f4e463df436b0b3c97efe7e53c81b663e4241180;hp=3533948a818736952348ff52b9f5fa0a96c586fc;hpb=2127e1931eec4a688d41baf253744fc48ed8c989;p=mirror_smartmontools-debian.git diff --git a/utility.h b/utility.h index 3533948..13d5ac9 100644 --- a/utility.h +++ b/utility.h @@ -3,8 +3,8 @@ * * Home page of code is: http://smartmontools.sourceforge.net * - * Copyright (C) 2002-9 Bruce Allen - * Copyright (C) 2008-9 Christian Franke + * Copyright (C) 2002-11 Bruce Allen + * Copyright (C) 2008-12 Christian Franke * Copyright (C) 2000 Michael Cornwell * * This program is free software; you can redistribute it and/or modify @@ -26,7 +26,7 @@ #ifndef UTILITY_H_ #define UTILITY_H_ -#define UTILITY_H_CVSID "$Id: utility.h 2848 2009-07-18 20:14:38Z chrfranke $" +#define UTILITY_H_CVSID "$Id: utility.h 3558 2012-06-05 16:42:05Z chrfranke $" #include #include // for regex.h (according to POSIX) @@ -36,8 +36,14 @@ #include #include -#if !defined(__GNUC__) && !defined(__attribute__) -#define __attribute__(x) /**/ +#ifndef __GNUC__ +#define __attribute_format_printf(x, y) /**/ +#elif defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO +// Check format of __mingw_*printf() instead of MSVCRT.DLL:*printf() +#define __attribute_format_printf(x, y) __attribute__((format (gnu_printf, x, y))) +#define HAVE_WORKING_SNPRINTF 1 +#else +#define __attribute_format_printf(x, y) __attribute__((format (printf, x, y))) #endif // Make version information string @@ -45,13 +51,20 @@ std::string format_version_info(const char * prog_name, bool full = false); // return (v)sprintf() formated std::string std::string strprintf(const char * fmt, ...) - __attribute__ ((format (printf, 1, 2))); + __attribute_format_printf(1, 2); std::string vstrprintf(const char * fmt, va_list ap); +// Return true if STR starts with PREFIX +inline bool str_starts_with(const char * str, const char * prefix) + { return !strncmp(str, prefix, strlen(prefix)); } + +inline bool str_starts_with(const std::string & str, const char * prefix) + { return !strncmp(str.c_str(), prefix, strlen(prefix)); } + #ifndef HAVE_WORKING_SNPRINTF // Substitute by safe replacement functions int safe_snprintf(char *buf, int size, const char *fmt, ...) - __attribute__ ((format (printf, 3, 4))); + __attribute_format_printf(3, 4); int safe_vsnprintf(char *buf, int size, const char *fmt, va_list ap); #define snprintf safe_snprintf #define vsnprintf safe_vsnprintf @@ -75,7 +88,7 @@ void dateandtimezoneepoch(char *buffer, time_t tval); // itself is defined differently in smartctl and smartd. So the // function definition(s) are in smartd.c and in smartctl.c. void pout(const char *fmt, ...) - __attribute__ ((format (printf, 1, 2))); + __attribute_format_printf(1, 2); // replacement for perror() with redirected output. void syserror(const char *message); @@ -134,18 +147,19 @@ inline T * CheckFree(T * address, int whatline, const char* file) #endif // OLD_INTERFACE -// This function prints either to stdout or to the syslog as needed - -// [From GLIBC Manual: Since the prototype doesn't specify types for -// optional arguments, in a call to a variadic function the default -// argument promotions are performed on the optional argument -// values. This means the objects of type char or short int (whether -// signed or not) are promoted to either int or unsigned int, as -// appropriate.] -void PrintOut(int priority, const char *fmt, ...) __attribute__ ((format(printf, 2, 3))); +// Compile time check of byte ordering +// (inline const function allows compiler to remove dead code) +inline bool isbigendian() +{ +#ifdef WORDS_BIGENDIAN + return true; +#else + return false; +#endif +} -// run time, determine byte ordering -int isbigendian(); +// Runtime check of byte ordering, throws if different from isbigendian(). +void check_endianness(); // This value follows the peripheral device type value as defined in // SCSI Primary Commands, ANSI INCITS 301:1997. It is also used in @@ -172,6 +186,14 @@ void FixGlibcTimeZoneBug(); // convert time in msec to a text string void MsecToText(unsigned int msec, char *txt); +// Format integer with thousands separator +const char * format_with_thousands_sep(char * str, int strsize, uint64_t val, + const char * thousands_sep = 0); + +// Format capacity with SI prefixes +const char * format_capacity(char * str, int strsize, uint64_t val, + const char * decimal_point = 0); + // Wrapper class for a raw data buffer class raw_buffer { @@ -262,7 +284,8 @@ public: // Construction & assignment regular_expression(); - regular_expression(const char * pattern, int flags); + regular_expression(const char * pattern, int flags, + bool throw_on_error = true); ~regular_expression(); @@ -312,26 +335,19 @@ private: bool compile(); }; -// macros to control printing -#define PRINT_ON(control) {if (control->printing_switchable) control->dont_print=false;} -#define PRINT_OFF(control) {if (control->printing_switchable) control->dont_print=true;} +#ifdef _WIN32 +// Get exe directory +//(implemented in os_win32.cpp) +std::string get_exe_dir(); +#endif + #ifdef OLD_INTERFACE -// possible values for controller_type in extern.h +// remaining controller types in old interface modules #define CONTROLLER_UNKNOWN 0x00 #define CONTROLLER_ATA 0x01 #define CONTROLLER_SCSI 0x02 -#define CONTROLLER_3WARE 0x03 // set by -d option, but converted to one of three types below -#define CONTROLLER_3WARE_678K 0x04 // NOT set by guess_device_type() -#define CONTROLLER_3WARE_9000_CHAR 0x05 // set by guess_device_type() -#define CONTROLLER_3WARE_678K_CHAR 0x06 // set by guess_device_type() -#define CONTROLLER_MARVELL_SATA 0x07 // SATA drives behind Marvell controllers -#define CONTROLLER_SAT 0x08 // SATA device behind a SCSI ATA Translation (SAT) layer -#define CONTROLLER_HPT 0x09 // SATA drives behind HighPoint Raid controllers -#define CONTROLLER_CCISS 0x10 // CCISS controller -#define CONTROLLER_PARSEDEV 0x11 // "smartctl -r ataioctl,2 ..." output parser pseudo-device -#define CONTROLLER_USBCYPRESS 0x12 // ATA device behind Cypress USB bridge -#define CONTROLLER_ARECA 0x13 // Areca controller #endif #endif +