blob: a92723d60ddaa59af5c43c0adf2bd0db77ac2f85 [file] [log] [blame]
José Fonsecaa3285532011-11-27 12:32:00 +00001/**************************************************************************
2 *
3 * Copyright 2011 Jose Fonseca
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 **************************************************************************/
25
26
27#include <string.h>
28#include <iostream>
29
30#include "cli.hpp"
31
32#include "trace_file.hpp"
33
34
35static const char *synopsis = "Repack a trace file with Snappy compression.";
36
37static void
38usage(void)
39{
40 std::cout
41 << "usage: apitrace repack <in-trace-file> <out-trace-file>\n"
42 << synopsis << "\n"
43 << "\n"
44 << "Snappy compression allows for faster replay and smaller memory footprint,\n"
45 << "at the expense of a slightly smaller compression ratio than zlib\n"
46 << "\n";
47}
48
49static int
50repack(const char *inFileName, const char *outFileName)
51{
52 trace::File *inFile = trace::File::createForRead(inFileName);
53 if (!inFile) {
54 return 1;
55 }
56
57 trace::File *outFile = trace::File::createForWrite(outFileName);
58 if (!outFile) {
59 delete inFile;
60 return 1;
61 }
62
63 size_t size = 8192;
64 char *buf = new char[size];
65 size_t read;
66
67 while ((read = inFile->read(buf, size)) != 0) {
68 outFile->write(buf, read);
69 }
70
71 delete [] buf;
72 delete outFile;
73 delete inFile;
74
75 return 0;
76}
77
78static int
79command(int argc, char *argv[])
80{
81 int i;
82
José Fonseca54f752c2012-01-31 10:02:52 +000083 for (i = 1; i < argc; ++i) {
José Fonsecaa3285532011-11-27 12:32:00 +000084 const char *arg = argv[i];
85
86 if (arg[0] != '-') {
87 break;
88 }
89
90 if (!strcmp(arg, "--")) {
91 break;
92 } else if (strcmp(arg, "--help") == 0) {
93 usage();
94 return 0;
95 } else {
96 std::cerr << "error: unknown option " << arg << "\n";
97 usage();
98 return 1;
99 }
100 }
101
102 if (argc != i + 2) {
103 std::cerr << "error: insufficient number of arguments\n";
104 usage();
105 return 1;
106 }
107
108 return repack(argv[i], argv[i + 1]);
109}
110
111const Command repack_command = {
112 "repack",
113 synopsis,
114 usage,
115 command
116};