tzwrapper.cc: fixed use of iterator after erase
[barry.git] / tools / btranslate.cc
blob92cd58f73d853a8f76ea514d534b1d5ea2ffec5d
1 /*
2 Copyright (C) 2005-2013, 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>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <limits.h>
23 #include "i18n.h"
25 using namespace std;
27 bool IsHexData(const char *str)
29 for( int i = 0; i < 4 && *str; str++, i++ )
30 if( *str != ' ' )
31 return false;
33 for( int i = 0; i < 8 && *str; str++, i++ )
34 if( !isdigit(*str) && !(*str >= 'a' && *str <= 'f') )
35 return false;
37 if( *str != ':' )
38 return false;
40 return true;
43 void PrintHex(const char *str)
45 cout << setiosflags(ios::left) << setw(14 + 16 * 3 + 1) << str;
46 cout << setw(0);
47 str += 14;
48 char *endpos = (char*) str;
49 while( *endpos ) {
50 long c = strtol(str, &endpos, 16);
51 if( c == LONG_MIN || c == LONG_MAX )
52 break;
53 if( isprint(c) )
54 cout << (char)c;
55 else
56 cout << '.';
57 str = endpos;
59 cout << '\n';
62 int main()
64 INIT_I18N(PACKAGE);
66 cout.sync_with_stdio(false);
68 while( cin ) {
69 char buff[1024];
70 cin.getline(buff, sizeof(buff));
71 if( IsHexData(buff) ) {
72 // strip whitespace
73 size_t sln = strlen(buff);
74 while( sln && (buff[sln] == 0 || isspace(buff[sln])) ){
75 buff[sln--] = 0;
77 PrintHex(buff);
79 else {
80 cout << buff << "\n";
83 if( cin.fail() && !cin.eof() ) {
84 // getline busted its buffer... discard the
85 // rest of the line.
86 while( cin.fail() && !cin.eof() ) {
87 cin.clear();
88 cin.getline(buff, sizeof(buff));