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-2006 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 #include "common.h"
00009 //
00010 #if defined(SOCI_HEADERS_BURIED)
00011 #       include <soci/core/soci-backend.h>
00012 #else
00013 #       include <soci-backend.h>
00014 #endif
00015 //
00016 #include <ciso646>
00017 #include <cstdlib>
00018 #include <cstring>
00019 #include <ctime>
00020 
00021 namespace // anonymous
00022 {
00023 
00024 // helper function for parsing decimal data (for std::tm)
00025 long parse10(char const *&p1, char *&p2, const char *msg)
00026 {
00027     long v = std::strtol(p1, &p2, 10);
00028     if (p2 != p1)
00029     {
00030         p1 = p2 + 1;
00031         return v;
00032     }
00033     else
00034     {
00035         throw soci::soci_error(msg);
00036     }
00037 }
00038 
00039 } // namespace anonymous
00040 
00041 
00042 void soci::details::mysql::parse_std_tm(char const *buf, std::tm &t)
00043 {
00044     char const *p1 = buf;
00045     char *p2;
00046     long year, month, day;
00047     long hour = 0, minute = 0, second = 0;
00048 
00049     const char *errMsg = "Cannot convert data to std::tm.";
00050 
00051     if (strchr(buf, '-') != NULL)
00052     {
00053       year  = parse10(p1, p2, errMsg);
00054       month = parse10(p1, p2, errMsg);
00055       day   = parse10(p1, p2, errMsg);
00056     }
00057     else
00058     {
00059       year  = 2000;
00060       month = 1;
00061       day   = 1;
00062     }
00063 
00064 
00065     if (strchr(buf, ':') != NULL)
00066     {
00067         // there is also the time of day available
00068         hour   = parse10(p1, p2, errMsg);
00069         minute = parse10(p1, p2, errMsg);
00070         second = parse10(p1, p2, errMsg);
00071     }
00072 
00073     t.tm_isdst = -1;
00074     t.tm_year = year - 1900;
00075     t.tm_mon  = month - 1;
00076     t.tm_mday = day;
00077     t.tm_hour = hour;
00078     t.tm_min  = minute;
00079     t.tm_sec  = second;
00080 
00081     std::mktime(&t);
00082 }
00083 
00084 char * soci::details::mysql::quote(MYSQL * conn, const char *s, int len)
00085 {
00086     char *retv = new char[2 * len + 3];
00087     retv[0] = '\'';
00088     int len_esc = mysql_real_escape_string(conn, retv + 1, s, len);
00089     retv[len_esc + 1] = '\'';
00090     retv[len_esc + 2] = '\0';
00091 
00092     return retv;
00093 }
 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