~drizzle-trunk/drizzle/development

971.3.10 by Eric Day
Added new convert file for hex function.
1
/* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2009 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; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
21
#include <drizzled/util/convert.h>
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
22
#include <string>
23
#include <iomanip>
24
#include <sstream>
25
26
using namespace std;
971.3.10 by Eric Day
Added new convert file for hex function.
27
28
uint64_t drizzled_string_to_hex(char *to, const char *from, uint64_t from_size)
29
{
30
  static const char hex_map[]= "0123456789ABCDEF";
31
  const char *from_end;
32
33
  for (from_end= from + from_size; from != from_end; from++)
34
  {
35
    *to++= hex_map[((unsigned char) *from) >> 4];
36
    *to++= hex_map[((unsigned char) *from) & 0xF];
37
  }
38
39
  *to= 0;
40
41
  return from_size * 2;
42
}
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
43
44
void bytesToHexdumpFormat(string &to, const unsigned char *from, size_t from_length)
45
{
46
  static const char hex_map[]= "0123456789abcdef";
47
  unsigned int x, y;
48
  ostringstream line_number;
49
50
  for (x= 0; x < from_length; x+= 16)
51
  {
52
    line_number << setfill('0') << setw(6);
53
    line_number << x;
54
    to.append(line_number.str());
55
    to.append(": ", 2);
56
57
    for (y= 0; y < 16; y++)
58
    {
59
      if ((x + y) < from_length)
60
      {
61
        to.push_back(hex_map[(from[x+y]) >> 4]);
62
        to.push_back(hex_map[(from[x+y]) & 0xF]);
63
        to.push_back(' ');
64
      }
65
      else
66
        to.append("   ");
67
    }
68
    to.push_back(' ');
69
    for (y= 0; y < 16; y++)
70
    {
71
      if ((x + y) < from_length)
72
        to.push_back(isprint(from[x + y]) ? from[x + y] : '.');
73
    }
74
    to.push_back('\n');
75
    line_number.str("");
76
  }
77
}