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.
9 #include "CharArrayParser.h"
11 #include "utils/log.h"
15 void CCharArrayParser::Reset()
21 void CCharArrayParser::Reset(const char* data
, int limit
)
28 int CCharArrayParser::CharsLeft()
30 return m_limit
- m_position
;
33 int CCharArrayParser::GetPosition()
38 bool CCharArrayParser::SetPosition(int position
)
40 if (position
>= 0 && position
<= m_limit
)
41 m_position
= position
;
44 CLog::Log(LOGERROR
, "{} - Position out of range", __FUNCTION__
);
50 bool CCharArrayParser::SkipChars(int nChars
)
52 return SetPosition(m_position
+ nChars
);
55 uint8_t CCharArrayParser::ReadNextUnsignedChar()
60 CLog::Log(LOGERROR
, "{} - No data to read", __FUNCTION__
);
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()
72 CLog::Log(LOGERROR
, "{} - No data to read", __FUNCTION__
);
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()
86 CLog::Log(LOGERROR
, "{} - No data to read", __FUNCTION__
);
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
)
102 CLog::Log(LOGERROR
, "{} - No data to read", __FUNCTION__
);
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__
);
112 bool CCharArrayParser::ReadNextArray(int length
, char* data
)
116 CLog::Log(LOGERROR
, "{} - No data to read", __FUNCTION__
);
119 if (m_position
+ length
> m_limit
)
121 CLog::Log(LOGERROR
, "{} - Position out of range", __FUNCTION__
);
124 std::strncpy(data
, m_data
+ m_position
, length
);
129 bool CCharArrayParser::ReadNextLine(std::string
& line
)
133 CLog::Log(LOGERROR
, "{} - No data to read", __FUNCTION__
);
136 if (CharsLeft() == 0)
142 int lineLimit
= m_position
;
143 while (lineLimit
< m_limit
&& !(m_data
[lineLimit
] == '\n' || m_data
[lineLimit
] == '\r'))
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.
155 line
.assign(m_data
+ m_position
, lineLimit
- m_position
);
156 m_position
= lineLimit
;
158 if (m_data
[m_position
] == '\r')
162 if (m_data
[m_position
] == '\n')