sources/network/bytestream.cpp

changeset 173
248fba6d1e76
parent 158
de7574d292ad
child 174
78ee24fd3321
child 180
2e7225dbd9b2
equal deleted inserted replaced
168:96ffd13c08a2 173:248fba6d1e76
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 ByteArray::Iterator stringEndIterator; 132 String result;
133 133
134 // Where's the end of the string? 134 for (char byte; (byte = readByte()) != '\0';)
135 for (stringEndIterator = getCurrentIterator(); *stringEndIterator != '\0'; ++stringEndIterator)
136 { 135 {
137 if (stringEndIterator == m_data.end()) 136 if (result.length() < MAX_NETWORK_STRING)
138 { 137 result += byte;
139 // Past the end of the buffer
140 throw IOError("unterminated or too long string in packet");
141 }
142 } 138 }
143 139
144 // Skip past the null terminator. 140 return result;
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));
154 } 141 }
155 142
156 /*! 143 /*!
157 * \brief Reads in a buffer of the specified length. 144 * \brief Reads in a buffer of the specified length.
158 * \param length Amount of bytes to read. 145 * \param length Amount of bytes to read.

mercurial