127 * \brief Reads in characters until a null terminator is encountered. |
127 * \brief Reads in characters until a null terminator is encountered. |
128 * \returns the read string. |
128 * \returns the read string. |
129 */ |
129 */ |
130 String Bytestream::readString() |
130 String Bytestream::readString() |
131 { |
131 { |
132 String result; |
132 ByteArray::Iterator stringEndIterator; |
133 |
133 |
134 for (char byte; (byte = readByte()) != '\0';) |
134 // Where's the end of the string? |
|
135 for (stringEndIterator = getCurrentIterator(); *stringEndIterator != '\0'; ++stringEndIterator) |
135 { |
136 { |
136 if (result.length() < MAX_NETWORK_STRING) |
137 if (stringEndIterator == m_data.end()) |
137 result += byte; |
138 { |
|
139 // Past the end of the buffer |
|
140 throw IOError("unterminated or too long string in packet"); |
|
141 } |
138 } |
142 } |
139 |
143 |
140 return result; |
144 // Skip past the null terminator. |
|
145 if (*stringEndIterator == '\0') |
|
146 stringEndIterator += 1; |
|
147 |
|
148 // Build and return the string, and advance the position. |
|
149 int stringStart = m_position; |
|
150 unsigned int length = stringEndIterator - getCurrentIterator(); |
|
151 length = min<int>(length, MAX_NETWORK_STRING); |
|
152 m_position += length; |
|
153 return String::fromBytes(m_data.splice(stringStart, stringStart + length)); |
141 } |
154 } |
142 |
155 |
143 /*! |
156 /*! |
144 * \brief Reads in a buffer of the specified length. |
157 * \brief Reads in a buffer of the specified length. |
145 * \param length Amount of bytes to read. |
158 * \param length Amount of bytes to read. |