Merged barry-b1-socket-arch-branch into MAIN.
[barry.git] / tools / btranslate.cc
blobcd3364ac49a3b9053bd6ec6e5f3c3bdf82004640
1 /*
2 Copyright (C) 2005-2008, Net Direct Inc. (http://www.netdirect.ca/)
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License in the COPYING file at the
14 root directory of this project for more details.
17 #include <iostream>
18 #include <iomanip>
19 #include <sstream>
21 using namespace std;
23 bool IsHexData(const char *str)
25 for( int i = 0; i < 4 && *str; str++, i++ )
26 if( *str != ' ' )
27 return false;
29 for( int i = 0; i < 8 && *str; str++, i++ )
30 if( !isdigit(*str) && !(*str >= 'a' && *str <= 'f') )
31 return false;
33 if( *str != ':' )
34 return false;
36 return true;
39 void PrintHex(const char *str)
41 cout << setiosflags(ios::left) << setw(14 + 16 * 3 + 1) << str;
42 cout << setw(0);
43 str += 14;
44 char *endpos = (char*) str;
45 while( *endpos ) {
46 long c = strtol(str, &endpos, 16);
47 if( c == LONG_MIN || c == LONG_MAX )
48 break;
49 if( isprint(c) )
50 cout << (char)c;
51 else
52 cout << '.';
53 str = endpos;
55 cout << '\n';
58 int main()
60 cout.sync_with_stdio(false);
62 while( cin ) {
63 char buff[1024];
64 cin.getline(buff, sizeof(buff));
65 if( IsHexData(buff) ) {
66 // strip whitespace
67 size_t sln = strlen(buff);
68 while( sln && (buff[sln] == 0 || isspace(buff[sln])) ){
69 buff[sln--] = 0;
71 PrintHex(buff);
73 else {
74 cout << buff << "\n";
77 if( cin.fail() && !cin.eof() ) {
78 // getline busted its buffer... discard the
79 // rest of the line.
80 while( cin.fail() && !cin.eof() ) {
81 cin.clear();
82 cin.getline(buff, sizeof(buff));