~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/hex_functions/hex_functions.cc

  • Committer: Jay Pipes
  • Date: 2009-09-15 21:01:42 UTC
  • mto: (1126.2.5 merge)
  • mto: This revision was merged to the branch mainline in revision 1128.
  • Revision ID: jpipes@serialcoder-20090915210142-x8mwiqn1q0vzjspp
Moves Alter_info out into its own header and source file, cleans up some related include mess in sql_lex.h, and renames Alter_info to AlterInfo.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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/charset_info.h>
23
 
#include <drizzled/function/str/strfunc.h>
24
 
#include <drizzled/internal/m_string.h>
25
 
#include <drizzled/plugin/function.h>
26
 
#include <drizzled/util/convert.h>
27
 
 
28
 
using namespace drizzled;
29
 
 
30
 
class HexFunction :public Item_str_func
31
 
{
32
 
  String tmp_value;
33
 
public:
34
 
  HexFunction() :Item_str_func() {}
35
 
  const char *func_name() const { return "hex"; }
36
 
  String *val_str(String *);
37
 
  void fix_length_and_dec()
38
 
  {
39
 
    collation.set(default_charset());
40
 
    decimals=0;
41
 
    max_length=args[0]->max_length*2*collation.collation->mbmaxlen;
42
 
  }
43
 
 
44
 
  bool check_argument_count(int n) { return n == 1; }
45
 
};
46
 
 
47
 
class UnHexFunction :public Item_str_func
48
 
{
49
 
  String tmp_value;
50
 
public:
51
 
  UnHexFunction() :Item_str_func()
52
 
  {
53
 
    /* there can be bad hex strings */
54
 
    maybe_null= 1;
55
 
  }
56
 
  const char *func_name() const { return "unhex"; }
57
 
  String *val_str(String *);
58
 
  void fix_length_and_dec()
59
 
  {
60
 
    collation.set(&my_charset_bin);
61
 
    decimals=0;
62
 
    max_length=(1+args[0]->max_length)/2;
63
 
  }
64
 
  bool check_argument_count(int n) { return n == 1; }
65
 
};
66
 
 
67
 
/**
68
 
  convert a hex digit into number.
69
 
*/
70
 
static int hexchar_to_int(char c)
71
 
{
72
 
  if (c <= '9' && c >= '0')
73
 
    return c-'0';
74
 
  c|=32;
75
 
  if (c <= 'f' && c >= 'a')
76
 
    return c-'a'+10;
77
 
  return -1;
78
 
}
79
 
 
80
 
String *HexFunction::val_str(String *str)
81
 
{
82
 
  String *res;
83
 
  assert(fixed == 1);
84
 
  if (args[0]->result_type() != STRING_RESULT)
85
 
  {
86
 
    uint64_t dec;
87
 
    char ans[65],*ptr;
88
 
    /* Return hex of unsigned int64_t value */
89
 
    if (args[0]->result_type() == REAL_RESULT ||
90
 
        args[0]->result_type() == DECIMAL_RESULT)
91
 
    {
92
 
      double val= args[0]->val_real();
93
 
      if ((val <= (double) INT64_MIN) ||
94
 
          (val >= (double) (uint64_t) UINT64_MAX))
95
 
        dec=  ~(int64_t) 0;
96
 
      else
97
 
        dec= (uint64_t) (val + (val > 0 ? 0.5 : -0.5));
98
 
    }
99
 
    else
100
 
      dec= (uint64_t) args[0]->val_int();
101
 
 
102
 
    if ((null_value= args[0]->null_value))
103
 
      return 0;
104
 
    ptr= internal::int64_t2str(dec,ans,16);
105
 
    if (str->copy(ans,(uint32_t) (ptr-ans),default_charset()))
106
 
      return &my_empty_string;                  // End of memory
107
 
    return str;
108
 
  }
109
 
 
110
 
  /* Convert given string to a hex string, character by character */
111
 
  res= args[0]->val_str(str);
112
 
  if (!res || tmp_value.alloc(res->length()*2+1))
113
 
  {
114
 
    null_value=1;
115
 
    return 0;
116
 
  }
117
 
  null_value=0;
118
 
  tmp_value.length(res->length()*2);
119
 
 
120
 
  (void) drizzled_string_to_hex((char*) tmp_value.ptr(), res->ptr(),
121
 
                                res->length());
122
 
  return &tmp_value;
123
 
}
124
 
 
125
 
  /** Convert given hex string to a binary string. */
126
 
 
127
 
String *UnHexFunction::val_str(String *str)
128
 
{
129
 
  const char *from, *end;
130
 
  char *to;
131
 
  String *res;
132
 
  uint32_t length;
133
 
  assert(fixed == 1);
134
 
 
135
 
  res= args[0]->val_str(str);
136
 
  if (!res || tmp_value.alloc(length= (1+res->length())/2))
137
 
  {
138
 
    null_value=1;
139
 
    return 0;
140
 
  }
141
 
 
142
 
  from= res->ptr();
143
 
  null_value= 0;
144
 
  tmp_value.length(length);
145
 
  to= (char*) tmp_value.ptr();
146
 
  if (res->length() % 2)
147
 
  {
148
 
    int hex_char;
149
 
    *to++= hex_char= hexchar_to_int(*from++);
150
 
    if ((null_value= (hex_char == -1)))
151
 
      return 0;
152
 
  }
153
 
  for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
154
 
  {
155
 
    int hex_char;
156
 
    *to= (hex_char= hexchar_to_int(from[0])) << 4;
157
 
    if ((null_value= (hex_char == -1)))
158
 
      return 0;
159
 
    *to|= hex_char= hexchar_to_int(from[1]);
160
 
    if ((null_value= (hex_char == -1)))
161
 
      return 0;
162
 
  }
163
 
  return &tmp_value;
164
 
}
165
 
 
166
 
plugin::Create_function<HexFunction> *hex_function= NULL;
167
 
plugin::Create_function<UnHexFunction> *unhex_function= NULL;
168
 
 
169
 
static int initialize(drizzled::module::Context &context)
170
 
{
171
 
  hex_function= new plugin::Create_function<HexFunction>("hex");
172
 
  unhex_function= new plugin::Create_function<UnHexFunction>("unhex");
173
 
  context.add(hex_function);
174
 
  context.add(unhex_function);
175
 
  return 0;
176
 
}
177
 
 
178
 
DRIZZLE_DECLARE_PLUGIN
179
 
{
180
 
  DRIZZLE_VERSION_ID,
181
 
  "hex_functions",
182
 
  "1.0",
183
 
  "Stewart Smith",
184
 
  "Convert a string to HEX() or from UNHEX()",
185
 
  PLUGIN_LICENSE_GPL,
186
 
  initialize, /* Plugin Init */
187
 
  NULL,   /* depends */
188
 
  NULL    /* config options */
189
 
}
190
 
DRIZZLE_DECLARE_PLUGIN_END;