File indexing completed on 2025-02-23 05:15:20

0001 //
0002 // Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton
0003 // Distributed under the Boost Software License, Version 1.0.
0004 // (See accompanying file LICENSE_1_0.txt or copy at
0005 // http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 
0008 #ifndef SOCI_POSTGRESQL_COMMON_H_INCLUDED
0009 #define SOCI_POSTGRESQL_COMMON_H_INCLUDED
0010 
0011 #include "soci/postgresql/soci-postgresql.h"
0012 #include "soci-cstrtoi.h"
0013 #include <cstdio>
0014 #include <cstring>
0015 #include <ctime>
0016 #include <vector>
0017 
0018 namespace soci
0019 {
0020 
0021 namespace details
0022 {
0023 
0024 namespace postgresql
0025 {
0026 
0027 // helper function for parsing boolean values as integers, throws if parsing
0028 // fails.
0029 template <typename T>
0030 T parse_as_boolean_or_throw(char const * buf)
0031 {
0032     // try additional conversion from boolean
0033     // (PostgreSQL gives 't' or 'f' for boolean results)
0034 
0035     if (buf[0] == 't' && buf[1] == '\0')
0036     {
0037         return static_cast<T>(1);
0038     }
0039     else if (buf[0] == 'f' && buf[1] == '\0')
0040     {
0041         return static_cast<T>(0);
0042     }
0043     else
0044     {
0045         throw soci_error("Cannot convert data.");
0046     }
0047 }
0048 
0049 template <typename T>
0050 T string_to_integer(char const * buf)
0051 {
0052     T result;
0053     if (!cstring_to_integer(result, buf))
0054         result = parse_as_boolean_or_throw<T>(buf);
0055 
0056     return result;
0057 }
0058 
0059 // helper function for parsing unsigned integers
0060 template <typename T>
0061 T string_to_unsigned_integer(char const * buf)
0062 {
0063     T result;
0064     if (!cstring_to_unsigned(result, buf))
0065         result = parse_as_boolean_or_throw<T>(buf);
0066 
0067     return result;
0068 }
0069 
0070 // helper for vector operations
0071 template <typename T>
0072 std::size_t get_vector_size(void * p)
0073 {
0074     std::vector<T> * v = static_cast<std::vector<T> *>(p);
0075     return v->size();
0076 }
0077 
0078 } // namespace postgresql
0079 
0080 } // namespace details
0081 
0082 } // namespace soci
0083 
0084 #endif // SOCI_POSTGRESQL_COMMON_H_INCLUDED