rpm: removed opensuse special cases
[barry.git] / tools / brimtrans.cc
blobedbc94f580bfecf9dd7c07ee5976d83d539a5daa
1 ///
2 /// \file brimtrans.cc
3 /// Translates log files generated by RIM's USB driver
4 /// into something more or less readable.
5 ///
7 /*
8 Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
9 Copyright (C) 2009, Josh Kropf
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 See the GNU General Public License in the COPYING file at the
21 root directory of this project for more details.
24 #include <iostream>
25 #include <iomanip>
26 #include <sstream>
27 #include <string>
28 #include <vector>
29 #include <list>
30 #include <cctype>
31 #include <stdint.h>
32 #include "i18n.h"
34 using namespace std;
36 string trim_right(const string& source , const string& t = " ");
37 string trim_data_line(const string& line);
38 void dump(const list<string>& event);
39 void parse_hex(const string& source, vector<uint16_t>& data);
40 void print_hex(const vector<uint16_t>& data);
42 int main(int argc, char* argv[])
44 list<string>* event = NULL;
45 string line;
47 INIT_I18N(PACKAGE);
49 while( !getline(cin, line).eof() ) {
50 size_t pos = line.find_first_of(':');
52 // skip lines missing colon
53 if( pos == string::npos )
54 continue;
56 line = trim_right(line.substr(pos + 2), "\n\r ");
58 // each sequence of write/read packets begins with this line
59 if( line == "BbUsbDevice: WriteToDevice" ) {
60 if( event != NULL ) {
61 dump(*event);
62 delete event;
65 event = new list<string>;
67 else if( line.substr(0, 3) == "<-:" ) { // send to device
68 event->push_front(trim_data_line(line.erase(0, 3)));
70 else if( line.substr(0, 3) == "->:" ) { // receive from device
71 event->push_back(trim_data_line(line.erase(0, 3)));
75 if( event != NULL ) {
76 dump(*event);
77 delete event;
80 return 0;
83 string trim_right(const string& source , const string& t)
85 string str = source;
86 return str.erase(str.find_last_not_of(t) + 1);
89 string trim_data_line(const string& line)
91 size_t pos = line.find_first_of(':');
92 return line.substr(pos + 2);
95 void dump(const list<string>& event)
97 vector<uint16_t> data;
98 list<string>::const_iterator i, begin = event.begin(), end = event.end();
99 for( i=begin; i != end; ++i ) {
100 data.clear();
101 parse_hex(*i, data);
102 cout << (i == begin? "Send" : "Receive") << endl;
103 print_hex(data);
105 cout << endl;
108 void parse_hex(const string& source, vector<uint16_t>& data)
110 istringstream ss(source);
111 uint16_t byte;
113 while( !ss.eof() ) {
114 ss >> hex >> byte;
115 data.push_back(byte);
119 void print_hex(const vector<uint16_t>& data)
121 int remaining = data.size(), offset = 0;
122 do {
123 cout << " " << hex << setfill('0') << setw(8) << offset;
124 int margin = 13;
126 for( int i=0, stop=min(16, remaining); i<stop; i++ ) {
127 cout << ' ' << hex << setfill('0') << setw(2) << data[offset + i];
128 margin += 3;
131 cout << string(62-margin, ' ');
133 for( int i=0, stop=min(16, remaining); i<stop; i++) {
134 char c = data[offset + i];
135 cout << (isprint(c)? c : '.');
138 offset += 16;
139 remaining -= 16;
141 cout << endl;
142 } while( remaining > 0 );