Skip to content

Commit ece98b1

Browse files
committed
Add code to handle display of header info
1 parent d385fc6 commit ece98b1

File tree

2 files changed

+129
-23
lines changed

2 files changed

+129
-23
lines changed

src/app/InfoPrinter.cpp

Lines changed: 126 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License.
1414
*/
1515

16+
#include <cstdlib>
1617
#include <iomanip>
1718
#include <ios>
1819
#include <sstream>
@@ -46,11 +47,18 @@ InfoPrinter::InfoPrinter(int infoLevel, InfoPrinter::Type type, OutputStream& os
4647

4748
for (int i = 0; i < 1024; i++)
4849
_map[i] = nullptr;
50+
51+
_headerInfo = 0;
4952
}
5053

5154
void InfoPrinter::processEvent(const Event& evt)
5255
{
53-
int currentBlockId = evt.getId();
56+
if (_type == InfoPrinter::INFO) {
57+
processHeaderInfo(evt);
58+
return;
59+
}
60+
61+
const int currentBlockId = evt.getId();
5462

5563
if (evt.getType() == _thresholds[1]) {
5664
// Register initial block size
@@ -136,36 +144,131 @@ void InfoPrinter::processEvent(const Event& evt)
136144
delete bi;
137145
_map[hash(currentBlockId)] = nullptr;
138146
}
139-
else if (evt.getType() == Event::AFTER_HEADER_DECODING) {
140-
if (_level >= 3) {
141-
stringstream ss(evt.toString());
142-
string s = ss.str();
143-
vector<string> tokens;
144-
const int nbTokens = tokenizeCSV(s, tokens);
145-
ss.str(string());
146-
147-
if (nbTokens > 1)
148-
ss << "Bitstream version: " << tokens[1] << endl;
147+
else if ((evt.getType() == Event::AFTER_HEADER_DECODING) && (_level >= 3)) {
148+
stringstream ss(evt.toString());
149+
string s = ss.str();
150+
vector<string> tokens;
151+
const int nbTokens = tokenizeCSV(s, tokens);
152+
ss.str(string());
149153

150-
if (nbTokens > 2)
151-
ss << "Block checksum: " << tokens[2] << (tokens[2] == "NONE" ? "" : " bits") << endl;
154+
if (nbTokens > 1)
155+
ss << "Bitstream version: " << tokens[1] << endl;
152156

153-
if (nbTokens > 3)
154-
ss << "Block size: " << tokens[3] << " bytes" << endl;
157+
if (nbTokens > 2)
158+
ss << "Block checksum: " << tokens[2] << (tokens[2] == "NONE" ? "" : " bits") << endl;
155159

156-
if (nbTokens > 4)
157-
ss << "Using " << (tokens[4] == "" ? "no" : tokens[4]) << " entropy codec (stage 1)" << endl;
160+
if (nbTokens > 3)
161+
ss << "Block size: " << tokens[3] << " bytes" << endl;
158162

159-
if (nbTokens > 5)
160-
ss << "Using " << (tokens[5] == "" ? "no" : tokens[5]) << " transform (stage 2)" << endl;
163+
if (nbTokens > 4)
164+
ss << "Using " << (tokens[4] == "" ? "no" : tokens[4]) << " entropy codec (stage 1)" << endl;
161165

162-
if (nbTokens > 7)
163-
ss << "Original size: " << tokens[7] << " byte(s)" << endl;;
166+
if (nbTokens > 5)
167+
ss << "Using " << (tokens[5] == "" ? "no" : tokens[5]) << " transform (stage 2)" << endl;
164168

165-
_os << ss.str() << endl;
166-
}
169+
if (nbTokens > 7)
170+
ss << "Original size: " << tokens[7] << " byte(s)" << endl;;
171+
172+
_os << ss.str() << endl;
167173
}
168174
else if (_level >= 5) {
169175
_os << evt.toString() << endl;
170176
}
171177
}
178+
179+
180+
void InfoPrinter::processHeaderInfo(const Event& evt)
181+
{
182+
if ((_level == 0) || (evt.getType() != Event::AFTER_HEADER_DECODING))
183+
return;
184+
185+
stringstream ss(evt.toString());
186+
string s = ss.str();
187+
vector<string> tokens;
188+
const int nbTokens = tokenizeCSV(s, tokens);
189+
ss.str(string());
190+
191+
if (_headerInfo++ == 0) {
192+
// Display header
193+
ss << "|" << " File Name ";
194+
ss << "|" << "Ver";
195+
ss << "|" << "Check";
196+
ss << "|" << "Block Size";
197+
ss << "|" << " File Size ";
198+
ss << "|" << " Orig. Size ";
199+
ss << "|" << " Ratio ";
200+
201+
if (_level >= 4) {
202+
ss << "|" << " Entropy";
203+
ss << "|" << " Transforms ";
204+
}
205+
206+
ss << "|" << endl;
207+
}
208+
209+
ss << "|";
210+
211+
if (nbTokens > 0) {
212+
string inputName = tokens[0];
213+
size_t idx = inputName.find_last_of(PATH_SEPARATOR);
214+
215+
if (idx != string::npos)
216+
inputName = inputName.substr(idx + 1);
217+
218+
if (inputName.length() > 20)
219+
inputName = inputName.substr(0, 18) + "..";
220+
221+
ss << std::left << setw(20) << inputName << "|" << std::right; // inputName
222+
}
223+
224+
if (nbTokens > 1)
225+
ss << setw(3) << tokens[1] << "|"; //bsVersion
226+
227+
if (nbTokens > 2)
228+
ss << setw(5) << tokens[2] << "|"; // checksum
229+
230+
if (nbTokens > 3)
231+
ss << setw(10) << tokens[3] << "|"; // block size
232+
233+
if (nbTokens > 6)
234+
ss << setw(12) << formatSize(tokens[6]) << "|"; // compressed size
235+
else
236+
ss << setw(12) << " N/A " << "|";
237+
238+
if (nbTokens > 7)
239+
ss << setw(12) << formatSize(tokens[7]) << "|"; // original size
240+
else
241+
ss << setw(12) << " N/A " << "|";
242+
243+
if ((tokens[6] != "") && (tokens[7] != "")) {
244+
string compStr = tokens[6];
245+
string origStr = tokens[7];
246+
double compSz = atof(compStr.c_str());
247+
double origSz = atof(origStr.c_str());
248+
249+
if (origSz == double(0)) {
250+
ss << setw(7) << " N/A " << "|";
251+
}
252+
else {
253+
double ratio = compSz / origSz;
254+
ss << setw(7) << std::fixed << std::setprecision(3) << ratio << "|"; // compression ratio
255+
}
256+
}
257+
else {
258+
ss << setw(7) << " N/A " << "|";
259+
}
260+
261+
if ((_level >= 4) && (nbTokens > 4))
262+
ss << setw(8) << (tokens[4] == "" ? "NONE" : tokens[4]) << "|"; // entropy
263+
264+
if ((_level >= 4) && (nbTokens > 5)) {
265+
string t = tokens[5];
266+
267+
if (t.length() > 22)
268+
t = t.substr(0, 20) + "..";
269+
270+
ss << setw(22) << (t == "" ? "NONE" : t) << "|"; // transforms
271+
}
272+
273+
_os << ss.str() << endl;
274+
}

src/app/InfoPrinter.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ namespace kanzi
5959
Event::Type _thresholds[6];
6060
InfoPrinter::Type _type;
6161
int _level;
62+
int _headerInfo;
6263
Clock _clock12;
6364
Clock _clock23;
6465
Clock _clock34;
6566

67+
void processHeaderInfo(const Event& evt);
68+
6669
static uint hash(uint id) { return (id * 0x1E35A7BD) & 0x03FF; }
6770
};
6871
}

0 commit comments

Comments
 (0)