Drizzled Public API Documentation

boolean.cc
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2011 Brian Aker
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include <drizzled/sql_string.h>
00024 #include <drizzled/type/boolean.h>
00025 #include <drizzled/global_charset_info.h>
00026 #include <drizzled/charset_info.h>
00027 
00028 namespace drizzled
00029 {
00030 
00031 namespace type
00032 {
00033 
00034 bool convert(String &destination, const bool source, bool ansi_display)
00035 {
00036   uint32_t mlength= (5) * system_charset_info->mbmaxlen;
00037 
00038   destination.alloc(mlength);
00039   char *buffer=(char*) destination.c_ptr();
00040 
00041   if (source)
00042   {
00043     if (ansi_display)
00044     {
00045       memcpy(buffer, "YES", 3);
00046       destination.length(3);
00047     }
00048     else
00049     {
00050       memcpy(buffer, "TRUE", 4);
00051       destination.length(4);
00052     }
00053   }
00054   else
00055   {
00056     if (ansi_display)
00057     {
00058       memcpy(buffer, "NO", 2);
00059       destination.length(2);
00060     }
00061     else
00062     {
00063       memcpy(buffer, "FALSE", 5);
00064       destination.length(5);
00065     }
00066   }
00067 
00068   return true;
00069 }
00070 
00071 bool convert(bool &destination, const char *source, const size_t source_length)
00072 {
00073   switch (source_length)
00074   {
00075   case 1:
00076     {
00077       switch (source[0])
00078       {
00079       case 'y': case 'Y':
00080       case 't': case 'T': // PG compatibility
00081         destination= true;
00082         return true;
00083 
00084       case 'n': case 'N':
00085       case 'f': case 'F': // PG compatibility
00086         destination= false;
00087         return true;
00088       }
00089     }
00090     break;
00091 
00092   case 5:
00093     if (not (my_strcasecmp(system_charset_info, source, "FALSE")))
00094     {
00095       destination= false;
00096       return true;
00097     }
00098     break;
00099 
00100   case 4:
00101     if (not (my_strcasecmp(system_charset_info, source, "TRUE")))
00102     {
00103       destination= true;
00104       return true;
00105     }
00106     break;
00107 
00108   case 3:
00109     if (not (my_strcasecmp(system_charset_info, source, "YES")))
00110     {
00111       destination= true;
00112       return true;
00113     }
00114     break;
00115 
00116   case 2:
00117     if (not (my_strcasecmp(system_charset_info, source, "NO")))
00118     {
00119       destination= false;
00120       return true;
00121     }
00122     break;
00123   }
00124 
00125   // Failure to match
00126   destination= false;
00127   return false;
00128 }
00129 
00130 bool convert(bool &destination, String &source)
00131 {
00132   return convert(destination, source.c_ptr(), source.length());
00133 }
00134 
00135 } /* namespace type */
00136 } /* namespace drizzled */