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   bin item.
00027   In string context this is a binary string. 
00028   In number context this is a int64_t value.
00029 */
00030 
00031 Item_bin_string::Item_bin_string(str_ref arg)
00032 {
00033   const char *str= arg.data();
00034   const char *end= str + arg.size() - 1;
00035   unsigned char bits= 0;
00036   uint32_t power= 1;
00037 
00038   max_length= (arg.size() + 7) >> 3;
00039   char *ptr= (char*) memory::sql_alloc(max_length + 1);
00040   str_value.set(ptr, max_length, &my_charset_bin);
00041   ptr+= max_length - 1;
00042   ptr[1]= 0;                     // Set end null for string
00043   for (; end >= str; end--)
00044   {
00045     if (power == 256)
00046     {
00047       power= 1;
00048       *ptr--= bits;
00049       bits= 0;
00050     }
00051     if (*end == '1')
00052       bits|= power;
00053     power<<= 1;
00054   }
00055   *ptr= (char) bits;
00056   collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
00057   fixed= 1;
00058 }
00059 
00060 } /* namespace drizzled */