Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

util.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  *   FILE
00004  *      pqxx/util.h
00005  *
00006  *   DESCRIPTION
00007  *      Various utility definitions for libpqxx
00008  *
00009  * Copyright (c) 2001-2003, Jeroen T. Vermeulen <jtv@xs4all.nl>
00010  *
00011  *-------------------------------------------------------------------------
00012  */
00013 #ifndef PQXX_UTIL_H
00014 #define PQXX_UTIL_H
00015 
00016 #include "pqxx/compiler.h"
00017 
00018 #include <cstdio>
00019 #include <stdexcept>
00020 #include <string>
00021 #include <typeinfo>
00022 
00023 extern "C"
00024 {
00025 #include "libpq-fe.h"
00026 }
00027 
00028 
00029 namespace pqxx
00030 {
00031 typedef long Result_size_type;
00032 typedef int Tuple_size_type;
00033 
00036 template<typename T> inline const char *FmtString(T);
00037 
00038 // Not implemented to prevent accidents with irregular meaning of argument:
00039 // template<> inline const char *FmtString(const char *&) { return "%s"; }
00040 
00041 template<> inline const char *FmtString(short)         { return "%hd"; }
00042 template<> inline const char *FmtString(unsigned short){ return "%hu"; }
00043 template<> inline const char *FmtString(int)           { return  "%i"; }
00044 template<> inline const char *FmtString(long)          { return "%li"; }
00045 template<> inline const char *FmtString(unsigned)      { return  "%u"; }
00046 template<> inline const char *FmtString(unsigned long) { return "%lu"; }
00047 template<> inline const char *FmtString(float)         { return  "%f"; }
00048 template<> inline const char *FmtString(double)        { return "%lf"; }
00049 template<> inline const char *FmtString(long double)   { return "%Lf"; }
00050 template<> inline const char *FmtString(char)          { return  "%c"; }
00051 template<> inline const char *FmtString(unsigned char) { return  "%c"; }
00052 
00053 
00055 template<typename T> inline PGSTD::string ToString(const T &Obj)
00056 {
00057   // TODO: Find a decent way to determine max string length at compile time!
00058   char Buf[500];
00059   sprintf(Buf, FmtString(Obj), Obj);
00060   return PGSTD::string(Buf);
00061 }
00062 
00063 template<> inline PGSTD::string ToString(const PGSTD::string &Obj) {return Obj;}
00064 template<> inline PGSTD::string ToString(const char *const &Obj) { return Obj; }
00065 template<> inline PGSTD::string ToString(char *const &Obj) { return Obj; }
00066 
00067 template<> inline PGSTD::string ToString(const bool &Obj) 
00068 { 
00069   return ToString(unsigned(Obj));
00070 }
00071 
00072 template<> inline PGSTD::string ToString(const short &Obj)
00073 {
00074   return ToString(int(Obj));
00075 }
00076 
00077 template<> inline PGSTD::string ToString(const unsigned short &Obj)
00078 {
00079   return ToString(unsigned(Obj));
00080 }
00081 
00082 
00083 template<typename T> inline void FromString(const char Str[], T &Obj)
00084 {
00085   if (!Str) throw PGSTD::runtime_error("Attempt to convert NULL string to " +
00086                                      PGSTD::string(typeid(T).name()));
00087 
00088   if (sscanf(Str, FmtString(Obj), &Obj) != 1)
00089     throw PGSTD::runtime_error("Cannot convert value '" + 
00090                              PGSTD::string(Str) + 
00091                              "' to " + typeid(T).name());
00092 }
00093 
00094 
00095 template<> inline void FromString(const char Str[], PGSTD::string &Obj)
00096 {
00097   if (!Str) 
00098     throw PGSTD::runtime_error("Attempt to convert NULL C string to C++ "
00099                                "string");
00100   Obj = Str;
00101 }
00102 
00103 
00104 template<> inline void FromString(const char Str[], const char *&Obj)
00105 {
00106   if (!Str)
00107     throw PGSTD::runtime_error("Attempt to read NULL string");
00108   Obj = Str;
00109 }
00110 
00111 template<> inline void FromString(const char Str[], bool &Obj)
00112 {
00113   if (!Str)
00114     throw PGSTD::runtime_error("Attempt to read NULL string");
00115 
00116   switch (Str[0])
00117   {
00118   case 0:
00119   case 'f':
00120     Obj = false;
00121     break;
00122   case '0':
00123     {
00124       int I;
00125       FromString(Str, I);
00126       Obj = (I != 0);
00127     }
00128     break;
00129   default:
00130     Obj = true;
00131   }
00132 }
00133 
00134 
00137 template<typename T> PGSTD::string Quote(const T &Obj, bool EmptyIsNull=false);
00138 
00139 
00141 template<> inline PGSTD::string Quote(const PGSTD::string &Obj, 
00142                                       bool EmptyIsNull)
00143 {
00144   if (EmptyIsNull && Obj.empty()) return "null";
00145 
00146   PGSTD::string Result;
00147   Result.reserve(Obj.size() + 2);
00148   Result += "'";
00149 
00150 #ifdef HAVE_PQESCAPESTRING
00151 
00152   char *const Buf = new char[2*Obj.size() + 1];
00153   try
00154   {
00155     PQescapeString(Buf, Obj.c_str(), Obj.size());
00156     Result += Buf;
00157   }
00158   catch (const PGSTD::exception &)
00159   {
00160     delete [] Buf;
00161     throw;
00162   }
00163   delete [] Buf;
00164 
00165 #else
00166 
00167   for (PGSTD::string::size_type i=0; i < Obj.size(); ++i)
00168   {
00169     if (isgraph(Obj[i]))
00170     {
00171       switch (Obj[i])
00172       {
00173       case '\'':
00174       case '\\':
00175         Result += '\\';
00176       }
00177       Result += Obj[i];
00178     }
00179     else
00180     {
00181         char s[10];
00182         sprintf(s, 
00183                 "\\%03o", 
00184                 static_cast<unsigned int>(static_cast<unsigned char>(Obj[i])));
00185         Result.append(s, 4);
00186     }
00187   }
00188 
00189 #endif
00190 
00191   return Result + '\'';
00192 }
00193 
00194 
00197 template<> inline PGSTD::string Quote(const char *const & Obj, 
00198                                       bool EmptyIsNull)
00199 {
00200   if (!Obj) return "null";
00201   return Quote(PGSTD::string(Obj), EmptyIsNull);
00202 }
00203 
00204 
00206 
00211 template<int LEN> inline PGSTD::string Quote(const char (&Obj)[LEN],
00212                                              bool EmptyIsNull=false)    //[t18]
00213 {
00214   return Quote(PGSTD::string(Obj), EmptyIsNull);
00215 }
00216 
00217 
00221 template<typename T> inline PGSTD::string Quote(const T &Obj, bool EmptyIsNull)
00222 {
00223   return Quote(ToString(Obj), EmptyIsNull);
00224 }
00225 
00226 
00227 
00229 template<typename T> PGSTD::string Classname(const T *);
00230 
00231 
00237 template<typename GUEST>
00238 class Unique
00239 {
00240 public:
00241   Unique() : m_Guest(0) {}
00242 
00243   const GUEST *get() const throw () { return m_Guest; }
00244 
00245   void Register(const GUEST *G)
00246   {
00247     if (!G) throw PGSTD::logic_error("Internal libpqxx error: NULL " + 
00248                                      Classname(G));
00249     
00250     if (m_Guest)
00251     {
00252       if (G == m_Guest)
00253         throw PGSTD::logic_error(Classname(G) +
00254                                  " '" +
00255                                  G->Name() +
00256                                  "' started more than once without closing");
00257 
00258       throw PGSTD::logic_error("Started " + 
00259                                Classname(G) +
00260                                " '" + 
00261                                G->Name() + 
00262                                "' while '" +
00263                                m_Guest->Name() +
00264                                "' was still active");
00265     }
00266     
00267     m_Guest = G;
00268   }
00269 
00270   void Unregister(const GUEST *G)
00271   {
00272     if (G != m_Guest)
00273     {
00274       if (!G) 
00275         throw PGSTD::logic_error("Closing NULL " + Classname(G));
00276       else if (!m_Guest)
00277         throw PGSTD::logic_error("Closing " + 
00278                                  Classname(G) +
00279                                  " '" +
00280                                  G->Name() +
00281                                  "' which wasn't open");
00282       else
00283         throw PGSTD::logic_error("Closing wrong " + 
00284                                  Classname(G) +
00285                                  "; expected '" +
00286                                  m_Guest->Name() +
00287                                  "' but got '" +
00288                                  G->Name() +
00289                                  "'");
00290     }
00291 
00292     m_Guest = 0;
00293   }
00294 
00295 private:
00296   const GUEST *m_Guest;
00297 
00298   // Not allowed:
00299   Unique(const Unique &);
00300   Unique &operator=(const Unique &);
00301 };
00302 
00303 }
00304 
00305 #endif
00306 

Generated on Mon Mar 31 17:57:33 2003 for libpqxx by doxygen1.3-rc3