molecular
TextStream.h
Go to the documentation of this file.
1 /* TextStream.h
2 
3 MIT License
4 
5 Copyright (c) 2018 Fabian Herb
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 */
25 
26 #ifndef MOLECULAR_UTIL_TEXTSTREAM_H
27 #define MOLECULAR_UTIL_TEXTSTREAM_H
28 
29 #include <stdexcept>
30 #include <stdint.h>
31 
32 namespace molecular
33 {
34 namespace util
35 {
36 
38 class TextReadStreamBase
39 {
40 public:
41  virtual ~TextReadStreamBase() {}
42  virtual char* GetNextLine() = 0;
43 };
44 
46 template<class Storage>
48 {
49 public:
50  TextReadStream(Storage& storage) : mStorage(storage)
51  {
52  mLine[0] = 0;
53  }
54 
56 
59  char* GetNextLine() override
60  {
61  if(mStorage.EndOfData())
62  return nullptr;
63 
64  char c;
65  int i;
66  for(i = 0; i < kMaxLineLength; ++i)
67  {
68  if(mStorage.EndOfData() || mStorage.Read(&c, 1) != 1 || c == '\n')
69  {
70  mLine[i] = 0;
71  return mLine;
72  }
73  else
74  mLine[i] = c;
75  }
76  throw std::range_error("Line too long");
77  }
78 
79 private:
80  static const int kMaxLineLength = 1024;
81  char mLine[kMaxLineLength];
82  Storage& mStorage;
83 };
84 
85 }
86 } // namespace molecular
87 
88 #endif // MOLECULAR_UTIL_TEXTSTREAM_H
Reads a file line by line.
Definition: TextStream.h:45
Reads a file line by line.
Definition: TextStream.h:37
TextReadStream(Storage &storage)
Definition: TextStream.h:50
virtual ~TextReadStreamBase()
Definition: TextStream.h:41
Definition: AssetManager.h:36
char * GetNextLine() override
Reads a line and returns it.
Definition: TextStream.h:59