OmniSciDB  340b00dbf6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shared Namespace Reference

Classes

struct  PrintContainer
 
struct  is_std_container
 
struct  is_std_container< std::deque< T, A > >
 
struct  is_std_container< std::list< T, A > >
 
struct  is_std_container< std::set< T, A > >
 
struct  is_std_container< std::unordered_set< T, A > >
 
struct  is_std_container< std::vector< T, A > >
 
struct  DivUMod
 

Functions

std::string decode_base64_uri (const std::string &data, bool trim_nulls)
 
std::string decode_base64 (const std::string &val, bool trim_nulls)
 
std::string decode_base64 (const std::string &val)
 
static std::string encode_base64 (const std::string &val)
 
size_t formatDate (char *buf, size_t const max, int64_t const unixtime)
 
size_t formatDateTime (char *buf, size_t const max, int64_t const timestamp, int const dimension)
 
size_t formatHMS (char *buf, size_t const max, int64_t const unixtime)
 
template<typename T >
size_t appendMove (std::vector< T > &destination, std::vector< T > &source)
 
template<typename... Ts, typename T >
bool dynamic_castable_to_any (T const *ptr)
 
template<typename CONTAINER >
PrintContainer< CONTAINER > printContainer (CONTAINER &container)
 
template<typename OSTREAM , typename CONTAINER >
OSTREAM & operator<< (OSTREAM &os, PrintContainer< CONTAINER > pc)
 
DivUMod divUMod (int64_t num, int64_t den)
 
uint64_t unsignedMod (int64_t num, int64_t den)
 

Function Documentation

template<typename T >
size_t shared::appendMove ( std::vector< T > &  destination,
std::vector< T > &  source 
)

Definition at line 33 of file misc.h.

33  {
34  if (source.empty()) {
35  return 0;
36  } else if (destination.empty()) {
37  destination = std::move(source);
38  return destination.size();
39  } else {
40  size_t const source_size = source.size();
41  destination.reserve(destination.size() + source_size);
42  std::move(std::begin(source), std::end(source), std::back_inserter(destination));
43  source.clear();
44  return source_size;
45  }
46 }
std::string shared::decode_base64 ( const std::string &  val,
bool  trim_nulls 
)
inline

Definition at line 27 of file base64.h.

Referenced by decode_base64(), and decode_base64_uri().

27  {
28  // For some strings, signatature particualary it is important no to trim
29  // '\0' characters
30  using namespace boost::archive::iterators;
31  using It = transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>;
32 
33  if (!trim_nulls) {
34  return std::string(It(val.begin()), It(val.end()));
35  }
36  return boost::algorithm::trim_right_copy_if(
37  std::string(It(std::begin(val)), It(std::end(val))),
38  [](char c) { return c == '\0'; });
39 }

+ Here is the caller graph for this function:

std::string shared::decode_base64 ( const std::string &  val)
inline

Definition at line 41 of file base64.h.

References decode_base64().

41  {
42  return decode_base64(val, true);
43 }
std::string decode_base64(const std::string &val, bool trim_nulls)
Definition: base64.h:27

+ Here is the call graph for this function:

std::string shared::decode_base64_uri ( const std::string &  data,
bool  trim_nulls 
)

Definition at line 21 of file base64.cpp.

References decode_base64().

21  {
22  // Allocate a string large enough to hold exta '=' as padding
23  std::string uri_dec;
24  size_t data_len = data.length();
25  // base64_uri encoding removes '=' padding at the end of the string.
26  size_t padding = 4 - (data_len % 4);
27  if (padding == 4) {
28  padding = 0;
29  }
30  uri_dec.resize(data_len + padding);
31 
32  // base64_uri encoding replaces all '+' and '/' with '-' and '_' respectively.
33  std::transform(
34  data.begin(), data.end(), uri_dec.begin(), [](unsigned char c) -> unsigned char {
35  switch (c) {
36  case '-':
37  return '+';
38  case '_':
39  return '/';
40  default:
41  return c;
42  }
43  });
44  if (padding != 0) {
45  uri_dec.replace(uri_dec.begin() + data_len, uri_dec.end(), padding, '=');
46  }
47  // in the case of a signature from a JWT trim_nulls should be false
48  return decode_base64(uri_dec, trim_nulls);
49 }
std::string decode_base64(const std::string &val, bool trim_nulls)
Definition: base64.h:27

+ Here is the call graph for this function:

DivUMod shared::divUMod ( int64_t  num,
int64_t  den 
)
inline

Definition at line 128 of file misc.h.

References shared::DivUMod::quot.

Referenced by formatDate(), and formatDateTime().

128  {
129  DivUMod div{num / den, num % den};
130  if (div.rem < 0) {
131  --div.quot;
132  div.rem += den;
133  }
134  return div;
135 }

+ Here is the caller graph for this function:

template<typename... Ts, typename T >
bool shared::dynamic_castable_to_any ( T const *  ptr)

Definition at line 49 of file misc.h.

49  {
50  return (... || dynamic_cast<Ts const*>(ptr));
51 }
static std::string shared::encode_base64 ( const std::string &  val)
inlinestatic

Definition at line 45 of file base64.h.

45  {
46  using namespace boost::archive::iterators;
47  using It = base64_from_binary<transform_width<std::string::const_iterator, 6, 8>>;
48  auto tmp = std::string(It(std::begin(val)), It(std::end(val)));
49  return tmp.append((3 - val.size() % 3) % 3, '=');
50 }
size_t shared::formatDate ( char *  buf,
size_t const  max,
int64_t const  unixtime 
)

Definition at line 25 of file misc.cpp.

References divUMod(), shared::DivUMod::quot, and shared::DivUMod::rem.

