Drizzled Public API Documentation

bin_string.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
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; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <config.h>
00021 #include <drizzled/item/bin_string.h>
00022 
00023 namespace drizzled
00024 {
00025 
00026 /*
00027   bin item.
00028   In string context this is a binary string. 
00029   In number context this is a int64_t value.
00030 */
00031 
00032 Item_bin_string::Item_bin_string(const char *str, uint32_t str_length)
00033 {
00034   const char *end= str + str_length - 1;
00035   unsigned char bits= 0;
00036   uint32_t power= 1;
00037 
00038   max_length= (str_length + 7) >> 3;
00039   char *ptr= (char*) memory::sql_alloc(max_length + 1);
00040   if (!ptr)
00041     return;
00042   str_value.set(ptr, max_length, &my_charset_bin);
00043   ptr+= max_length - 1;
00044   ptr[1]= 0;                     // Set end null for string
00045   for (; end >= str; end--)
00046   {
00047     if (power == 256)
00048     {
00049       power= 1;
00050       *ptr--= bits;
00051       bits= 0;
00052     }
00053     if (*end == '1')
00054       bits|= power;
00055     power<<= 1;
00056   }
00057   *ptr= (char) bits;
00058   collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
00059   fixed= 1;
00060 }
00061 
00062 
00063 } /* namespace drizzled */