3 /// Translates log files generated by RIM's USB driver
4 /// into something more or less readable.
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.
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
;
49 while( !getline(cin
, line
).eof() ) {
50 size_t pos
= line
.find_first_of(':');
52 // skip lines missing colon
53 if( pos
== string::npos
)
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" ) {
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)));
83 string
trim_right(const string
& source
, const string
& t
)
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
) {
102 cout
<< (i
== begin
? "Send" : "Receive") << endl
;
108 void parse_hex(const string
& source
, vector
<uint16_t>& data
)
110 istringstream
ss(source
);
115 data
.push_back(byte
);
119 void print_hex(const vector
<uint16_t>& data
)
121 int remaining
= data
.size(), offset
= 0;
123 cout
<< " " << hex
<< setfill('0') << setw(8) << offset
;
126 for( int i
=0, stop
=min(16, remaining
); i
<stop
; i
++ ) {
127 cout
<< ' ' << hex
<< setfill('0') << setw(2) << data
[offset
+ i
];
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
: '.');
142 } while( remaining
> 0 );