16 #ifndef INCLUDE_NN_NLIB_INPUTSTREAM_H_ 17 #define INCLUDE_NN_NLIB_INPUTSTREAM_H_ 49 if (*
this && is_buf_curidx_ == is_buf_endidx_) {
51 return *
this && is_buf_curidx_ == is_buf_endidx_;
59 if (
NLIB_UNLIKELY(is_buf_curidx_ == is_buf_endidx_))
return -1;
61 return is_buf_[is_buf_curidx_++];
67 if (
NLIB_UNLIKELY(is_buf_curidx_ == is_buf_endidx_))
return -1;
69 return is_buf_[is_buf_curidx_];
73 size_t Read(
void* ptr,
size_t nbytes) NLIB_NOEXCEPT {
74 #ifndef NLIB_NONNULL_ENABLED 76 this->SetError(EINVAL);
81 this->SetError(EINVAL);
84 if (is_buf_curidx_ + nbytes <= is_buf_endidx_) {
85 nlib_memcpy(ptr, nbytes, &is_buf_[is_buf_curidx_], nbytes);
86 is_buf_curidx_ += nbytes;
89 return this->Read_(ptr, nbytes);
98 is_buf_ =
static_cast<unsigned char*
>(p);
99 is_bufsize_ =
static_cast<int>(nbytes);
102 if (errno_ == 0) errno_ = e;
106 virtual size_t FillBuffer_(
void* p,
size_t nbytes) NLIB_NOEXCEPT = 0;
107 virtual bool Close_() NLIB_NOEXCEPT = 0;
121 size_t is_buf_curidx_;
122 size_t is_buf_endidx_;
123 unsigned char* is_buf_;
138 char dummy_buf_[256];
140 virtual bool Close_()
NLIB_NOEXCEPT NLIB_OVERRIDE {
return true; }
154 if (cur_ + n > end_) {
156 size_t rem = end_ - cur_;
159 size_t nread = stream->Read(&buf_[rem], N - rem);
161 end_ = &buf_[0] + rem + nread;
162 return (nread + rem >= n);
169 size_t nread = stream->Read(&buf_[0], N);
172 end_ = &buf_[0] + nread;
179 size_t nread = stream->Read(&buf_[0], N);
182 end_ = &buf_[0] + nread;
201 size_t ReadBytes(IS* stream,
void* data,
size_t nbytes)
NLIB_NOEXCEPT {
202 if (cur_ + nbytes <= end_) {
207 uint8_t* p =
static_cast<uint8_t*
>(data);
208 size_t rem = end_ - cur_;
209 nlib_memcpy(static_cast<void*>(p), nbytes, cur_, rem);
210 cur_ = end_ = &buf_[0];
211 size_t nread = stream->Read(p + rem, nbytes - rem);
214 template<
class IS,
class T>
218 size_t nbytes =
sizeof(*data) * count;
219 size_t nread = ReadBytes(stream, data, nbytes);
221 size_t mod = nread %
sizeof(*data);
222 uint8_t* p =
static_cast<uint8_t*
>(
static_cast<void*
>(data));
224 p + nread - mod, mod);
226 return nread /
sizeof(*data);
239 #endif // INCLUDE_NN_NLIB_INPUTSTREAM_H_ #define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
Substitute definitions for the C++11 standard header type_traits. These substitute definitions are us...
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
Class that wraps errno_t. This class improves visual representations in the Visual Studio debugger...
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
A file that contains the configuration information for each development environment.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_STATIC_ASSERT(exp)
Defines a static assertion. Uses static_assert if it is available for use.
bool Read(BinaryReader *r, T *x)
You can read to user-defined class objects by specializing this function template.