Referenced by DatumToString().

25  {
26  DivUMod const div_day = divUMod(unixtime, 24 * 60 * 60);
27  DivUMod const div_era = divUMod(div_day.quot - 11017, 146097);
28  unsigned const doe = static_cast<unsigned>(div_era.rem);
29  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
30  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
31  unsigned const moy = (5 * doy + 2) / 153;
32  static_assert(8 <= sizeof(long long)); // long long needed for snprintf()
33  long long const y = 2000 + div_era.quot * 400 + yoe + (9 < moy);
34  unsigned const m = moy + (9 < moy ? -9 : 3);
35  unsigned const d = doy - (153 * moy + 2) / 5 + 1;
36  int const len = snprintf(buf, max, "%04lld-%02u-%02u", y, m, d);
37  if (0 <= len && static_cast<size_t>(len) < max) {
38  return static_cast<size_t>(len);
39  }
40  return 0;
41 }
DivUMod divUMod(int64_t num, int64_t den)
Definition: misc.h:128

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t shared::formatDateTime ( char *  buf,
size_t const  max,
int64_t const  timestamp,
int const  dimension 
)

Definition at line 43 of file misc.cpp.

References divUMod(), shared::DivUMod::quot, and shared::DivUMod::rem.

Referenced by DatumToString().

46  {
47  constexpr int pow10[10]{
48  1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
49  DivUMod const div_hip = divUMod(timestamp, pow10[dimension]);
50  DivUMod const div_day = divUMod(div_hip.quot, 24 * 60 * 60);
51  DivUMod const div_era = divUMod(div_day.quot - 11017, 146097);
52  unsigned const doe = static_cast<unsigned>(div_era.rem);
53  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
54  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
55  unsigned const moy = (5 * doy + 2) / 153;
56  static_assert(8 <= sizeof(long long)); // long long needed for snprintf()
57  long long const y = 2000 + div_era.quot * 400 + yoe + (9 < moy);
58  unsigned const m = moy + (9 < moy ? -9 : 3);
59  unsigned const d = doy - (153 * moy + 2) / 5 + 1;
60  unsigned const minutes = static_cast<unsigned>(div_day.rem) / 60;
61  unsigned const ss = div_day.rem % 60;
62  unsigned const hh = minutes / 60;
63  unsigned const mm = minutes % 60;
64  int const len =
65  snprintf(buf, max, "%04lld-%02u-%02u %02u:%02u:%02u", y, m, d, hh, mm, ss);
66  if (0 <= len && static_cast<size_t>(len) < max) {
67  if (dimension) {
68  int const len_frac = snprintf(
69  buf + len, max - len, ".%0*d", dimension, static_cast<int>(div_hip.rem));
70  if (0 <= len_frac && static_cast<size_t>(len + len_frac) < max) {
71  return static_cast<size_t>(len + len_frac);
72  }
73  } else {
74  return static_cast<size_t>(len);
75  }
76  }
77  return 0;
78 }
DivUMod divUMod(int64_t num, int64_t den)
Definition: misc.h:128

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t shared::formatHMS ( char *  buf,
size_t const  max,
int64_t const  unixtime 
)

Definition at line 80 of file misc.cpp.

References unsignedMod().

Referenced by DatumToString(), import_export::QueryExporterCSV::exportResults(), import_export::anonymous_namespace{QueryExporterGDAL.cpp}::insert_array_column(), and import_export::anonymous_namespace{QueryExporterGDAL.cpp}::insert_scalar_column().

80  {
81  unsigned const seconds = static_cast<unsigned>(unsignedMod(unixtime, 24 * 60 * 60));
82  unsigned const minutes = seconds / 60;
83  unsigned const ss = seconds % 60;
84  unsigned const hh = minutes / 60;
85  unsigned const mm = minutes % 60;
86  int const len = snprintf(buf, max, "%02u:%02u:%02u", hh, mm, ss);
87  if (0 <= len && static_cast<size_t>(len) < max) {
88  return static_cast<size_t>(len);
89  }
90  return 0;
91 }
uint64_t unsignedMod(int64_t num, int64_t den)
Definition: misc.h:138

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename OSTREAM , typename CONTAINER >
OSTREAM& shared::operator<< ( OSTREAM &  os,
PrintContainer< CONTAINER >  pc 
)

Definition at line 82 of file misc.h.

References printContainer().

82  {
83  if (pc.container.empty()) {
84  return os << "()";
85  } else {
86  if constexpr (is_std_container<typename CONTAINER::value_type>::value) {
87  os << '(';
88  for (auto& container : pc.container) {
89  os << printContainer(container);
90  }
91  } else {
92  for (auto itr = pc.container.begin(); itr != pc.container.end(); ++itr) {
93  if constexpr (std::is_pointer_v<typename CONTAINER::value_type>) {
94  os << (itr == pc.container.begin() ? '(' : ' ') << (void const*)*itr;
95  } else {
96  os << (itr == pc.container.begin() ? '(' : ' ') << *itr;
97  }
98  }
99  }
100  return os << ')';
101  }
102 }
PrintContainer< CONTAINER > printContainer(CONTAINER &container)
Definition: misc.h:64

+ Here is the call graph for this function:

template<typename CONTAINER >
PrintContainer<CONTAINER> shared::printContainer ( CONTAINER &  container)
uint64_t shared::unsignedMod ( int64_t  num,
int64_t  den 
)
inline

Definition at line 138 of file misc.h.

Referenced by formatHMS().

138  {
139  int64_t mod = num % den;
140  if (mod < 0) {
141  mod += den;
142  }
143  return mod;
144 }

+ Here is the caller graph for this function: