HexDumpBuffer.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #include <ctype.h>
00019 #include "HexDumpBuffer.h"
00020 
00021 namespace oasys {
00022 
00023 void
00024 HexDumpBuffer::hexify()
00025 {
00026     // make a copy of the current data
00027     size_t len = length();
00028     std::string contents(data(), len);
00029     char strbuf[16];
00030 
00031     // rewind the string buffer backwards
00032     trim(length());
00033 
00034     // generate the dump
00035     u_char* bp = (u_char*)contents.data();
00036     for (size_t i = 0; i < len; ++i, ++bp)
00037     {
00038         // print the offset on each new line
00039         if (i % 16 == 0) {
00040             appendf("%07x ", (u_int)i);
00041         }
00042         
00043         // otherwise print a space every two bytes (except the first)
00044         else if (i % 2 == 0) {
00045             append(" ");
00046         }
00047 
00048         // print the hex character
00049         appendf("%02x", *bp);
00050         
00051         // tack on the ascii character if it's printable, '.' otherwise
00052         if (isalnum(*bp) || ispunct(*bp) || (*bp == ' ')) {
00053             strbuf[i % 16] = *bp;
00054         } else {
00055             strbuf[i % 16] = '.';
00056         }
00057         
00058         if (i % 16 == 15)
00059         {
00060             appendf(" |  %.*s\n", 16, strbuf);
00061         }
00062     }
00063 
00064     // deal with the ending partial line
00065     for (size_t i = len % 16; i < 16; ++i) {
00066         if (i % 2 == 0) {
00067             append(" ");
00068         }
00069 
00070         append("  ");
00071     }
00072 
00073     appendf(" |  %.*s\n", (int)len % 16, strbuf);
00074 }
00075 
00076 } // namespace oasys

Generated on Sat Sep 8 08:36:17 2007 for DTN Reference Implementation by  doxygen 1.5.3