btranslate.cc

00001 /*
00002     Copyright (C) 2005-2008, Net Direct Inc. (http://www.netdirect.ca/)
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00012 
00013     See the GNU General Public License in the COPYING file at the
00014     root directory of this project for more details.
00015 */
00016 
00017 #include <iostream>
00018 #include <iomanip>
00019 #include <sstream>
00020 #include <string.h>
00021 #include <stdlib.h>
00022 #include <limits.h>
00023 
00024 using namespace std;
00025 
00026 bool IsHexData(const char *str)
00027 {
00028         for( int i = 0; i < 4 && *str; str++, i++ )
00029                 if( *str != ' ' )
00030                         return false;
00031 
00032         for( int i = 0; i < 8 && *str; str++, i++ )
00033                 if( !isdigit(*str) && !(*str >= 'a' && *str <= 'f') )
00034                         return false;
00035 
00036         if( *str != ':' )
00037                 return false;
00038 
00039         return true;
00040 }
00041 
00042 void PrintHex(const char *str)
00043 {
00044         cout << setiosflags(ios::left) << setw(14 + 16 * 3 + 1) << str;
00045         cout << setw(0);
00046         str += 14;
00047         char *endpos = (char*) str;
00048         while( *endpos ) {
00049                 long c = strtol(str, &endpos, 16);
00050                 if( c == LONG_MIN || c == LONG_MAX )
00051                         break;
00052                 if( isprint(c) )
00053                         cout << (char)c;
00054                 else
00055                         cout << '.';
00056                 str = endpos;
00057         }
00058         cout << '\n';
00059 }
00060 
00061 int main()
00062 {
00063         cout.sync_with_stdio(false);
00064 
00065         while( cin ) {
00066                 char buff[1024];
00067                 cin.getline(buff, sizeof(buff));
00068                 if( IsHexData(buff) ) {
00069                         // strip whitespace
00070                         size_t sln = strlen(buff);
00071                         while( sln && (buff[sln] == 0 || isspace(buff[sln])) ){
00072                                 buff[sln--] = 0;
00073                         }
00074                         PrintHex(buff);
00075                 }
00076                 else {
00077                         cout << buff << "\n";
00078                 }
00079 
00080                 if( cin.fail() && !cin.eof() ) {
00081                         // getline busted its buffer... discard the
00082                         // rest of the line.
00083                         while( cin.fail() && !cin.eof() ) {
00084                                 cin.clear();
00085                                 cin.getline(buff, sizeof(buff));
00086                         }
00087                 }
00088         }
00089 }
00090 

Generated on Wed Sep 24 21:27:31 2008 for Barry by  doxygen 1.5.1