TODO update
[barry.git] / tools / translate.cc
blobb6ceb0d6f664b6702706f2a961e2d10759e91f43
1 /*
2 Copyright (C) 2005-2007, 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)
42 ios::fmtflags flags = cout.setf(ios::right);
43 streamsize width = cout.width(14 + 16 * 3 + 1);
44 cout.flags(flags);
45 cout.width(width);
48 cout << setiosflags(ios::left) << setw(14 + 16 * 3 + 1) << str;
49 cout << setw(0);
50 str += 14;
51 char *endpos = (char*) str;
52 while( *endpos ) {
53 int c = (int) strtol(str, &endpos, 16);
54 if( c == LONG_MIN || c == LONG_MAX )
55 break;
56 if( isprint(c) )
57 cout << (char)c;
58 else
59 cout << '.';
60 str = endpos;
62 cout << '\n';
65 cout << str << ' ';
66 istringstream iss(str+14);
67 iss.setf(ios::hex);
68 cout.setf(ios::hex);
69 cout.width(2);
70 cout.fill('0');
71 while( iss ) {
72 unsigned int val;
73 iss >> val;
74 cout << val << ' ';
76 cout << '\n';
80 int main()
82 cout.sync_with_stdio(false);
84 while( cin ) {
85 char buff[1024];
86 cin.getline(buff, sizeof(buff));
87 if( IsHexData(buff) ) {
88 // strip whitespace
89 int sln = strlen(buff);
90 while( sln && (buff[sln] == 0 || isspace(buff[sln])) ){
91 buff[sln--] = 0;
93 PrintHex(buff);
95 else {
96 cout << buff << "\n";
99 if( cin.fail() && !cin.eof() ) {
100 // getline busted its buffer... discard the
101 // rest of the line.
102 while( cin.fail() && !cin.eof() ) {
103 cin.clear();
104 cin.getline(buff, sizeof(buff));