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/charset.h>
00026 
00027 namespace drizzled {
00028 namespace type {
00029 
00030 const char* convert(bool source, bool ansi_display)
00031 {
00032   if (source)
00033     return ansi_display ? "YES" : "TRUE";
00034   return ansi_display ? "NO" : "FALSE";
00035 }
00036 
00037 void convert(String& destination, bool source, bool ansi_display)
00038 {
00039   const char* v= convert(source, ansi_display);
00040   destination.alloc(strlen(v));
00041   strcpy(destination.c_ptr(), v);
00042   destination.length(strlen(v));
00043 }
00044 
00045 bool convert(bool &destination, const char *source, const size_t source_length)
00046 {
00047   switch (source_length)
00048   {
00049   case 1:
00050     {
00051       switch (source[0])
00052       {
00053       case 'y': case 'Y':
00054       case 't': case 'T': // PG compatibility
00055         destination= true;
00056         return true;
00057 
00058       case 'n': case 'N':
00059       case 'f': case 'F': // PG compatibility
00060         destination= false;
00061         return true;
00062       }
00063     }
00064     break;
00065 
00066   case 5:
00067     if (not (system_charset_info->strcasecmp(source, "FALSE")))
00068     {
00069       destination= false;
00070       return true;
00071     }
00072     break;
00073 
00074   case 4:
00075     if (not (system_charset_info->strcasecmp(source, "TRUE")))
00076     {
00077       destination= true;
00078       return true;
00079     }
00080     break;
00081 
00082   case 3:
00083     if (not (system_charset_info->strcasecmp(source, "YES")))
00084     {
00085       destination= true;
00086       return true;
00087     }
00088     break;
00089 
00090   case 2:
00091     if (not (system_charset_info->strcasecmp(source, "NO")))
00092     {
00093       destination= false;
00094       return true;
00095     }
00096     break;
00097   }
00098 
00099   // Failure to match
00100   destination= false;
00101   return false;
00102 }
00103 
00104 bool convert(bool &destination, String &source)
00105 {
00106   return convert(destination, source.c_ptr(), source.length());
00107 }
00108 
00109 } /* namespace type */
00110 } /* namespace drizzled */