1/*
2 * Copyright (c) 2012 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Andreas Hansson
38 */
39
40
41/**
42 * @file
43 * Declaration of a wrapper for protobuf output streams and input streams.
44 */
45
46#ifndef __PROTO_PROTOIO_HH__
47#define __PROTO_PROTOIO_HH__
48
49#include <google/protobuf/io/coded_stream.h>
50#include <google/protobuf/io/gzip_stream.h>
51#include <google/protobuf/io/zero_copy_stream_impl.h>
52#include <google/protobuf/message.h>
53
54#include <fstream>
55
56/**
57 * A ProtoStream provides the shared functionality of the input and
58 * output streams. At the moment this is limited to magic number.
59 */
60class ProtoStream
61{
62
63  protected:
64
65    /// Use the ASCII characters gem5 as our magic number
66    static const uint32_t magicNumber = 0x356d6567;
67
68    /**
69     * Create a ProtoStream.
70     */
71    ProtoStream() {}
72
73  private:
74
75    /**
76     * Hide the copy constructor and assignment operator.
77     * @{
78     */
79    ProtoStream(const ProtoStream&);
80    ProtoStream& operator=(const ProtoStream&);
81    /** @} */
82};
83
84/**
85 * A ProtoOutputStream wraps a coded stream, potentially with
86 * compression, based on looking at the file name. Writing to the
87 * stream is done to enable interaction with the file on a per-message
88 * basis to avoid having to deal with huge data structures. The latter
89 * is made possible by encoding the length of each message in the
90 * stream.
91 */
92class ProtoOutputStream : public ProtoStream
93{
94
95  public:
96
97    /**
98     * Create an output stream for a given file name. If the filename
99     * ends with .gz then the file will be compressed accordinly.
100     *
101     * @param filename Path to the file to create or truncate
102     */
103    ProtoOutputStream(const std::string& filename);
104
105    /**
106     * Destruct the output stream, and also flush and close the
107     * underlying file streams and coded streams.
108     */
109    ~ProtoOutputStream();
110
111    /**
112     * Write a message to the stream, preprending it with the message
113     * size.
114     *
115     * @param msg Message to write to the stream
116     */
117    void write(const google::protobuf::Message& msg);
118
119  private:
120
121    /// Underlying file output stream
122    std::ofstream fileStream;
123
124    /// Zero Copy stream wrapping the STL output stream
125    google::protobuf::io::OstreamOutputStream* wrappedFileStream;
126
127    /// Optional Gzip stream to wrap the Zero Copy stream
128    google::protobuf::io::GzipOutputStream* gzipStream;
129
130    /// Top-level zero-copy stream, either with compression or not
131    google::protobuf::io::ZeroCopyOutputStream* zeroCopyStream;
132
133};
134
135/**
136 * A ProtoInputStream wraps a coded stream, potentially with
137 * decompression, based on looking at the file name. Reading from the
138 * stream is done on a per-message basis to avoid having to deal with
139 * huge data structures. The latter assumes the length of each message
140 * is encoded in the stream when it is written.
141 */
142class ProtoInputStream : public ProtoStream
143{
144
145  public:
146
147    /**
148     * Create an input stream for a given file name. If the filename
149     * ends with .gz then the file will be decompressed accordingly.
150     *
151     * @param filename Path to the file to read from
152     */
153    ProtoInputStream(const std::string& filename);
154
155    /**
156     * Destruct the input stream, and also close the underlying file
157     * streams and coded streams.
158     */
159    ~ProtoInputStream();
160
161    /**
162     * Read a message from the stream.
163     *
164     * @param msg Message read from the stream
165     * @param return True if a message was read, false if reading fails
166     */
167    bool read(google::protobuf::Message& msg);
168
169    /**
170     * Reset the input stream and seek to the beginning of the file.
171     */
172    void reset();
173
174  private:
175
176    /**
177     * Create the internal streams that are wrapping the input file.
178     */
179    void createStreams();
180
181    /**
182     * Destroy the internal streams that are wrapping the input file.
183     */
184    void destroyStreams();
185
186    /// Underlying file input stream
187    std::ifstream fileStream;
188
189    /// Hold on to the file name for debug messages
190    const std::string fileName;
191
192    /// Boolean flag to remember whether we use gzip or not
193    bool useGzip;
194
195    /// Zero Copy stream wrapping the STL input stream
196    google::protobuf::io::IstreamInputStream* wrappedFileStream;
197
198    /// Optional Gzip stream to wrap the Zero Copy stream
199    google::protobuf::io::GzipInputStream* gzipStream;
200
201    /// Top-level zero-copy stream, either with compression or not
202    google::protobuf::io::ZeroCopyInputStream* zeroCopyStream;
203
204};
205
206#endif //__PROTO_PROTOIO_HH
207