3 #ifndef INCLUDE_NN_NLIB_TEXTREADER_H_
4 #define INCLUDE_NN_NLIB_TEXTREADER_H_
26 if (m_Cur == m_BufEnd) {
28 if (m_Cur == m_BufEnd)
return -1;
30 int rval = *
reinterpret_cast<unsigned char*
>(m_Cur);
34 m_BegLinePos = m_BasePos + (m_Cur - &m_Buf[0]);
39 if (!m_Utf32Cache) ConstructUtf32Cache();
41 rval =
static_cast<int>(m_Utf32Cache);
46 if (m_Cur == m_BufEnd) {
48 if (m_Cur == m_BufEnd)
return -1;
50 int rval = *
reinterpret_cast<unsigned char*
>(m_Cur);
51 if (!(rval & 0x80))
return rval;
52 if (!m_Utf32Cache) ConstructUtf32Cache();
53 return static_cast<int>(m_Utf32Cache);
57 if (m_Cur + 1 < m_BufEnd) {
58 unsigned char c = *
reinterpret_cast<unsigned char*
>(m_Cur);
59 if (c >
' ')
return 0;
62 c = *
reinterpret_cast<unsigned char*
>(m_Cur);
67 return this->SkipWs_(1);
70 return this->SkipWs_(0);
75 return this->ReadUntil(len, &buf[0], N, delim);
78 bool ReadUntil(
size_t* len,
char* buf,
size_t n, T pred)
NLIB_NOEXCEPT;
79 template <
class T,
size_t N>
80 bool ReadUntil(
size_t* len,
char (&buf)[N], T pred) NLIB_NOEXCEPT {
81 return this->ReadUntil(len, buf, N, pred);
83 size_t ReadDecimalString(
char* buf,
size_t n) NLIB_NOEXCEPT
NLIB_NONNULL;
86 return this->ReadDecimalString(buf, N);
88 bool Proceed(
const char* str,
size_t n) NLIB_NOEXCEPT
NLIB_NONNULL;
90 NLIB_ASSERT(!(c & 0x80) && c != 0x0A);
91 if (m_Cur == m_BufEnd) {
93 if (m_Cur == m_BufEnd)
return false;
95 if (*m_Cur != c)
return false;
101 int ReadAsUtf8(
char* b0,
char* b1,
char* b2,
char* b3) NLIB_NOEXCEPT {
102 if (m_Cur == m_BufEnd) {
104 if (m_Cur == m_BufEnd)
return 0;
112 m_BegLinePos = m_BasePos + (m_Cur - &m_Buf[0]);
138 int utf32 = this->
Read();
142 int utf32 = this->Peek();
145 bool Close() NLIB_NOEXCEPT;
146 void SetError(
errno_t e) const NLIB_NOEXCEPT {
147 if (m_ErrorValue == 0) m_ErrorValue = e;
151 int GetLine() const NLIB_NOEXCEPT {
return m_Line; }
153 return static_cast<int>((m_Cur - &m_Buf[0]) + 1 + m_BasePos - m_BegLinePos);
158 char* GetCur() NLIB_NOEXCEPT {
return m_Cur; }
159 char* GetBufEnd() NLIB_NOEXCEPT {
return m_BufEnd; }
160 void SetBufEnd(
char* p) NLIB_NOEXCEPT { m_BufEnd = p; }
163 virtual void FillBuffer_() NLIB_NOEXCEPT;
166 void FillBuffer() NLIB_NOEXCEPT { this->FillBuffer_(); }
168 void ConstructUtf32Cache() NLIB_NOEXCEPT;
169 int SkipWs_(
int base) NLIB_NOEXCEPT;
172 char m_Buf[512 + 3 + 1];
177 InputStream* m_Stream;
187 bool TextReader::ReadUntil(
size_t* len,
char* buf,
size_t n, T pred) NLIB_NOEXCEPT {
195 char* pend = buf + n;
197 if (m_Cur == m_BufEnd) {
199 if (m_Cur == m_BufEnd) {
205 while (p != pend && m_Cur != m_BufEnd) {
206 if ((*reinterpret_cast<unsigned char*>(m_Cur) & 0xC0) != 0x80 &&
207 pred((
const char*)m_Cur)) {
216 if (m_Cur != m_BufEnd && (*reinterpret_cast<unsigned char*>(m_Cur) & 0xC0) == 0x80) {
220 }
while ((*reinterpret_cast<unsigned char*>(m_Cur) & 0xC0) == 0x80);
230 #endif // INCLUDE_NN_NLIB_TEXTREADER_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
bool ReadUntil(size_t *len, char(&buf)[N], char delim) noexcept
Calls ReadUntil(len, buf, N, delim).
int PeekAsUtf16(nlib_utf16_t *upper, nlib_utf16_t *lower) noexcept
Stores the one code point from the start of the stream as UTF-16 in upper and lower.
errno_t GetErrorValue() const noexcept
This function can get the cause of the error when reading has failed.
#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...
int Read() noexcept
Reads one character from the stream and returns UTF-32 data.
InputStream * GetStream() noexcept
Gets the stream for the text reader to read.
size_t ReadDecimalString(char(&buf)[N]) noexcept
Calls ReadDecimalString(buf, N).
bool Proceed(char c) noexcept
Advances the stream by the amount of the character specified by c.
The class for reading text from streams.
A file that contains the configuration information for each development environment.
int ReadAsUtf16(nlib_utf16_t *upper, nlib_utf16_t *lower) noexcept
Reads one code point from the stream and stores it as UTF-16 in upper and lower.
int GetLine() const noexcept
Gets the current line number.
int ReadAsUtf8(char *b0, char *b1, char *b2, char *b3) noexcept
Reads one code point from the stream and stores it as UTF-8 in b0, b1, b2, and b3.
bool ReadUntil(size_t *len, char(&buf)[N], T pred) noexcept
Calls ReadUntil(len, buf, N, pred).
int GetColumn() const noexcept
Gets the current column.
int SkipWs() noexcept
Skips white-space characters (space, newline, tab, and return) in the stream and returns the number t...
bool Read(BinaryReader *r, T *x)
You can read to user-defined class objects by specializing this function template.
int Peek() noexcept
Returns one character from the start of the stream in UTF-32.