43 size_t pos = stream.
pos();
45 size = MIN<size_t>(stream.
size() - pos, size);
55 uint32 n = MIN<size_t>(size, 16);
56 if (stream.
read(rowData, n) != n)
60 std::fprintf(stderr,
"%08X ", offset);
63 for (
uint32 i = 0; i < 2; i++) {
64 for (
uint32 j = 0; j < 8; j++) {
69 std::fprintf(stderr,
"%02X ", rowData[m]);
72 std::fprintf(stderr,
" ");
76 std::fprintf(stderr,
" ");
79 std::fprintf(stderr,
"|");
82 for (
uint32 i = 0; i < n; i++)
83 std::fprintf(stderr,
"%c", std::isprint(rowData[i]) ? rowData[i] :
'.');
85 std::fprintf(stderr,
"|\n");
96 if (!data || (size == 0))
106 const char *tS =
reinterpret_cast<const char *
>(&tag);
107 if (!std::isprint(tS[0]) || !std::isprint(tS[1]) || !std::isprint(tS[2]) || !std::isprint(tS[3]))
128 if (strchr(str,
'-') != 0)
132 static inline void parse(
const char *nptr,
char **endptr,
signed long long &value) {
133 value =
strtoll(nptr, endptr, 0);
136 static inline void parse(
const char *nptr,
char **endptr,
unsigned long long &value) {
142 static inline void parse(
const char *nptr,
char **endptr,
signed long &value) {
143 value = strtol(nptr, endptr, 0);
146 static inline void parse(
const char *nptr,
char **endptr,
unsigned long &value) {
149 value = strtoul(nptr, endptr, 0);
152 static inline void parse(
const char *nptr,
char **endptr,
signed int &value) {
153 signed long tmp = strtol(nptr, endptr, 0);
154 if ((tmp < INT_MIN) || (tmp > INT_MAX))
157 value = (
signed int) tmp;
160 static inline void parse(
const char *nptr,
char **endptr,
unsigned int &value) {
163 unsigned long tmp = strtoul(nptr, endptr, 0);
167 value = (
unsigned int) tmp;
170 static inline void parse(
const char *nptr,
char **endptr,
signed short &value) {
171 signed long tmp = strtol(nptr, endptr, 0);
172 if ((tmp < SHRT_MIN) || (tmp > SHRT_MAX))
175 value = (
signed short) tmp;
178 static inline void parse(
const char *nptr,
char **endptr,
unsigned short &value) {
181 unsigned long tmp = strtoul(nptr, endptr, 0);
185 value = (
unsigned short) tmp;
188 static inline void parse(
const char *nptr,
char **endptr,
signed char &value) {
189 signed long tmp = strtol(nptr, endptr, 0);
190 if ((tmp < SCHAR_MIN) || (tmp > SCHAR_MAX))
193 value = (
signed char) tmp;
196 static inline void parse(
const char *nptr,
char **endptr,
unsigned char &value) {
199 unsigned long tmp = strtoul(nptr, endptr, 0);
203 value = (
unsigned char) tmp;
206 static inline void parse(
const char *nptr,
char **endptr,
float &value) {
207 value =
strtof(nptr, endptr);
210 static inline void parse(
const char *nptr,
char **endptr,
double &value) {
211 value = strtod(nptr, endptr);
220 throw Exception(
"Trying to parse an empty string");
223 const char *nptr = str.
c_str();
229 parse(nptr, &endptr, newValue);
231 while (endptr && isspace(*endptr))
234 if (endptr && (*endptr !=
'\0'))
235 throw Exception(
"Can't convert \"%s\" to type of size %u", str.
c_str(), (
uint)
sizeof(T));
237 throw Exception(
"\"%s\" out of range for type of size %u", str.
c_str(), (
uint)
sizeof(T));
247 throw Exception(
"Trying to parse an empty string");
286 const bool isNegative = value < 0;
290 char buf[64], *strStart = buf +
sizeof(buf) - 1;
309 *strStart-- =
ABS(value % 10) +
'0';
310 }
while ((value /= 10) && (strStart != buf));
314 throw Exception(
"Buffer overrun in composeString()");
327 return value ?
"true" :
"false";
350 size_t maxReadBack) {
352 if (needleSize == 0 || maxReadBack == 0)
355 assert(maxReadBack >= needleSize);
357 static const size_t kReadBufferSize = 0x400;
359 const size_t sizeFile = haystack.
size();
360 const size_t maxBack = MIN<size_t>(maxReadBack, sizeFile);
364 size_t backRead = needleSize;
365 while (backRead < maxBack) {
366 backRead = MIN<size_t>(maxBack, backRead + kReadBufferSize);
368 const size_t readPos = sizeFile - backRead;
369 const size_t readSize = MIN<size_t>(kReadBufferSize + needleSize, sizeFile - readPos);
372 haystack.
seek(readPos);
377 if (haystack.
read(buf.get(), readSize) != readSize)
380 for (
size_t i = (readSize - (needleSize - 1)); i-- > 0; )
381 if (!memcmp(buf.get() + i, needle, needleSize))
template void parseString< signed int >(const UString &str, signed int &value, bool allowEmpty)
template UString composeString< unsigned long >(unsigned long value)
A class holding an UTF-8 string.
virtual size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)=0
Sets the stream position indicator for the stream.
template UString composeString< signed short >(signed short value)
UString composeString(T value)
Convert any POD integer, float/double or bool type into a string.
template void parseString< signed short >(const UString &str, signed short &value, bool allowEmpty)
bool equalsIgnoreCase(const UString &str) const
Implementing the reading stream interfaces for plain memory blocks.
template void parseString< unsigned long long >(const UString &str, unsigned long long &value, bool allowEmpty)
template void parseString< unsigned short >(const UString &str, unsigned short &value, bool allowEmpty)
template void parseString< unsigned long >(const UString &str, unsigned long &value, bool allowEmpty)
Utility templates and functions for working with strings and streams.
A simple scoped smart pointer template.
template void parseString< float >(const UString &str, float &value, bool allowEmpty)
Basic exceptions to throw.
template UString composeString< signed char >(signed char value)
const char * c_str() const
Return the (utf8 encoded) string data.
size_t searchBackwards(SeekableReadStream &haystack, const byte *needle, size_t needleSize, size_t maxReadBack)
Search the stream, backwards, for the last occurrence of a set of bytes.
static UString format(const char *s,...) GCC_PRINTF(1
Print formatted data into an UString object, similar to sprintf().
Utility templates and functions.
static bool tagToString(uint32 tag, bool trim, UString &str)
template UString composeString< signed long long >(signed long long value)
template void parseString< double >(const UString &str, double &value, bool allowEmpty)
virtual size_t read(void *dataPtr, size_t dataSize)=0
Read data from the stream.
Simple memory based 'stream', which implements the ReadStream interface for a plain memory block...
bool empty() const
Is the string empty?
static void errorOnSign(const char *str)
template void parseString< signed char >(const UString &str, signed char &value, bool allowEmpty)
const Exception kReadError("Read error")
Exception when reading from a stream failed.
virtual size_t size() const =0
Obtains the total size of the stream, measured in bytes.
static void parse(const char *nptr, char **endptr, signed long long &value)
virtual size_t pos() const =0
Obtains the current value of the stream position indicator of the stream.
template void parseString< unsigned char >(const UString &str, unsigned char &value, bool allowEmpty)
void printDataHex(SeekableReadStream &stream, size_t size)
Print a quick hex dump of the given data.
template UString composeString< unsigned long long >(unsigned long long value)
template UString composeString< unsigned char >(unsigned char value)
template void parseString< signed long long >(const UString &str, signed long long &value, bool allowEmpty)
UString debugTag(uint32 tag, bool trim)
Create an elaborate string from an integer tag, for debugging purposes.
template UString composeString< signed long >(signed long value)
Low-level detection of architecture/system properties.
template UString composeString< unsigned int >(unsigned int value)
template UString composeString< unsigned short >(unsigned short value)
template void parseString< unsigned int >(const UString &str, unsigned int &value, bool allowEmpty)
Interface for a seekable & readable data stream.
void parseString(const UString &str, T &value, bool allowEmpty)
Parse a string into any POD integer, float/double or bool type.
template UString composeString< signed int >(signed int value)
template void parseString< signed long >(const UString &str, signed long &value, bool allowEmpty)