forked from f4pga/prjxray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segbits_file_reader.cc
67 lines (56 loc) · 1.77 KB
/
segbits_file_reader.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Copyright (C) 2017-2020 The Project X-Ray Authors.
*
* Use of this source code is governed by a ISC-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/ISC
*
* SPDX-License-Identifier: ISC
*/
#include <prjxray/segbits_file_reader.h>
namespace prjxray {
std::unique_ptr<SegbitsFileReader> SegbitsFileReader::InitWithFile(
const std::string& path) {
auto mapped_file = MemoryMappedFile::InitWithFile(path);
if (!mapped_file)
return nullptr;
return std::unique_ptr<SegbitsFileReader>(
new SegbitsFileReader(std::move(mapped_file)));
}
SegbitsFileReader::iterator SegbitsFileReader::begin() {
return iterator(
absl::string_view(static_cast<const char*>(mapped_file_->data()),
mapped_file_->size()));
}
SegbitsFileReader::iterator SegbitsFileReader::end() {
return iterator(absl::string_view());
}
SegbitsFileReader::value_type::value_type(const absl::string_view& view) {
size_t separator_start = view.find_first_of(" \t\n");
if (separator_start == absl::string_view::npos) {
tag_ = view;
bit_ = absl::string_view();
return;
}
size_t bit_start = view.find_first_not_of(" \t", separator_start);
size_t newline = view.find('\n', bit_start);
if (newline == absl::string_view::npos) {
tag_ = view.substr(0, separator_start);
bit_ = view.substr(bit_start);
return;
}
size_t bit_len = newline - bit_start;
tag_ = view.substr(0, separator_start);
bit_ = view.substr(bit_start, bit_len);
return;
}
SegbitsFileReader::iterator& SegbitsFileReader::iterator::operator++() {
size_t newline = view_.find('\n');
if (newline == absl::string_view::npos) {
view_ = absl::string_view();
}
view_.remove_prefix(newline + 1);
value_ = value_type(view_);
return *this;
}
} // namespace prjxray