src/wscout_gui_hex_dump_dock.hxx

00001 /*
00002  * WScout - Lightweight PCAP visualizer.
00003  * Copyright (C) 2007, 2008  Universite Pierre et Marie Curie - Paris 6
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018  * MA  02110-1301  USA
00019  *
00020  * Author: Thomas Claveirole <thomas.claveirole@lip6.fr>
00021  */
00022 
00023 #ifndef WSCOUT_GUI_HEX_DUMP_DOCK_HXX_
00024 # define WSCOUT_GUI_HEX_DUMP_DOCK_HXX_
00025 
00026 # include <sstream>
00027 # include <iomanip>
00028 # include <locale>
00029 
00030 # include "wscout_gui_hex_dump_dock.hh"
00031 
00032 namespace wscout
00033 {
00034 
00035   namespace gui
00036   {
00037 
00038     inline
00039     QString
00040     HexDumpDock::filterBlanksOut(const QString& str)
00041     {
00042       QString   r;
00043 
00044       for (int i = 0; i < str.size(); ++i)
00045         if (not str[i].isSpace())
00046           r.push_back(str[i]);
00047       return r;
00048     }
00049 
00050     inline
00051     void
00052     HexDumpDock::setup(const void* data, size_t len)
00053     {
00054       const unsigned char* const  b = static_cast<const unsigned char*> (data);
00055       const unsigned char* const  e = b + len;
00056 
00057       std::ostringstream        addresses;
00058       std::ostringstream        hex;
00059       std::ostringstream        ascii;
00060 
00061       addresses << std::hex << std::uppercase;
00062       hex << std::hex << std::uppercase;
00063       for (const unsigned char* c = b; c < e; c += lwidth)
00064         {
00065           if (c != b)
00066             {
00067               hex << '\n';
00068               ascii << '\n';
00069               addresses << '\n';
00070             }
00071           addresses << std::setfill ('0')
00072                     << std::setw (addr_len * 2)
00073                     << (c - b);
00074           for (unsigned i = 0; i < lwidth and c + i < e; i += bwidth)
00075             {
00076               if (i)
00077                 hex << bigsep;
00078               for (unsigned j = 0; j < bwidth and c + i + j < e; ++j)
00079                 {
00080                   if (j)
00081                     hex << smallsep;
00082                   hex << std::setfill ('0') << std::setw (2)
00083                       << unsigned (c[i + j]);
00084                   ascii << (std::isprint(c[i + j]) ? char (c[i + j]) : '.');
00085                 }
00086             }
00087         }
00088 
00089       addresses_.setPlainText(addresses.str().c_str());
00090       hexData_.setPlainText(hex.str().c_str());
00091       asciiData_.setPlainText(ascii.str().c_str());
00092     }
00093 
00094   } // End of namespace gui.
00095 
00096 } // End of namespace wscout.
00097 
00098 #endif // ! WSCOUT_GUI_HEX_DUMP_DOCK_HXX_

Generated on Wed Jan 30 19:02:27 2008 for WScout by  doxygen 1.5.4