Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
fbx_memory_stream.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
8#ifdef MOMENTUM_WITH_FBX_SDK
9
11
12namespace momentum {
13
14FbxMemoryStream::FbxMemoryStream(std::span<const std::byte> buffer, int pReaderId)
15 : buffer_(buffer),
16 length_(buffer.size()),
17 position_(0),
18 state_(EState::eClosed),
19 readerId_(pReaderId) {}
20
21FbxMemoryStream::~FbxMemoryStream() {
22 Close();
23}
24
25FbxStream::EState FbxMemoryStream::GetState() {
26 return state_;
27}
28
29bool FbxMemoryStream::Open(void* /* pStreamData */) {
30 position_ = 0;
31 state_ = EState::eOpen;
32 return true;
33}
34
35bool FbxMemoryStream::Close() {
36 position_ = 0;
37 state_ = EState::eClosed;
38 return true;
39}
40
41bool FbxMemoryStream::Flush() {
42 return true;
43}
44
45#if FBX_VERSION_GE(2020, 3, 2)
46size_t FbxMemoryStream::Write(const void* /* buffer */, FbxUInt64 /* count */) {
47#else
48int FbxMemoryStream::Write(const void* /* buffer */, int /* count */) {
49#endif
50 errorCode_ = 1;
51 return 0;
52}
53
54#if FBX_VERSION_GE(2020, 3, 2)
55size_t FbxMemoryStream::Read(void* buffer, FbxUInt64 count) const {
56#else
57int FbxMemoryStream::Read(void* buffer, int count) const {
58#endif
59 long remaining = length_ - position_;
60 if (count > remaining) {
61 errorCode_ = 1;
62 return 0;
63 }
64
65 memcpy(buffer, buffer_.data() + position_, count);
66 position_ += count;
67 return count;
68}
69
70int FbxMemoryStream::GetReaderID() const {
71 return readerId_;
72}
73
74int FbxMemoryStream::GetWriterID() const {
75 return -1;
76}
77
78void FbxMemoryStream::Seek(const FbxInt64& pOffset, const FbxFile::ESeekPos& pSeekPos) {
79 long offset = (long)pOffset;
80 switch (pSeekPos) {
81 case FbxFile::ESeekPos::eCurrent:
82 position_ += offset;
83 break;
84 case FbxFile::ESeekPos::eEnd:
85 position_ = length_ - offset;
86 break;
87 default:
88 position_ = offset;
89 break;
90 }
91 // Clamp position to valid range to prevent out-of-bounds reads
92 if (position_ < 0) {
93 position_ = 0;
94 } else if (position_ > length_) {
95 position_ = length_;
96 }
97}
98
99#if FBX_VERSION_GE(2020, 3, 2)
100FbxInt64 FbxMemoryStream::GetPosition() const {
101#else
102long FbxMemoryStream::GetPosition() const {
103#endif
104 return position_;
105}
106
107#if FBX_VERSION_GE(2020, 3, 2)
108void FbxMemoryStream::SetPosition(FbxInt64 pPosition) {
109#else
110void FbxMemoryStream::SetPosition(long pPosition) {
111#endif
112 position_ = pPosition;
113}
114
115int FbxMemoryStream::GetError() const {
116 return errorCode_;
117}
118
119void FbxMemoryStream::ClearError() {
120 errorCode_ = 0;
121}
122
123} // namespace momentum
124
125#endif // MOMENTUM_WITH_FBX_SDK