# btranslate.cc

    00001 /*
    00002     Copyright (C) 2005-2011, 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 #include "i18n.h"
    00024
    00025 using namespace std;
    00026
    00027 bool IsHexData(const char *str)
    00028 {
    00029         for( int i = 0; i < 4 && *str; str++, i++ )
    00030                 if( *str != ' ' )
    00031                         return false;
    00032
    00033         for( int i = 0; i < 8 && *str; str++, i++ )
    00034                 if( !isdigit(*str) && !(*str >= 'a' && *str <= 'f') )
    00035                         return false;
    00036
    00037         if( *str != ':' )
    00038                 return false;
    00039
    00040         return true;
    00041 }
    00042
    00043 void PrintHex(const char *str)
    00044 {
    00045         cout << setiosflags(ios::left) << setw(14 + 16 * 3 + 1) << str;
    00046         cout << setw(0);
    00047         str += 14;
    00048         char *endpos = (char*) str;
    00049         while( *endpos ) {
    00050                 long c = strtol(str, &endpos, 16);
    00051                 if( c == LONG_MIN || c == LONG_MAX )
    00052                         break;
    00053                 if( isprint(c) )
    00054                         cout << (char)c;
    00055                 else
    00056                         cout << '.';
    00057                 str = endpos;
    00058         }
    00059         cout << '\n';
    00060 }
    00061
    00062 int main()
    00063 {
    00064         INIT_I18N(PACKAGE);
    00065
    00066         cout.sync_with_stdio(false);
    00067
    00068         while( cin ) {
    00069                 char buff[1024];
    00070                 cin.getline(buff, sizeof(buff));
    00071                 if( IsHexData(buff) ) {
    00072                         // strip whitespace
    00073                         size_t sln = strlen(buff);
    00074                         while( sln && (buff[sln] == 0 || isspace(buff[sln])) ){
    00075                                 buff[sln--] = 0;
    00076                         }
    00077                         PrintHex(buff);
    00078                 }
    00079                 else {
    00080                         cout << buff << "\n";
    00081                 }
    00082
    00083                 if( cin.fail() && !cin.eof() ) {
    00084                         // getline busted its buffer... discard the
    00085                         // rest of the line.
    00086                         while( cin.fail() && !cin.eof() ) {
    00087                                 cin.clear();
    00088                                 cin.getline(buff, sizeof(buff));
    00089                         }
    00090                 }
    00091         }
    00092 }
    00093