xbmc
CharArrayParser.h
1 /*
2  * Copyright (C) 2005-2021 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include <cstdint>
12 #include <string>
13 
18 {
19 public:
20  CCharArrayParser() = default;
21  ~CCharArrayParser() = default;
22 
26  void Reset();
27 
33  void Reset(const char* data, int limit);
34 
38  int CharsLeft();
39 
43  int GetPosition();
44 
50  bool SetPosition(int position);
51 
57  bool SkipChars(int nChars);
58 
64  uint8_t ReadNextUnsignedChar();
65 
71  uint16_t ReadNextUnsignedShort();
72 
78  uint32_t ReadNextUnsignedInt();
79 
86  std::string ReadNextString(int length);
87 
95  bool ReadNextArray(int length, char* data);
96 
106  bool ReadNextLine(std::string& line);
107 
112  const char* GetData() { return m_data; };
113 
114 private:
115  const char* m_data{nullptr};
116  int m_position{0};
117  int m_limit{0};
118 };
Wraps a char array, providing a set of methods for parsing data from it.
Definition: CharArrayParser.h:17
uint16_t ReadNextUnsignedShort()
Reads the next two chars as unsigned short value (it is assumed that the caller has already checked t...
Definition: CharArrayParser.cpp:69
const char * GetData()
Get the current data.
Definition: CharArrayParser.h:112
bool ReadNextLine(std::string &line)
Reads a line of text. A line is considered to be terminated by any one of a carriage return (&#39;\r&#39;)...
Definition: CharArrayParser.cpp:130
bool SkipChars(int nChars)
Skip a specified number of chars.
Definition: CharArrayParser.cpp:51
int CharsLeft()
Return the number of chars yet to be read.
Definition: CharArrayParser.cpp:29
uint32_t ReadNextUnsignedInt()
Reads the next four chars as unsigned int value (it is assumed that the caller has already checked th...
Definition: CharArrayParser.cpp:83
bool SetPosition(int position)
Set the reading offset in the array.
Definition: CharArrayParser.cpp:39
std::string ReadNextString(int length)
Reads the next string of specified length (it is assumed that the caller has already checked the avai...
Definition: CharArrayParser.cpp:99
uint8_t ReadNextUnsignedChar()
Reads the next unsigned char (it is assumed that the caller has already checked the availability of t...
Definition: CharArrayParser.cpp:56
int GetPosition()
Returns the current offset in the array.
Definition: CharArrayParser.cpp:34
bool ReadNextArray(int length, char *data)
Reads the next chars array of specified length (it is assumed that the caller has already checked the...
Definition: CharArrayParser.cpp:113
void Reset()
Sets the position and limit to zero.
Definition: CharArrayParser.cpp:16