[PVR][Estuary] Timer settings dialog: Show client name in timer type selection dialog...
[xbmc.git] / xbmc / utils / CharArrayParser.cpp
blob2e917e2ff2ba06744c0268f774f712061be37a6e
1 /*
2 * Copyright (C) 2005-2021 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "CharArrayParser.h"
11 #include "utils/log.h"
13 #include <cstring>
15 void CCharArrayParser::Reset()
17 m_limit = 0;
18 m_position = 0;
21 void CCharArrayParser::Reset(const char* data, int limit)
23 m_data = data;
24 m_limit = limit;
25 m_position = 0;
28 int CCharArrayParser::CharsLeft()
30 return m_limit - m_position;
33 int CCharArrayParser::GetPosition()
35 return m_position;
38 bool CCharArrayParser::SetPosition(int position)
40 if (position >= 0 && position <= m_limit)
41 m_position = position;
42 else
44 CLog::Log(LOGERROR, "{} - Position out of range", __FUNCTION__);
45 return false;
47 return true;
50 bool CCharArrayParser::SkipChars(int nChars)
52 return SetPosition(m_position + nChars);
55 uint8_t CCharArrayParser::ReadNextUnsignedChar()
57 m_position++;
58 if (!m_data)
60 CLog::Log(LOGERROR, "{} - No data to read", __FUNCTION__);
61 return 0;
63 if (m_position > m_limit)
64 CLog::Log(LOGERROR, "{} - Position out of range", __FUNCTION__);
65 return static_cast<uint8_t>(m_data[m_position - 1]) & 0xFF;
68 uint16_t CCharArrayParser::ReadNextUnsignedShort()
70 if (!m_data)
72 CLog::Log(LOGERROR, "{} - No data to read", __FUNCTION__);
73 return 0;
75 m_position += 2;
76 if (m_position > m_limit)
77 CLog::Log(LOGERROR, "{} - Position out of range", __FUNCTION__);
78 return (static_cast<uint16_t>(m_data[m_position - 2]) & 0xFF) << 8 |
79 (static_cast<uint16_t>(m_data[m_position - 1]) & 0xFF);
82 uint32_t CCharArrayParser::ReadNextUnsignedInt()
84 if (!m_data)
86 CLog::Log(LOGERROR, "{} - No data to read", __FUNCTION__);
87 return 0;
89 m_position += 4;
90 if (m_position > m_limit)
91 CLog::Log(LOGERROR, "{} - Position out of range", __FUNCTION__);
92 return (static_cast<uint32_t>(m_data[m_position - 4]) & 0xFF) << 24 |
93 (static_cast<uint32_t>(m_data[m_position - 3]) & 0xFF) << 16 |
94 (static_cast<uint32_t>(m_data[m_position - 2]) & 0xFF) << 8 |
95 (static_cast<uint32_t>(m_data[m_position - 1]) & 0xFF);
98 std::string CCharArrayParser::ReadNextString(int length)
100 if (!m_data)
102 CLog::Log(LOGERROR, "{} - No data to read", __FUNCTION__);
103 return "";
105 std::string str(m_data + m_position, length);
106 m_position += length;
107 if (m_position > m_limit)
108 CLog::Log(LOGERROR, "{} - Position out of range", __FUNCTION__);
109 return str;
112 bool CCharArrayParser::ReadNextArray(int length, char* data)
114 if (!m_data)
116 CLog::Log(LOGERROR, "{} - No data to read", __FUNCTION__);
117 return false;
119 if (m_position + length > m_limit)
121 CLog::Log(LOGERROR, "{} - Position out of range", __FUNCTION__);
122 return false;
124 std::strncpy(data, m_data + m_position, length);
125 data[length] = '\0';
126 return true;
129 bool CCharArrayParser::ReadNextLine(std::string& line)
131 if (!m_data)
133 CLog::Log(LOGERROR, "{} - No data to read", __FUNCTION__);
134 return false;
136 if (CharsLeft() == 0)
138 line.clear();
139 return false;
142 int lineLimit = m_position;
143 while (lineLimit < m_limit && !(m_data[lineLimit] == '\n' || m_data[lineLimit] == '\r'))
145 lineLimit++;
148 if (lineLimit - m_position >= 3 && m_data[m_position] == '\xEF' &&
149 m_data[m_position + 1] == '\xBB' && m_data[m_position + 2] == '\xBF')
151 // There's a UTF-8 byte order mark at the start of the line. Discard it.
152 m_position += 3;
155 line.assign(m_data + m_position, lineLimit - m_position);
156 m_position = lineLimit;
158 if (m_data[m_position] == '\r')
160 m_position++;
162 if (m_data[m_position] == '\n')
164 m_position++;
167 return true;