2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2008 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifndef SkReader32_DEFINED
|
|
|
|
#define SkReader32_DEFINED
|
|
|
|
|
|
|
|
#include "SkScalar.h"
|
|
|
|
|
2011-06-21 15:43:11 +00:00
|
|
|
class SkString;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
class SkReader32 : SkNoncopyable {
|
|
|
|
public:
|
|
|
|
SkReader32() : fCurr(NULL), fStop(NULL), fBase(NULL) {}
|
|
|
|
SkReader32(const void* data, size_t size) {
|
|
|
|
this->setMemory(data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setMemory(const void* data, size_t size) {
|
|
|
|
SkASSERT(ptr_align_4(data));
|
|
|
|
SkASSERT(SkAlign4(size) == size);
|
|
|
|
|
|
|
|
fBase = fCurr = (const char*)data;
|
|
|
|
fStop = (const char*)data + size;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t size() const { return fStop - fBase; }
|
|
|
|
uint32_t offset() const { return fCurr - fBase; }
|
|
|
|
bool eof() const { return fCurr >= fStop; }
|
|
|
|
const void* base() const { return fBase; }
|
|
|
|
const void* peek() const { return fCurr; }
|
2011-04-26 17:49:03 +00:00
|
|
|
|
|
|
|
uint32_t available() const { return fStop - fCurr; }
|
|
|
|
bool isAvailable(uint32_t size) const { return fCurr + size <= fStop; }
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void rewind() { fCurr = fBase; }
|
|
|
|
|
|
|
|
void setOffset(size_t offset) {
|
|
|
|
SkASSERT(SkAlign4(offset) == offset);
|
|
|
|
SkASSERT(offset <= this->size());
|
|
|
|
fCurr = fBase + offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool readBool() { return this->readInt() != 0; }
|
|
|
|
|
|
|
|
int32_t readInt() {
|
|
|
|
SkASSERT(ptr_align_4(fCurr));
|
|
|
|
int32_t value = *(const int32_t*)fCurr;
|
|
|
|
fCurr += sizeof(value);
|
|
|
|
SkASSERT(fCurr <= fStop);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkScalar readScalar() {
|
|
|
|
SkASSERT(ptr_align_4(fCurr));
|
|
|
|
SkScalar value = *(const SkScalar*)fCurr;
|
|
|
|
fCurr += sizeof(value);
|
|
|
|
SkASSERT(fCurr <= fStop);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const void* skip(size_t size) {
|
|
|
|
SkASSERT(ptr_align_4(fCurr));
|
|
|
|
const void* addr = fCurr;
|
|
|
|
fCurr += SkAlign4(size);
|
|
|
|
SkASSERT(fCurr <= fStop);
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2011-09-07 13:52:17 +00:00
|
|
|
template <typename T> const T& skipT() {
|
|
|
|
SkASSERT(SkAlign4(sizeof(T)) == sizeof(T));
|
|
|
|
return *(const T*)this->skip(sizeof(T));
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void read(void* dst, size_t size) {
|
2011-05-05 20:04:44 +00:00
|
|
|
SkASSERT(0 == size || dst != NULL);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(ptr_align_4(fCurr));
|
|
|
|
memcpy(dst, fCurr, size);
|
|
|
|
fCurr += SkAlign4(size);
|
|
|
|
SkASSERT(fCurr <= fStop);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t readU8() { return (uint8_t)this->readInt(); }
|
|
|
|
uint16_t readU16() { return (uint16_t)this->readInt(); }
|
|
|
|
int32_t readS32() { return this->readInt(); }
|
|
|
|
uint32_t readU32() { return this->readInt(); }
|
2011-05-23 12:21:05 +00:00
|
|
|
|
|
|
|
/**
|
2011-06-21 15:43:11 +00:00
|
|
|
* Read the length of a string (written by SkWriter32::writeString) into
|
|
|
|
* len (if len is not NULL) and return the null-ternimated address of the
|
|
|
|
* string within the reader's buffer.
|
2011-05-23 12:21:05 +00:00
|
|
|
*/
|
|
|
|
const char* readString(size_t* len = NULL);
|
|
|
|
|
2011-06-21 15:43:11 +00:00
|
|
|
/**
|
|
|
|
* Read the string (written by SkWriter32::writeString) and return it in
|
|
|
|
* copy (if copy is not null). Return the length of the string.
|
|
|
|
*/
|
|
|
|
size_t readIntoString(SkString* copy);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
// these are always 4-byte aligned
|
|
|
|
const char* fCurr; // current position within buffer
|
|
|
|
const char* fStop; // end of buffer
|
|
|
|
const char* fBase; // beginning of buffer
|
|
|
|
|
|
|
|
#ifdef SK_DEBUG
|
2011-09-07 13:52:17 +00:00
|
|
|
static bool ptr_align_4(const void* ptr) {
|
2008-12-17 15:59:43 +00:00
|
|
|
return (((const char*)ptr - (const char*)NULL) & 3) == 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|