sqlite3/common.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 #ifndef SOCI_SQLITE3_COMMON_H_INCLUDED
9 #define SOCI_SQLITE3_COMMON_H_INCLUDED
10 
11 #include <error.h>
12 #include <cstddef>
13 #include <cstdio>
14 #include <cstring>
15 #include <ctime>
16 #include <vector>
17 #include <limits>
18 
19 namespace soci { namespace details { namespace sqlite3 {
20 
21 // helper function for parsing datetime values
22 void parse_std_tm(char const *buf, std::tm &t);
23 
24 // helper for vector operations
25 template <typename T>
26 std::size_t get_vector_size(void *p)
27 {
28  std::vector<T> *v = static_cast<std::vector<T> *>(p);
29  return v->size();
30 }
31 
32 template <typename T>
33 void resize_vector(void *p, std::size_t sz)
34 {
35  std::vector<T> *v = static_cast<std::vector<T> *>(p);
36  v->resize(sz);
37 }
38 
39 // helper function for parsing integers
40 template <typename T>
41 T string_to_integer(char const * buf)
42 {
43  long long t(0);
44  int n(0);
45  int const converted = std::sscanf(buf, "%" LL_FMT_FLAGS "d%n", &t, &n);
46  if (converted == 1 && static_cast<std::size_t>(n) == std::strlen(buf))
47  {
48  // successfully converted to long long
49  // and no other characters were found in the buffer
50 
51  const T max = (std::numeric_limits<T>::max)();
52  const T min = (std::numeric_limits<T>::min)();
53  if (t <= static_cast<long long>(max) &&
54  t >= static_cast<long long>(min))
55  {
56  return static_cast<T>(t);
57  }
58  }
59 
60  throw soci_error("Cannot convert data.");
61 }
62 
63 // helper function for parsing unsigned integers
64 template <typename T>
65 T string_to_unsigned_integer(char const * buf)
66 {
67  unsigned long long t(0);
68  int n(0);
69  int const converted = std::sscanf(buf, "%" LL_FMT_FLAGS "u%n", &t, &n);
70  if (converted == 1 && static_cast<std::size_t>(n) == std::strlen(buf))
71  {
72  // successfully converted to unsigned long long
73  // and no other characters were found in the buffer
74 
75  T const max = (std::numeric_limits<T>::max)();
76  if (t <= static_cast<unsigned long long>(max))
77  {
78  return static_cast<T>(t);
79  }
80  }
81 
82  throw soci_error("Cannot convert data.");
83 }
84 
85 }}} // namespace soci::details::sqlite3
86 
87 #endif // SOCI_SQLITE3_COMMON_H_INCLUDED
T string_to_integer(char const *buf)
T string_to_unsigned_integer(char const *buf)
SOCI_SQLITE3_DECL sqlite3_backend_factory const sqlite3
void parse_std_tm(char const *buf, std::tm &t)
std::size_t get_vector_size(void *p)
void resize_vector(void *p, std::size_t sz)
#define LL_FMT_FLAGS
Definition: soci-platform.h:14


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:40