NDEVR
API Documentation
Scanner.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: Scanner
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/File.h>
35#include <NDEVR/String.h>
36
37namespace NDEVR
38{
39 class TranslatedString;
46 class NDEVR_BASE_API Scanner
47 {
48 public:
55 explicit Scanner(const File& file, char delimiter = '|', File::OpenMode mode = File::e_ascii_read);
61 explicit Scanner(const StringView& string, char delimiter = '|');
67 explicit Scanner(const String& string, char delimiter = '|');
71 virtual ~Scanner();
76 void setDelimiter(char delimiter);
96 virtual bool nextLine();
103 virtual bool nextLine(String& string, bool clear_string = true);
108 const String& currentLine() const { return m_current_line; }
118 const char* getCurrent() const;
123 void moveForward(uint04 distance = 1);
129 bool isComment(char comment) const;
146 bool skipTo(char location, bool ignore_case = false);
153 bool skipTo(const Buffer<char>& locations, bool ignore_case = false);
160 bool skipTo(const char* location, bool ignore_case = false);
174 void setHasDelimiter(const bool has_delimiter) { m_has_delimiter = has_delimiter; }
184 void setQuoteCharacter(char quote_character) { m_quote_character = quote_character; }
189 char quoteCharacter() const { return m_quote_character; }
194 File file() const { return m_file; }
199 template<class t_type>
200 t_type getNext();
201
206 template<class t_type>
207 t_type peekNext()
208 {
209 uint04 cur_line_pos = m_cur_line_pos;
210 uint04 cur_column = m_cur_column;
211 t_type next_val = getNext<t_type>();
212 m_cur_line_pos = cur_line_pos;
213 m_cur_column = cur_column;
214 return next_val;
215 }
216
222
227 void getNext(String& next);
228
233 template<class t_type>
234 t_type getNextHex();
235
241 template<class t_type>
242 t_type getNext(uint04 count)
243 {
244 char* start = &m_current_line[m_cur_line_pos];
245 if (count == 0)
246 return StringView();
247 m_cur_line_pos += count;
248 return StringView(start, count).getAs<t_type>();
249 }
250
254 bool hasNext() const;
255
261 template<class t_type>
262 t_type getColumn(uint04 column)
263 {
264 if (m_cur_column > column)
265 {
266 m_cur_column = 0;
267 m_cur_line_pos = 0;
268 }
269
270 for (uint04 i = m_cur_column; i < column; i++)
271 skip();
272 return getNext<t_type>();
273 }
274
280
285 void setIgnoreEmptyLines(bool ignore_new_lines) { m_ignore_empty_lines = ignore_new_lines; }
290 void setIgnoreWhiteSpace(bool ignore_white_space) { m_ignore_white_space = ignore_white_space; }
296 virtual void setFilePosition(uint08 file_position, bool read_line = true);
297
303 {
305 }
306
311 protected:
324 bool m_ignore_white_space = false;
326 };
327}
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
Logic for reading or writing to a file as well as navigating filesystems or other common file operati...
Definition File.h:53
OpenMode
Specifies the mode in which a file is opened.
Definition File.h:59
@ e_ascii_read
Open for ASCII text reading.
Definition File.h:62
virtual uint01 * getByteArray()
Returns a pointer to the raw byte array at the current line position.
Definition Scanner.h:302
uint04 getLineLength() const
Returns the length of the current line.
bool m_ignore_empty_lines
Whether to skip empty lines when advancing.
Definition Scanner.h:323
uint04 m_cur_line_pos
The current character index within the current line.
Definition Scanner.h:318
bool m_ignore_white_space
Whether to skip leading whitespace when reading tokens.
Definition Scanner.h:324
char quoteCharacter() const
Returns the current quote character.
Definition Scanner.h:189
virtual bool nextLine(String &string, bool clear_string=true)
Advances to the next line and stores it in the provided string.
bool skipTo(const Buffer< char > &locations, bool ignore_case=false)
Skips forward until one of the specified characters is found.
t_type getNextHex()
Reads the next token as a hexadecimal value and returns it as the specified type.
void setIgnoreEmptyLines(bool ignore_new_lines)
Sets whether empty lines should be skipped when reading.
Definition Scanner.h:285
Scanner(const StringView &string, char delimiter='|')
Constructs a Scanner that reads from a string view.
virtual void setFilePosition(uint08 file_position, bool read_line=true)
Sets the file read position to the specified byte offset.
File m_file
The file being read, if constructed from a file.
Definition Scanner.h:313
uint08 m_data_size
The total size of the data source in bytes.
Definition Scanner.h:317
void moveForward(uint04 distance=1)
Moves the line position forward by the specified distance.
uint08 getCurrentFilePosition()
Returns the current byte position within the file.
Scanner(const String &string, char delimiter='|')
Constructs a Scanner that reads from a string.
bool skipTo(char location, bool ignore_case=false)
Skips forward until the specified character is found.
void setHasDelimiter(const bool has_delimiter)
Sets whether the scanner expects delimiters between tokens.
Definition Scanner.h:174
char m_delimiter
The delimiter character used to separate tokens.
Definition Scanner.h:321
t_type getNext()
Reads and returns the next token parsed as the specified type.
void getNext(String &next)
Reads the next token and stores it in the provided string.
StringView getNext()
Reads and returns the next token as a StringView.
void setIgnoreWhiteSpace(bool ignore_white_space)
Sets whether leading whitespace should be ignored when reading tokens.
Definition Scanner.h:290
uint08 m_current_file_position
The current byte offset within the file.
Definition Scanner.h:316
File file() const
Returns the file associated with this scanner.
Definition Scanner.h:194
uint08 dataSize() const
Returns the total size of the data source in bytes.
const String & currentLine() const
Returns a const reference to the current line being parsed.
Definition Scanner.h:108
void setQuoteCharacter(char quote_character)
Sets the character used to denote quoted strings.
Definition Scanner.h:184
uint04 m_cur_column
The current column index for column-based reading.
Definition Scanner.h:319
void setDelimiter(char delimiter)
Sets the delimiter character used to separate tokens.
String m_current_line
The contents of the current line being parsed.
Definition Scanner.h:312
t_type getColumn(uint04 column)
Skips to the specified column index and returns the token there as the specified type.
Definition Scanner.h:262
bool m_has_delimiter
Whether the scanner expects delimiters between tokens.
Definition Scanner.h:325
bool hasNext() const
Checks whether there are more tokens available on the current line.
uint04 skipWhiteSpace()
Skips over any whitespace characters at the current position.
bool skipTo(const char *location, bool ignore_case=false)
Skips forward until the specified string is found.
virtual ~Scanner()
Destructor.
const char * getCurrent() const
Returns a pointer to the current character position in the line.
virtual bool nextLine()
Advances the scanner to the next line in the stream.
fltp04 getFilePercent()
Returns the percentage of the file that has been read.
Scanner(const File &file, char delimiter='|', File::OpenMode mode=File::e_ascii_read)
Constructs a Scanner that reads from a file.
String & currentLine()
Returns a mutable reference to the current line being parsed.
Definition Scanner.h:113
String m_time_format
The format string used for parsing time values.
Definition Scanner.h:314
t_type getNext(uint04 count)
Reads the next token of a fixed character count and returns it as the specified type.
Definition Scanner.h:242
uint04 m_current_line_size
The length of the current line in characters.
Definition Scanner.h:320
uint04 & linePosition()
Returns a mutable reference to the current position within the line.
Definition Scanner.h:179
uint04 skip()
Skips the current token and advances to the next delimiter.
TranslatedString getDebugPosition() const
Returns a human-readable string describing the current file and line position for debugging.
uint08 getCurrentLineNumber() const
Returns the current line number in the stream.
uint08 m_current_line_number
The current one-based line number in the stream.
Definition Scanner.h:315
void resetLinePosition()
Resets the line position back to the beginning of the current line.
char m_quote_character
The character used to denote quoted strings.
Definition Scanner.h:322
t_type peekNext()
Peeks at the next token without advancing the read position.
Definition Scanner.h:207
bool isComment(char comment) const
Checks if the current line begins with a comment character.
The core String View class for the NDEVR API.
Definition StringView.h:58
t_type getAs() const
Converts a string into an object.
Definition StringView.h:125
The core String class for the NDEVR API.
Definition String.h:95
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
The primary namespace for the NDEVR SDK.
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...