proto_stream.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 namespace cartographer {
20 namespace io {
21 
22 namespace {
23 
24 // First eight bytes to identify our proto stream format.
25 const size_t kMagic = 0x7b1d1f7b5bf501db;
26 
27 void WriteSizeAsLittleEndian(size_t size, std::ostream* out) {
28  for (int i = 0; i != 8; ++i) {
29  out->put(size & 0xff);
30  size >>= 8;
31  }
32 }
33 
34 bool ReadSizeAsLittleEndian(std::istream* in, size_t* size) {
35  *size = 0;
36  for (int i = 0; i != 8; ++i) {
37  *size >>= 8;
38  *size += static_cast<size_t>(in->get()) << 56;
39  }
40  return !in->fail();
41 }
42 
43 } // namespace
44 
45 ProtoStreamWriter::ProtoStreamWriter(const string& filename)
46  : out_(filename, std::ios::out | std::ios::binary) {
47  WriteSizeAsLittleEndian(kMagic, &out_);
48 }
49 
51 
52 void ProtoStreamWriter::Write(const string& uncompressed_data) {
53  string compressed_data;
54  common::FastGzipString(uncompressed_data, &compressed_data);
55  WriteSizeAsLittleEndian(compressed_data.size(), &out_);
56  out_.write(compressed_data.data(), compressed_data.size());
57 }
58 
60  out_.close();
61  return !out_.fail();
62 }
63 
64 ProtoStreamReader::ProtoStreamReader(const string& filename)
65  : in_(filename, std::ios::in | std::ios::binary) {
66  size_t magic;
67  if (!ReadSizeAsLittleEndian(&in_, &magic) || magic != kMagic) {
68  in_.setstate(std::ios::failbit);
69  }
70 }
71 
73 
74 bool ProtoStreamReader::Read(string* decompressed_data) {
75  size_t compressed_size;
76  if (!ReadSizeAsLittleEndian(&in_, &compressed_size)) {
77  return false;
78  }
79  string compressed_data(compressed_size, '\0');
80  if (!in_.read(&compressed_data.front(), compressed_size)) {
81  return false;
82  }
83  common::FastGunzipString(compressed_data, decompressed_data);
84  return true;
85 }
86 
87 } // namespace io
88 } // namespace cartographer
ProtoStreamWriter(const string &filename)
Definition: proto_stream.cc:45
void FastGzipString(const string &uncompressed, string *compressed)
Definition: port.h:50
void FastGunzipString(const string &compressed, string *decompressed)
Definition: port.h:60
bool Read(string *decompressed_data)
Definition: proto_stream.cc:74
ProtoStreamReader(const string &filename)
Definition: proto_stream.cc:64
void Write(const string &uncompressed_data)
Definition: proto_stream.cc:52


cartographer
Author(s):
autogenerated on Mon Jun 10 2019 12:51:39