moved the hexdump function to libUtils
[senf.git] / Utils / hexdump.ct
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief hexdump non-inline template implementation */
25
26 //#include "hexdump.ih"
27
28 // Custom includes
29 #include <iomanip> 
30
31 //#include "hexdump.mpp"
32 #define prefix_
33 ///////////////////////////////cc.p////////////////////////////////////////
34
35 template <class Iterator>
36 prefix_ void senf::hexdump(Iterator i, Iterator const & i_end, std::ostream& stream, unsigned block_size=16)
37 {
38     unsigned offset (0);
39     std::string ascii;
40     for (; i != i_end; ++i, ++offset) {
41         switch ((offset % block_size)==0) {
42         case true:
43             if (!ascii.empty()) {
44                 stream << "  " << ascii << "\n";
45                 ascii = "";
46             }
47             stream << "  "
48                       << std::hex << std::setw(4) << std::setfill('0')
49                       << offset << ' ';
50             break;
51         case false:
52             stream << " ";
53             ascii += ' ';
54             break;
55         }
56         stream << ' ' << std::hex << std::setw(2) << std::setfill('0')
57                   << unsigned(*i);
58         ascii += (*i >= ' ' && *i < 126) ? *i : '.';
59     }
60     if (!ascii.empty()) {
61         for (; (offset % block_size) != 0; ++offset) {
62             if ((offset % block_size) == block_size/2)
63                 stream << " ";
64             stream << "   ";
65         }
66         stream << "  " << ascii << "\n";
67     }
68     stream << std::dec;
69 }
70
71 ///////////////////////////////cc.e////////////////////////////////////////
72 #undef prefix_
73 //#include "hexdump.mpp"
74
75 \f
76 // Local Variables:
77 // mode: c++
78 // fill-column: 100
79 // c-file-style: "senf"
80 // indent-tabs-mode: nil
81 // ispell-local-dictionary: "american"
82 // compile-command: "scons -u test"
83 // comment-column: 40
84 // End: