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 uint64_t drizzled_string_to_hex(char *to, const char *from, uint64_t from_size)
00033 {
00034   static const char* hex_map= "0123456789ABCDEF";
00035   
00036   for (const char *from_end= from + from_size; from != from_end; from++)
00037   {
00038     *to++= hex_map[((unsigned char) *from) >> 4];
00039     *to++= hex_map[((unsigned char) *from) & 0xF];
00040   }
00041 
00042   *to= 0;
00043 
00044   return from_size * 2;
00045 }
00046 
00047 static inline char hex_char_value(char c)
00048 {
00049   if (c >= '0' && c <= '9')
00050     return c - '0';
00051   else if (c >= 'A' && c <= 'F')
00052     return c - 'A' + 10;
00053   else if (c >= 'a' && c <= 'f')
00054     return c - 'a' + 10;
00055   return 0;
00056 }
00057 
00058 void drizzled_hex_to_string(char *to, const char *from, uint64_t from_size)
00059 {
00060   const char *from_end = from + from_size;
00061   while (from != from_end)
00062   {
00063     *to= hex_char_value(*from++) << 4;
00064     if (from != from_end)
00065       *to++|= hex_char_value(*from++);
00066   }
00067 }
00068 
00069 void bytesToHexdumpFormat(string &to, const unsigned char *from, size_t from_length)
00070 {
00071   static const char* hex_map= "0123456789abcdef";
00072   ostringstream line_number;
00073 
00074   for (size_t x= 0; x < from_length; x+= 16)
00075   {
00076     line_number << setfill('0') << setw(6) << x;
00077     to.append(line_number.str());
00078     to.append(": ", 2);
00079 
00080     for (size_t y= 0; y < 16; y++)
00081     {
00082       if (x + y < from_length)
00083       {
00084         to.push_back(hex_map[(from[x+y]) >> 4]);
00085         to.push_back(hex_map[(from[x+y]) & 0xF]);
00086         to.push_back(' ');
00087       }
00088       else
00089         to.append("   ");
00090     }
00091     to.push_back(' ');
00092     for (size_t y= 0; y < 16; y++)
00093     {
00094       if (x + y < from_length)
00095         to.push_back(isprint(from[x + y]) ? from[x + y] : '.');
00096     }
00097     to.push_back('\n');
00098     line_number.str("");
00099   }
00100 }
00101 
00102 } /* namespace drizzled */