* utility.h
*
* Home page of code is: https://www.smartmontools.org
*
* Copyright (C) 2002-11 Bruce Allen
* Copyright (C) 2008-23 Christian Franke
* Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef UTILITY_H_
#define UTILITY_H_
#define UTILITY_H_CVSID "$Id: utility.h 5519 2023-07-24 15:57:54Z chrfranke $"
#include <float.h>
#include <time.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <sys/types.h>
#ifdef WITH_CXX11_REGEX
#include <regex>
#else
#include <regex.h>
#endif
#ifndef __GNUC__
#define __attribute_format_printf(x, y)
#elif defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO
#define __attribute_format_printf(x, y) __attribute__((format (gnu_printf, x, y)))
#else
#define __attribute_format_printf(x, y) __attribute__((format (printf, x, y)))
#endif
std::string format_version_info(const char * prog_name, int lines = 2);
std::string strprintf(const char * fmt, ...)
__attribute_format_printf(1, 2);
std::string vstrprintf(const char * fmt, va_list ap);
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)); }
struct tm * time_to_tm_local(struct tm * tp, time_t t);
#define DATEANDEPOCHLEN 64
void dateandtimezoneepoch(char (& buffer)[DATEANDEPOCHLEN], time_t tval);
void pout(const char *fmt, ...)
__attribute_format_printf(1, 2);
void syserror(const char *message);
int split_selective_arg(char *s, uint64_t *start, uint64_t *stop, int *mode);
inline bool isbigendian()
{
#ifdef WORDS_BIGENDIAN
return true;
#else
return false;
#endif
}
void swap2(char *location);
void swap4(char *location);
void swap8(char *location);
inline void swapx(unsigned short * p)
{ swap2((char*)p); }
inline void swapx(unsigned int * p)
{ swap4((char*)p); }
inline void swapx(uint64_t * p)
{ swap8((char*)p); }
void check_config();
const char *packetdevicetype(int type);
bool nonempty(const void * data, int size);
void FixGlibcTimeZoneBug();
const char * format_char_array(char * str, int strsize, const char * chr, int chrsize);
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); }
const char * format_with_thousands_sep(char * str, int strsize, uint64_t val,
const char * thousands_sep = 0);
const char * format_capacity(char * str, int strsize, uint64_t val,
const char * decimal_point = 0);
class raw_buffer
{
public:
explicit raw_buffer(unsigned sz, unsigned char val = 0)
: m_data(new unsigned char[sz]),
m_size(sz)
{ memset(m_data, val, m_size); }
~raw_buffer()
{ delete [] m_data; }
unsigned size() const
{ return m_size; }
unsigned char * data()
{ return m_data; }
const unsigned char * data() const
{ return m_data; }
private:
unsigned char * m_data;
unsigned m_size;
raw_buffer(const raw_buffer &);
void operator=(const raw_buffer &);
};
class stdio_file
{
public:
explicit stdio_file(FILE * f = 0, bool owner = false)
: m_file(f), m_owner(owner) { }
stdio_file(const char * name, const char * mode)
: m_file(fopen(name, mode)), m_owner(true) { }
~stdio_file()
{
if (m_file && m_owner)
fclose(m_file);
}
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;
}
void open(FILE * f, bool owner = false)
{
if (m_file && m_owner)
fclose(m_file);
m_file = f;
m_owner = owner;
}
bool close()
{
if (!m_file)
return true;
bool ok = !ferror(m_file);
if (fclose(m_file))
ok = false;
m_file = 0;
return ok;
}
operator FILE * ()
{ return m_file; }
bool operator!() const
{ return !m_file; }
private:
FILE * m_file;
bool m_owner;
stdio_file(const stdio_file &);
void operator=(const stdio_file &);
};
class regular_expression
{
public:
#ifdef WITH_CXX11_REGEX
regular_expression() = default;
#else
regular_expression();
~regular_expression();
regular_expression(const regular_expression & x);
regular_expression & operator=(const regular_expression & x);
#endif
explicit regular_expression(const char * pattern);
bool compile(const char * pattern);
const char * get_pattern() const
{ return m_pattern.c_str(); }
const char * get_errmsg() const
{ return m_errmsg.c_str(); }
bool empty() const
{ return (m_pattern.empty() || !m_errmsg.empty()); }
bool full_match(const char * str) const;
#ifdef WITH_CXX11_REGEX
struct match_range { int rm_so, rm_eo; };
#else
typedef regmatch_t match_range;
#endif
bool execute(const char * str, unsigned nmatch, match_range * pmatch) const;
private:
std::string m_pattern;
std::string m_errmsg;
#ifdef WITH_CXX11_REGEX
std::regex m_regex;
#else
regex_t m_regex_buf;
void free_buf();
void copy_buf(const regular_expression & x);
#endif
bool compile();
};
#if defined(HAVE_LONG_DOUBLE_WIDER) && \
(!defined(__MINGW32__) || __USE_MINGW_ANSI_STDIO)
#define HAVE_LONG_DOUBLE_WIDER_PRINTF 1
#else
#undef HAVE_LONG_DOUBLE_WIDER_PRINTF
#endif
inline int uint128_to_str_precision_bits()
{
#if defined(HAVE___INT128)
return 128;
#elif defined(HAVE_LONG_DOUBLE_WIDER_PRINTF)
return LDBL_MANT_DIG;
#else
return DBL_MANT_DIG;
#endif
}
const char * uint128_hilo_to_str(char * str, int strsize, uint64_t value_hi, uint64_t value_lo);
template <size_t SIZE>
inline const char * uint128_hilo_to_str(char (& str)[SIZE], uint64_t value_hi, uint64_t value_lo)
{ return uint128_hilo_to_str(str, (int)SIZE, value_hi, value_lo); }
long long get_timer_usec();
#ifdef _WIN32
std::string get_exe_dir();
#endif
#ifdef OLD_INTERFACE
#define CONTROLLER_UNKNOWN 0x00
#define CONTROLLER_ATA 0x01
#define CONTROLLER_SCSI 0x02
#endif
#endif