~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/util/convert.cc

  • Committer: Monty Taylor
  • Date: 2008-09-16 00:00:48 UTC
  • mto: This revision was merged to the branch mainline in revision 391.
  • Revision ID: monty@inaugust.com-20080916000048-3rvrv3gv9l0ad3gs
Fixed copyright headers in drizzled/

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) 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
 
 
28
 
using namespace std;
29
 
 
30
 
namespace drizzled
31
 
{
32
 
 
33
 
uint64_t drizzled_string_to_hex(char *to, const char *from, uint64_t from_size)
34
 
{
35
 
  static const char hex_map[]= "0123456789ABCDEF";
36
 
  const char *from_end;
37
 
 
38
 
  for (from_end= from + from_size; from != from_end; from++)
39
 
  {
40
 
    *to++= hex_map[((unsigned char) *from) >> 4];
41
 
    *to++= hex_map[((unsigned char) *from) & 0xF];
42
 
  }
43
 
 
44
 
  *to= 0;
45
 
 
46
 
  return from_size * 2;
47
 
}
48
 
 
49
 
static inline char hex_char_value(char c)
50
 
{
51
 
  if (c >= '0' && c <= '9')
52
 
    return c - '0';
53
 
  else if (c >= 'A' && c <= 'F')
54
 
    return c - 'A' + 10;
55
 
  else if (c >= 'a' && c <= 'f')
56
 
    return c - 'a' + 10;
57
 
  return 0;
58
 
}
59
 
 
60
 
void drizzled_hex_to_string(char *to, const char *from, uint64_t from_size)
61
 
{
62
 
  const char *from_end = from + from_size;
63
 
 
64
 
  while (from != from_end)
65
 
  {
66
 
    *to= hex_char_value(*from++) << 4;
67
 
    if (from != from_end)
68
 
      *to++|= hex_char_value(*from++);
69
 
  }
70
 
}
71
 
 
72
 
void bytesToHexdumpFormat(string &to, const unsigned char *from, size_t from_length)
73
 
{
74
 
  static const char hex_map[]= "0123456789abcdef";
75
 
  unsigned int x, y;
76
 
  ostringstream line_number;
77
 
 
78
 
  for (x= 0; x < from_length; x+= 16)
79
 
  {
80
 
    line_number << setfill('0') << setw(6);
81
 
    line_number << x;
82
 
    to.append(line_number.str());
83
 
    to.append(": ", 2);
84
 
 
85
 
    for (y= 0; y < 16; y++)
86
 
    {
87
 
      if ((x + y) < from_length)
88
 
      {
89
 
        to.push_back(hex_map[(from[x+y]) >> 4]);
90
 
        to.push_back(hex_map[(from[x+y]) & 0xF]);
91
 
        to.push_back(' ');
92
 
      }
93
 
      else
94
 
        to.append("   ");
95
 
    }
96
 
    to.push_back(' ');
97
 
    for (y= 0; y < 16; y++)
98
 
    {
99
 
      if ((x + y) < from_length)
100
 
        to.push_back(isprint(from[x + y]) ? from[x + y] : '.');
101
 
    }
102
 
    to.push_back('\n');
103
 
    line_number.str("");
104
 
  }
105
 
}
106
 
 
107
 
} /* namespace drizzled */