nlib
TextReader.h
Go to the documentation of this file.
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_TEXTREADER_H_
17 #define INCLUDE_NN_NLIB_TEXTREADER_H_
18 
19 #include "nn/nlib/Config.h"
20 
21 NLIB_NAMESPACE_BEGIN
22 
23 class InputStream;
24 
25 // code snippets:
26 // TextReader reader;
27 // if (reader.Init() != 0 || reader.Open(&stream) != 0) { error }
28 // while ((c = reader.Read()) >= 0)
29 // c is a codepoint
30 // if (!reader) { stream error if !stream, otherwise UTF-8 may be malformed }
31 // reader.Close();
32 // (stream.Close();)
34  public:
36  virtual ~TextReader() NLIB_NOEXCEPT;
37  errno_t Init() NLIB_NOEXCEPT;
38  errno_t Open(InputStream* stream) NLIB_NOEXCEPT;
39  size_t Read(nlib_utf8_t* buf, size_t n) NLIB_NOEXCEPT;
40  int Read() NLIB_NOEXCEPT;
41  int Peek() NLIB_NOEXCEPT;
43  // skip ' ' \t \n (\r)
44  if (cur_ + 1 < bufend_) {
45  unsigned char c = *reinterpret_cast<unsigned char*>(cur_);
46  if (c > ' ') return 0;
47  if (c == ' ') {
48  ++cur_;
49  c = *reinterpret_cast<unsigned char*>(cur_);
50  if (c > ' ') {
51  utf32_cache_ = 0;
52  return 1;
53  }
54  return this->SkipWs_(1);
55  }
56  }
57  return this->SkipWs_(0);
58  }
59  bool ReadUntil(size_t* len, nlib_utf8_t* buf, size_t n, char delim) NLIB_NOEXCEPT NLIB_NONNULL;
60  template<size_t N>
61  bool ReadUntil(size_t* len, nlib_utf8_t (&buf)[N], char delim) NLIB_NOEXCEPT {
62  return this->ReadUntil(len, &buf[0], N, delim);
63  }
64  // deprecated
65  template<class T>
66  bool ReadUntil(size_t* len, nlib_utf8_t* buf, size_t n, T pred) NLIB_NOEXCEPT;
67  template<class T, size_t N>
68  NLIB_DEPRECATED bool ReadUntil(size_t* len, nlib_utf8_t (&buf)[N], T pred) NLIB_NOEXCEPT {
69  return this->ReadUntil(len, buf, N, pred);
70  }
71  size_t ReadDecimalString(char* buf, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
72  template<size_t N>
73  size_t ReadDecimalString(char (&buf)[N]) NLIB_NOEXCEPT {
74  return this->ReadDecimalString(buf, N);
75  }
76  bool Proceed(const nlib_utf8_t* str, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
77  bool Proceed(char c) NLIB_NOEXCEPT {
78  NLIB_ASSERT(!(c & 0x80) && c != 0x0A);
79  if (cur_ == bufend_) {
80  this->FillBuffer();
81  if (cur_ == bufend_) return false;
82  }
83  if (*cur_ != c) return false;
84  ++cur_;
85  return true;
86  }
87  bool ProceedEx(const nlib_utf8_t* str) NLIB_NOEXCEPT;
88  bool Close() NLIB_NOEXCEPT;
89  void SetError(errno_t e) const NLIB_NOEXCEPT {
90  if (errno_ == 0) errno_ = e;
91  }
92  errno_t GetErrorValue() const NLIB_NOEXCEPT { return errno_; }
93  InputStream* GetStream() NLIB_NOEXCEPT { return stream_; }
94  int GetLine() const NLIB_NOEXCEPT { return line_; }
95  int GetColumn() const NLIB_NOEXCEPT {
96  return static_cast<int>((cur_ - &buf_[0]) + 1 + pos_of_buf_ - pos_of_line_);
97  }
98  NLIB_SAFE_BOOL(TextReader, GetErrorValue() == 0)
99 
100  protected:
101  char* GetCur() NLIB_NOEXCEPT { return cur_; }
102  char* GetBufEnd() NLIB_NOEXCEPT { return bufend_; }
103  void SetBufEnd(char* p) NLIB_NOEXCEPT { bufend_ = p; }
104 
105  // checks UTF-8 validity, converts CRLF to LF, and convert CR to LF
106  virtual void FillBuffer_() NLIB_NOEXCEPT;
107 
108  private:
109  void FillBuffer() NLIB_NOEXCEPT { this->FillBuffer_(); }
110  void ConstructUtf32Cache() NLIB_NOEXCEPT;
111  int SkipWs_(int base) NLIB_NOEXCEPT;
112 
113  private:
114  char buf_[512 + 3 + 1];
115  nlib_utf32_t utf32_cache_;
116  char* cur_;
117  char* bufend_;
118  size_t utf8_bytecount_;
119  InputStream* stream_;
120  mutable ErrnoT errno_;
121  int line_;
122  size_t pos_of_line_;
123  size_t pos_of_buf_;
124 
125  NLIB_DISALLOW_COPY_AND_ASSIGN(TextReader);
126 };
127 
128 template<class T>
129 bool TextReader::ReadUntil(size_t* len, nlib_utf8_t* buf, size_t n, T pred) NLIB_NOEXCEPT {
130  // NOTICE: not NULL terminated
131  if (!buf) {
132  *len = 0;
133  return false;
134  }
135  utf32_cache_ = 0;
136  char* p = buf;
137  char* pend = buf + n;
138  for (;;) {
139  if (cur_ == bufend_) {
140  this->FillBuffer();
141  if (cur_ == bufend_) {
142  *len = p - buf;
143  return false;
144  }
145  }
146  // T::operator()(const char* ptr);
147  while (p != pend && cur_ != bufend_) {
148  if ((*reinterpret_cast<unsigned char*>(cur_) & 0xC0) != 0x80 &&
149  pred((const char*)cur_)) {
150  *len = p - buf;
151  return true;
152  }
153  *p = *cur_;
154  ++p;
155  ++cur_;
156  }
157  if (p == pend) {
158  if (cur_ != bufend_ && (*reinterpret_cast<unsigned char*>(cur_) & 0xC0) == 0x80) {
159  do {
160  --p;
161  --cur_;
162  } while ((*reinterpret_cast<unsigned char*>(cur_) & 0xC0) == 0x80);
163  }
164  *len = p - buf;
165  return false;
166  }
167  }
168 }
169 
170 NLIB_NAMESPACE_END
171 
172 #endif // INCLUDE_NN_NLIB_TEXTREADER_H_
bool ReadUntil(size_t *len, nlib_utf8_t(&buf)[N], T pred) noexcept
A template overload of the above function.
Definition: TextReader.h:68
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:183
#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...
Definition: Config.h:199
InputStream * GetStream() noexcept
Gets the stream for the text reader to read.
Definition: TextReader.h:93
#define NLIB_DEPRECATED
Indicates that a function or something has been deprecated.
Definition: Config.h:113
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:87
int GetColumn() const noexcept
Gets the current column.
Definition: TextReader.h:95
errno_t GetErrorValue() const noexcept
This function can get the cause of the error when reading has failed.
Definition: TextReader.h:92
uint32_t nlib_utf32_t
Uses typedef to define as char32_t if that can be used. If not, it uses typedef to define as uint32_t...
Definition: Platform.h:289
size_t ReadDecimalString(char(&buf)[N]) noexcept
A template overload of the above function.
Definition: TextReader.h:73
bool Proceed(char c) noexcept
Advances the stream by the amount of the character specified by c.
Definition: TextReader.h:77
bool ReadUntil(size_t *len, nlib_utf8_t(&buf)[N], char delim) noexcept
A template overload of the above function.
Definition: TextReader.h:61
The class for reading text from streams.
Definition: TextReader.h:33
The base class for input streams. This class cannot be instantiated.
Definition: InputStream.h:29
int GetLine() const noexcept
Gets the current line number.
Definition: TextReader.h:94
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:109
A file that contains the configuration information for each development environment.
bool Proceed(StringView &str, const StringView &prefix) noexcept
If str starts with prefix, it is allowed to go forward by the length of that string.
Definition: StringView.h:353
int SkipWs() noexcept
Skips white-space characters (space, newline, tab, and return) in the stream and returns the number t...
Definition: TextReader.h:42
bool Read(BinaryReader *r, T *x)
You can read to user-defined class objects by specializing this function template.
Definition: BinaryReader.h:161
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
char nlib_utf8_t
Defines char with a typedef. Indicates that it is a UTF-8 string.
Definition: Platform.h:303
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37