27#if defined HAVE_CONFIG_H
31#include "id3/io_decorators.h"
38 while (!reader.
atEnd())
45 str +=
static_cast<char>(ch);
50String io::readText(
ID3_Reader& reader,
size_t len)
54 const size_t SIZE = 1024;
56 size_t remaining = len;
57 while (remaining > 0 && !reader.
atEnd())
59 size_t numRead = reader.
readChars(buf, min(remaining, SIZE));
61 str.append(
reinterpret_cast<String::value_type *
>(buf), numRead);
68 bool isNull(
unsigned char ch1,
unsigned char ch2)
70 return ch1 ==
'\0' && ch2 ==
'\0';
73 int isBOM(
unsigned char ch1,
unsigned char ch2)
94 if (ch1 == 0xFE && ch2 == 0xFF)
98 else if (ch1 == 0xFF && ch2 == 0xFE)
113 io::ExitTrigger et(reader);
125String io::readUnicodeString(
ID3_Reader& reader)
129 if (!readTwoChars(reader, ch1, ch2) || isNull(ch1, ch2))
133 int bom = isBOM(ch1, ch2);
136 unicode +=
static_cast<char>(ch1);
137 unicode +=
static_cast<char>(ch2);
139 while (!reader.
atEnd())
141 if (!readTwoChars(reader, ch1, ch2) || isNull(ch1, ch2))
147 unicode +=
static_cast<char>(ch2);
148 unicode +=
static_cast<char>(ch1);
152 unicode +=
static_cast<char>(ch1);
153 unicode +=
static_cast<char>(ch2);
159String io::readUnicodeText(
ID3_Reader& reader,
size_t len)
163 if (!readTwoChars(reader, ch1, ch2))
168 int bom = isBOM(ch1, ch2);
173 unicode += readText(reader, len);
177 unicode = readText(reader, len);
181 for (
size_t i = 0; i < len; i += 2)
183 if (!readTwoChars(reader, ch1, ch2))
199BString io::readBinary(
ID3_Reader& reader,
size_t len)
204 size_t remaining = len;
205 const size_t SIZE = 1024;
207 while (!reader.
atEnd() && remaining > 0)
209 size_t numRead = reader.
readChars(buf, min(remaining, SIZE));
210 remaining -= numRead;
211 binary.append(
reinterpret_cast<BString::value_type *
>(buf), numRead);
217uint32 io::readLENumber(
ID3_Reader& reader,
size_t len)
220 for (
size_t i = 0; i < len; i++)
226 val += (
static_cast<uint32
>(0xFF & reader.
readChar()) << (i * 8));
231uint32 io::readBENumber(
ID3_Reader& reader,
size_t len)
238 val +=
static_cast<uint32
>(0xFF & reader.
readChar());
243String io::readTrailingSpaces(
ID3_Reader& reader,
size_t len)
245 io::WindowedReader wr(reader, len);
253 if (ch ==
'\0' || ch ==
' ')
259 str += spaces + (char) ch;
269 const unsigned short BITSUSED = 7;
270 const uint32 MAXVAL =
MASK(BITSUSED *
sizeof(uint32));
272 for (
size_t i = 0; i <
sizeof(uint32); ++i)
279 val = (val << BITSUSED) | static_cast<uint32>(reader.
readChar()) &
MASK(BITSUSED);
283 return min(val, MAXVAL);
286size_t io::writeBENumber(
ID3_Writer& writer, uint32 val,
size_t len)
290 renderNumber(bytes, val, size);
294size_t io::writeTrailingSpaces(
ID3_Writer& writer, String buf,
size_t len)
300 for (; size < len; ++size)
304 return writer.
getCur() - beg;
307size_t io::writeUInt28(
ID3_Writer& writer, uint32 val)
309 uchar data[
sizeof(uint32)];
310 const unsigned short BITSUSED = 7;
311 const uint32 MAXVAL =
MASK(BITSUSED *
sizeof(uint32));
312 val = min(val, MAXVAL);
316 for (
size_t i = 0; i <
sizeof(uint32); ++i)
320 data[
sizeof(uint32) - i - 1] =
static_cast<uchar>(val &
MASK(BITSUSED));
328 return writer.
writeChars(data,
sizeof(uint32));
331size_t io::writeString(
ID3_Writer& writer, String data)
333 size_t size = writeText(writer, data);
338size_t io::writeText(
ID3_Writer& writer, String data)
342 return writer.
getCur() - beg;
345size_t io::writeUnicodeString(
ID3_Writer& writer, String data,
bool bom)
347 size_t size = writeUnicodeText(writer, data, bom);
349 writer.
writeChars((
const unsigned char*) &null, 2);
353size_t io::writeUnicodeText(
ID3_Writer& writer, String data,
bool bom)
356 size_t size = (data.size() / 2) * 2;
365 writer.
writeChars((
const unsigned char*) &BOM, 2);
366 const unsigned char* pdata = (
const unsigned char*)data.c_str();
367 for (
size_t i = 0; i < size; i += 2)
369 unicode_t ch = (pdata[i] << 8) | pdata[i+1];
370 writer.
writeChars((
const unsigned char*) &ch, 2);
373 return writer.
getCur() - beg;
virtual size_type readChars(char_type buf[], size_type len)=0
Read up to len characters into buf and advance the internal position accordingly.
virtual int_type readChar()
Read a single character and advance the internal position.
virtual size_type remainingBytes()
virtual size_type writeChars(const char_type buf[], size_type len)=0
Write up to len characters into buf and advance the internal position accordingly.
virtual int_type writeChar(char_type ch)
Write a single character and advance the internal position.
virtual pos_type getCur()=0
Return the next position that will be written to.