Mercurial > minori
annotate src/core/byte_stream.cc @ 365:f81bed4e04ac
*: megacommit that probably breaks things
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Wed, 02 Oct 2024 23:06:43 -0400 |
parents | 99c961c91809 |
children |
rev | line source |
---|---|
364
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
1 #include "core/byte_stream.h" |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
2 |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
3 ByteStream::ByteStream(std::uint8_t *bytes, std::size_t size) { |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
4 bytes_ = bytes; |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
5 size_ = size; |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
6 } |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
7 |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
8 void ByteStream::ResetOffset() { |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
9 offset_ = 0; |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
10 } |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
11 |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
12 void ByteStream::SetEndianness(ByteStream::ByteOrder endian) { |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
13 endian_ = endian; |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
14 } |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
15 |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
16 bool ByteStream::ReadString(std::string& str, std::size_t size) { |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
17 if (offset_ + size >= size_) |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
18 return false; |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
19 |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
20 str.assign(reinterpret_cast<const char *>(bytes_ + offset_), size); |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
21 Advance(size); |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
22 return true; |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
23 } |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
24 |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
25 bool ByteStream::Advance(std::size_t amount) { |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
26 if (offset_ + amount >= size_) |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
27 return false; |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
28 |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
29 offset_ += amount; |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
30 return true; |
99c961c91809
core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
31 } |