drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
convert.cc
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, 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; 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 <config.h>
22
23#include <drizzled/util/convert.h>
24#include <string>
25#include <iomanip>
26#include <sstream>
27
28using namespace std;
29
30namespace drizzled {
31
32uint64_t drizzled_string_to_hex(char *to, const char *from, uint64_t from_size)
33{
34 static const char* hex_map= "0123456789ABCDEF";
35
36 for (const char *from_end= from + from_size; from != from_end; from++)
37 {
38 *to++= hex_map[((unsigned char) *from) >> 4];
39 *to++= hex_map[((unsigned char) *from) & 0xF];
40 }
41
42 *to= 0;
43
44 return from_size * 2;
45}
46
47static inline char hex_char_value(char c)
48{
49 if (c >= '0' && c <= '9')
50 return c - '0';
51 else if (c >= 'A' && c <= 'F')
52 return c - 'A' + 10;
53 else if (c >= 'a' && c <= 'f')
54 return c - 'a' + 10;
55 return 0;
56}
57
58void drizzled_hex_to_string(char *to, const char *from, uint64_t from_size)
59{
60 const char *from_end = from + from_size;
61 while (from != from_end)
62 {
63 *to= hex_char_value(*from++) << 4;
64 if (from != from_end)
65 *to++|= hex_char_value(*from++);
66 }
67}
68
69void bytesToHexdumpFormat(string &to, const unsigned char *from, size_t from_length)
70{
71 static const char* hex_map= "0123456789abcdef";
72 ostringstream line_number;
73
74 for (size_t x= 0; x < from_length; x+= 16)
75 {
76 line_number << setfill('0') << setw(6) << x;
77 to.append(line_number.str());
78 to.append(": ", 2);
79
80 for (size_t y= 0; y < 16; y++)
81 {
82 if (x + y < from_length)
83 {
84 to.push_back(hex_map[(from[x+y]) >> 4]);
85 to.push_back(hex_map[(from[x+y]) & 0xF]);
86 to.push_back(' ');
87 }
88 else
89 to.append(" ");
90 }
91 to.push_back(' ');
92 for (size_t y= 0; y < 16; y++)
93 {
94 if (x + y < from_length)
95 to.push_back(isprint(from[x + y]) ? from[x + y] : '.');
96 }
97 to.push_back('\n');
98 line_number.str("");
99 }
100}
101
102} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.
STL namespace.