~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/str/hex.cc

  • Committer: Brian Aker
  • Date: 2010-02-11 22:43:58 UTC
  • Revision ID: brian@gaz-20100211224358-y0gdvnat2ahg4c1e
Disabling support for memcached plugins until we can test for version of
memcached.

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
 
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
#include <drizzled/util/convert.h>
 
22
#include <drizzled/function/str/hex.h>
 
23
#include "drizzled/internal/m_string.h"
 
24
 
 
25
namespace drizzled
 
26
{
 
27
 
 
28
/**
 
29
  convert a hex digit into number.
 
30
*/
 
31
static int hexchar_to_int(char c)
 
32
{
 
33
  if (c <= '9' && c >= '0')
 
34
    return c-'0';
 
35
  c|=32;
 
36
  if (c <= 'f' && c >= 'a')
 
37
    return c-'a'+10;
 
38
  return -1;
 
39
}
 
40
 
 
41
String *Item_func_hex::val_str(String *str)
 
42
{
 
43
  String *res;
 
44
  assert(fixed == 1);
 
45
  if (args[0]->result_type() != STRING_RESULT)
 
46
  {
 
47
    uint64_t dec;
 
48
    char ans[65],*ptr;
 
49
    /* Return hex of unsigned int64_t value */
 
50
    if (args[0]->result_type() == REAL_RESULT ||
 
51
        args[0]->result_type() == DECIMAL_RESULT)
 
52
    {
 
53
      double val= args[0]->val_real();
 
54
      if ((val <= (double) INT64_MIN) ||
 
55
          (val >= (double) (uint64_t) UINT64_MAX))
 
56
        dec=  ~(int64_t) 0;
 
57
      else
 
58
        dec= (uint64_t) (val + (val > 0 ? 0.5 : -0.5));
 
59
    }
 
60
    else
 
61
      dec= (uint64_t) args[0]->val_int();
 
62
 
 
63
    if ((null_value= args[0]->null_value))
 
64
      return 0;
 
65
    ptr= internal::int64_t2str(dec,ans,16);
 
66
    if (str->copy(ans,(uint32_t) (ptr-ans),default_charset()))
 
67
      return &my_empty_string;                  // End of memory
 
68
    return str;
 
69
  }
 
70
 
 
71
  /* Convert given string to a hex string, character by character */
 
72
  res= args[0]->val_str(str);
 
73
  if (!res || tmp_value.alloc(res->length()*2+1))
 
74
  {
 
75
    null_value=1;
 
76
    return 0;
 
77
  }
 
78
  null_value=0;
 
79
  tmp_value.length(res->length()*2);
 
80
 
 
81
  (void) drizzled_string_to_hex((char*) tmp_value.ptr(), res->ptr(),
 
82
                                res->length());
 
83
  return &tmp_value;
 
84
}
 
85
 
 
86
  /** Convert given hex string to a binary string. */
 
87
 
 
88
String *Item_func_unhex::val_str(String *str)
 
89
{
 
90
  const char *from, *end;
 
91
  char *to;
 
92
  String *res;
 
93
  uint32_t length;
 
94
  assert(fixed == 1);
 
95
 
 
96
  res= args[0]->val_str(str);
 
97
  if (!res || tmp_value.alloc(length= (1+res->length())/2))
 
98
  {
 
99
    null_value=1;
 
100
    return 0;
 
101
  }
 
102
 
 
103
  from= res->ptr();
 
104
  null_value= 0;
 
105
  tmp_value.length(length);
 
106
  to= (char*) tmp_value.ptr();
 
107
  if (res->length() % 2)
 
108
  {
 
109
    int hex_char;
 
110
    *to++= hex_char= hexchar_to_int(*from++);
 
111
    if ((null_value= (hex_char == -1)))
 
112
      return 0;
 
113
  }
 
114
  for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
 
115
  {
 
116
    int hex_char;
 
117
    *to= (hex_char= hexchar_to_int(from[0])) << 4;
 
118
    if ((null_value= (hex_char == -1)))
 
119
      return 0;
 
120
    *to|= hex_char= hexchar_to_int(from[1]);
 
121
    if ((null_value= (hex_char == -1)))
 
122
      return 0;
 
123
  }
 
124
  return &tmp_value;
 
125
}
 
126
 
 
127
} /* namespace drizzled */