Drizzled Public API Documentation

hex_string.cc
1 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2008 Sun Microsystems, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include <config.h>
21 
22 #include <drizzled/error.h>
23 #include <drizzled/field.h>
24 #include <drizzled/item/hex_string.h>
25 #include <drizzled/item/string.h>
26 #include <drizzled/type/decimal.h>
27 
28 #include <algorithm>
29 
30 using namespace std;
31 
32 namespace drizzled {
33 
34 static char _dig_vec_lower[] = "0123456789abcdefghijklmnopqrstuvwxyz";
35 
36 inline uint32_t char_val(char X)
37 {
38  return (uint32_t) (X >= '0' && X <= '9' ? X-'0' :
39  X >= 'A' && X <= 'Z' ? X-'A'+10 :
40  X-'a'+10);
41 }
42 
43 Item_hex_string::Item_hex_string(str_ref arg)
44 {
45  max_length= (arg.size() + 1) / 2;
46  char *ptr=(char*) memory::sql_alloc(max_length+1);
47  if (!ptr)
48  return;
49  str_value.set(ptr,max_length,&my_charset_bin);
50  const char *str= arg.data();
51  char *end=ptr+max_length;
52  if (max_length * 2 != arg.size())
53  *ptr++=char_val(*str++); // Not even, assume 0 prefix
54  while (ptr != end)
55  {
56  *ptr++= (char) (char_val(str[0])*16+char_val(str[1]));
57  str+=2;
58  }
59  *ptr=0; // Keep purify happy
60  collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
61  fixed= 1;
62  unsigned_flag= 1;
63 }
64 
65 int64_t Item_hex_string::val_int()
66 {
67  // following assert is redundant, because fixed=1 assigned in constructor
68  assert(fixed == 1);
69  char *end= (char*) str_value.ptr()+str_value.length(),
70  *ptr= end - min(str_value.length(), sizeof(int64_t));
71 
72  uint64_t value=0;
73  for (; ptr != end ; ptr++)
74  value=(value << 8)+ (uint64_t) (unsigned char) *ptr;
75  return (int64_t) value;
76 }
77 
78 
79 type::Decimal *Item_hex_string::val_decimal(type::Decimal *decimal_value)
80 {
81  // following assert is redundant, because fixed=1 assigned in constructor
82  assert(fixed == 1);
83  uint64_t value= (uint64_t)val_int();
84  int2_class_decimal(E_DEC_FATAL_ERROR, value, true, decimal_value);
85  return (decimal_value);
86 }
87 
88 int Item_hex_string::save_in_field(Field *field, bool)
89 {
90  field->set_notnull();
91  if (field->result_type() == STRING_RESULT)
92  return field->store(str_value.ptr(), str_value.length(), collation.collation);
93 
94  uint64_t nr;
95  uint32_t length= str_value.length();
96  if (length > 8)
97  {
98  nr= field->flags & UNSIGNED_FLAG ? UINT64_MAX : INT64_MAX;
99  goto warn;
100  }
101  nr= (uint64_t) val_int();
102  if ((length == 8) && !(field->flags & UNSIGNED_FLAG) && (nr > INT64_MAX))
103  {
104  nr= INT64_MAX;
105  goto warn;
106  }
107  return field->store((int64_t) nr, true); // Assume hex numbers are unsigned
108 
109 warn:
110  if (!field->store((int64_t) nr, true))
111  field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
112  return 1;
113 }
114 
115 void Item_hex_string::print(String *str)
116 {
117  char *end= (char*) str_value.ptr() + str_value.length(),
118  *ptr= end - min(str_value.length(), sizeof(int64_t));
119  str->append(STRING_WITH_LEN("0x"));
120  for (; ptr != end ; ptr++)
121  {
122  str->append(_dig_vec_lower[((unsigned char) *ptr) >> 4]);
123  str->append(_dig_vec_lower[((unsigned char) *ptr) & 0x0F]);
124  }
125 }
126 
127 
128 bool Item_hex_string::eq(const Item *arg, bool binary_cmp) const
129 {
130  if (arg->basic_const_item() && arg->type() == type())
131  {
132  if (binary_cmp)
133  return !stringcmp(&str_value, &arg->str_value);
134  return !sortcmp(&str_value, &arg->str_value, collation.collation);
135  }
136  return false;
137 }
138 
139 Item *Item_hex_string::safe_charset_converter(const charset_info_st* tocs)
140 {
141  String tmp, *str= val_str(&tmp);
142  Item_string* conv= new Item_string(str->ptr(), str->length(), tocs);
143  conv->str_value.copy();
144  conv->str_value.mark_as_const();
145  return conv;
146 }
147 
148 
149 } /* namespace drizzled */