SOCI Logo Get SOCI at SourceForge.net. Fast, secure and Free Open Source software downloads

common.cpp

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton
00003 // Distributed under the Boost Software License, Version 1.0.
00004 // (See accompanying file LICENSE_1_0.txt or copy at
00005 // http://www.boost.org/LICENSE_1_0.txt)
00006 //
00007 
00008 //
00009 #if defined(SOCI_HEADERS_BURIED)
00010 #       include <soci/core/soci-backend.h>
00011 #else
00012 #       include <soci-backend.h>
00013 #endif
00014 //
00015 #include "common.h"
00016 #include <cstdlib>
00017 #include <ctime>
00018 
00019 
00020 namespace // anonymous
00021 {
00022 
00023 // helper function for parsing decimal data (for std::tm)
00024 long parse10(char const * & p1, char * & p2, char const * msg)
00025 {
00026     long v = std::strtol(p1, &p2, 10);
00027     if (p2 != p1)
00028     {
00029         p1 = p2 + 1;
00030         return v;
00031     }
00032     else
00033     {
00034         throw soci::soci_error(msg);
00035     }
00036 }
00037 
00038 } // namespace anonymous
00039 
00040 
00041 void soci::details::postgresql::parse_std_tm(char const * buf, std::tm & t)
00042 {
00043     char const * p1 = buf;
00044     char * p2;
00045     char separator;
00046     long a, b, c;
00047     long year = 1900, month = 1, day = 1;
00048     long hour = 0, minute = 0, second = 0;
00049 
00050     char const * errMsg = "Cannot convert data to std::tm.";
00051 
00052     a = parse10(p1, p2, errMsg);
00053     separator = *p2;
00054     b = parse10(p1, p2, errMsg);
00055     c = parse10(p1, p2, errMsg);
00056 
00057     if (*p2 == ' ')
00058     {
00059         // there are more elements to parse
00060         // - assume that what was already parsed is a date part
00061         // and that the remaining elements describe the time of day
00062         year = a;
00063         month = b;
00064         day = c;
00065         hour   = parse10(p1, p2, errMsg);
00066         minute = parse10(p1, p2, errMsg);
00067         second = parse10(p1, p2, errMsg);
00068     }
00069     else
00070     {
00071         // only three values have been parsed
00072         if (separator == '-')
00073         {
00074             // assume the date value was read
00075             // (leave the time of day as 00:00:00)
00076             year = a;
00077             month = b;
00078             day = c;
00079         }
00080         else
00081         {
00082             // assume the time of day was read
00083             // (leave the date part as 1900-01-01)
00084             hour = a;
00085             minute = b;
00086             second = c;
00087         }
00088     }
00089 
00090     t.tm_isdst = -1;
00091     t.tm_year = year - 1900;
00092     t.tm_mon  = month - 1;
00093     t.tm_mday = day;
00094     t.tm_hour = hour;
00095     t.tm_min  = minute;
00096     t.tm_sec  = second;
00097 
00098     std::mktime(&t);
00099 }
00100 
00101 double soci::details::postgresql::string_to_double(char const * buf)
00102 {
00103     double t;
00104     int n;
00105     int const converted = sscanf(buf, "%lf%n", &t, &n);
00106     if (converted == 1 && static_cast<std::size_t>(n) == strlen(buf))
00107     {
00108         // successfully converted to double
00109         // and no other characters were found in the buffer
00110 
00111         return t;
00112     }
00113     else
00114     {
00115         throw soci_error("Cannot convert data.");
00116     }
00117 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
SourceForge Logo

Generated on Sun Oct 3 2010 17:42:16 for EXTRAS-SOCI by Doxygen 1.7.1