Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
stream_utils.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
9
11
12namespace momentum {
13
14spanstreambuf::spanstreambuf(std::span<const std::byte> buffer) {
15 if (buffer.empty()) {
16 return;
17 }
18
19 // This is a bit awkward: the stream buffer won't change the data
20 // but the interface still requires char* to be used
21 char* data = reinterpret_cast<char*>(const_cast<std::byte*>(buffer.data()));
22 this->setg(data, data, data + buffer.size());
23}
24
26
27std::streamsize spanstreambuf::xsputn(
28 const spanstreambuf::char_type* /* s */,
29 std::streamsize /* n */) {
30 MT_THROW("spanstreambuf is not writable.");
31}
32
33spanstreambuf::pos_type
34spanstreambuf::seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode which) {
35 if ((which & std::ios_base::in) == 0) {
36 return {off_type(-1)};
37 }
38 char* const beg = eback();
39 char* const cur = gptr();
40 char* const end = egptr();
41 if (beg == nullptr) {
42 // Empty buffer -- only seeking to position 0 is valid.
43 return (off == 0) ? pos_type(0) : pos_type(off_type(-1));
44 }
45 off_type base = 0;
46 // libstdc++ adds the implementation-detail sentinel `_S_ios_seekdir_end` to
47 // the seekdir enum; the `default:` branch handles it (and any future
48 // additions) safely.
49 switch (way) { // NOLINT(clang-diagnostic-switch-enum)
50 case std::ios_base::beg:
51 base = 0;
52 break;
53 case std::ios_base::cur:
54 base = cur - beg;
55 break;
56 case std::ios_base::end:
57 base = end - beg;
58 break;
59 default:
60 return {off_type(-1)};
61 }
62 const off_type target = base + off;
63 if (target < 0 || target > (end - beg)) {
64 return {off_type(-1)};
65 }
66 setg(beg, beg + target, end);
67 return {target};
68}
69
70spanstreambuf::pos_type spanstreambuf::seekpos(pos_type pos, std::ios_base::openmode which) {
71 return seekoff(off_type(pos), std::ios_base::beg, which);
72}
73
74ispanstream::ispanstream(std::span<const std::byte> buffer) : std::istream(&sbuf_), sbuf_(buffer) {
75 // Empty
76}
77
79
80std::istream& GetLineCrossPlatform(std::istream& is, std::string& line) {
81 line.clear();
82 while (is.good()) {
83 const int c = is.get();
84 if (!is.good()) {
85 break;
86 }
87
88 if (c == '\n') {
89 break;
90 }
91
92 if (c == '\r') {
93 const auto next_c = is.peek();
94 if (!is.good()) {
95 break;
96 }
97
98 if (next_c == '\n') {
99 is.get();
100 }
101
102 break;
103 }
104
105 line.push_back(gsl::narrow_cast<char>(c));
106 }
107
108 return is;
109}
110
111} // namespace momentum
#define MT_THROW(...)
Throws a std::runtime_error with a formatted message.
Definition exception.h:31
std::istream & GetLineCrossPlatform(std::istream &is, std::string &line)
Reads a line from the input stream, handling line endings for different platforms.
~ispanstream() override
Destructor.
ispanstream(std::span< const std::byte > buffer)
Construct an ispanstream with a buffer.
std::streamsize xsputn(const char_type *s, std::streamsize n) override
Write a sequence of characters to the output buffer.
~spanstreambuf() override
Destructor.
spanstreambuf(std::span< const std::byte > buffer={})
Construct a spanstreambuf with an optional buffer.
pos_type seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode which=std::ios_base::in) override
Seek to an offset relative to way (begin/cur/end).
pos_type seekpos(pos_type pos, std::ios_base::openmode which=std::ios_base::in) override
Seek to an absolute position from the start of the buffer.