Drizzled Public API Documentation

convert.cc

00001 /* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include <drizzled/util/convert.h>
00024 #include <string>
00025 #include <iomanip>
00026 #include <sstream>
00027 
00028 using namespace std;
00029 
00030 namespace drizzled
00031 {
00032 
00033 uint64_t drizzled_string_to_hex(char *to, const char *from, uint64_t from_size)
00034 {
00035   static const char hex_map[]= "0123456789ABCDEF";
00036   const char *from_end;
00037 
00038   for (from_end= from + from_size; from != from_end; from++)
00039   {
00040     *to++= hex_map[((unsigned char) *from) >> 4];
00041     *to++= hex_map[((unsigned char) *from) & 0xF];
00042   }
00043 
00044   *to= 0;
00045 
00046   return from_size * 2;
00047 }
00048 
00049 static inline char hex_char_value(char c)
00050 {
00051   if (c >= '0' && c <= '9')
00052     return c - '0';
00053   else if (c >= 'A' && c <= 'F')
00054     return c - 'A' + 10;
00055   else if (c >= 'a' && c <= 'f')
00056     return c - 'a' + 10;
00057   return 0;
00058 }
00059 
00060 void drizzled_hex_to_string(char *to, const char *from, uint64_t from_size)
00061 {
00062   const char *from_end = from + from_size;
00063 
00064   while (from != from_end)
00065   {
00066     *to= hex_char_value(*from++) << 4;
00067     if (from != from_end)
00068       *to++|= hex_char_value(*from++);
00069   }
00070 }
00071 
00072 void bytesToHexdumpFormat(string &to, const unsigned char *from, size_t from_length)
00073 {
00074   static const char hex_map[]= "0123456789abcdef";
00075   unsigned int x, y;
00076   ostringstream line_number;
00077 
00078   for (x= 0; x < from_length; x+= 16)
00079   {
00080     line_number << setfill('0') << setw(6);
00081     line_number << x;
00082     to.append(line_number.str());
00083     to.append(": ", 2);
00084 
00085     for (y= 0; y < 16; y++)
00086     {
00087       if ((x + y) < from_length)
00088       {
00089         to.push_back(hex_map[(from[x+y]) >> 4]);
00090         to.push_back(hex_map[(from[x+y]) & 0xF]);
00091         to.push_back(' ');
00092       }
00093       else
00094         to.append("   ");
00095     }
00096     to.push_back(' ');
00097     for (y= 0; y < 16; y++)
00098     {
00099       if ((x + y) < from_length)
00100         to.push_back(isprint(from[x + y]) ? from[x + y] : '.');
00101     }
00102     to.push_back('\n');
00103     line_number.str("");
00104   }
00105 }
00106 
00107 } /* namespace drizzled */