OmniSciDB  8a228a1076
shared Namespace Reference

Classes

struct  DivUMod
 
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  PrintContainer
 

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

◆ appendMove()

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

Definition at line 32 of file misc.h.

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

◆ decode_base64() [1/2]

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:

◆ decode_base64() [2/2]

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)
Definition: base64.h:41
+ Here is the call graph for this function:

◆ decode_base64_uri()

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

Definition at line 21 of file base64.cpp.

References decode_base64().

Referenced by encode_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:
+ Here is the caller graph for this function:

◆ divUMod()

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

Definition at line 127 of file misc.h.

References shared::DivUMod::quot.

Referenced by formatDate(), and formatDateTime().

127  {
128  DivUMod div{num / den, num % den};
129  if (div.rem < 0) {
130  --div.quot;
131  div.rem += den;
132  }
133  return div;
134 }
+ Here is the caller graph for this function:

◆ dynamic_castable_to_any()

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

Definition at line 48 of file misc.h.

48  {
49  return (... || dynamic_cast<Ts const*>(ptr));
50 }

◆ encode_base64()

static std::string shared::encode_base64 ( const std::string &  val)
inlinestatic

Definition at line 45 of file base64.h.

References decode_base64_uri().

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 }
+ Here is the call graph for this function:

◆ formatDate()

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(), and operator<<().

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 = 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  int const y = 2000 + div_era.quot * 400 + yoe + (9 < moy);
33  unsigned const m = moy + (9 < moy ? -9 : 3);
34  unsigned const d = doy - (153 * moy + 2) / 5 + 1;
35  int const len = snprintf(buf, max, "%04d-%02u-%02u", y, m, d);
36  if (0 <= len && static_cast<size_t>(len) < max) {
37  return static_cast<size_t>(len);
38  }
39  return 0;
40 }
DivUMod divUMod(int64_t num, int64_t den)
Definition: misc.h:127
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ formatDateTime()

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

Definition at line 42 of file misc.cpp.

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

Referenced by DatumToString(), and operator<<().

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

◆ formatHMS()

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

Definition at line 78 of file misc.cpp.

References unsignedMod().

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

78  {
79  unsigned const seconds = unsignedMod(unixtime, 24 * 60 * 60);
80  unsigned const minutes = seconds / 60;
81  unsigned const ss = seconds % 60;
82  unsigned const hh = minutes / 60;
83  unsigned const mm = minutes % 60;
84  int const len = snprintf(buf, max, "%02u:%02u:%02u", hh, mm, ss);
85  if (0 <= len && static_cast<size_t>(len) < max) {
86  return static_cast<size_t>(len);
87  }
88  return 0;
89 }
uint64_t unsignedMod(int64_t num, int64_t den)
Definition: misc.h:137
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator<<()

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

Definition at line 81 of file misc.h.

References shared::PrintContainer< CONTAINER >::container, formatDate(), formatDateTime(), formatHMS(), and printContainer().

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

◆ printContainer()

template<typename CONTAINER >
PrintContainer<CONTAINER> shared::printContainer ( CONTAINER &  container)

◆ unsignedMod()

uint64_t shared::unsignedMod ( int64_t  num,
int64_t  den 
)
inline

Definition at line 137 of file misc.h.

Referenced by formatHMS().

137  {
138  int64_t mod = num % den;
139  if (mod < 0) {
140  mod += den;
141  }
142  return mod;
143 }
+ Here is the caller graph for this function: