struct pack_t { char *data; size_t size; size_t pos; Ptr_flag ptr_flag; std::vector<PtrStoreRef> alloced; //allocated data used for cleaning up pack_t(size_t sz=0); pack_t(const char* filename, const char* mode); //pack to file pack_t& reseti(); pack_t& reseto(); pack_t& seeki(int offs); pack_t& seeko(int offs); void packraw(char *x, int sz); void unpackraw(char *x, int sz); };
data
points to the beginning of the buffer maintained by
pack_t
. size
refers to the current position of the input
stream (ie the size of current valid data). pos
refers to the
current position of the output stream. It is an error to assign values
directly to pos. It is OK to assign a value to size when setting
up a pack_t
variable for unpacking. Do not update size
whilst packing. It is OK to assign a pointer value to data for
unpacking only, however one should note that delete is called on
the pointer during destruction, so in general you should reset data to NULL before the pack_t
variable goes out of scope, if
you don't want the object deleted (for instance if you've set it to
the address of a static array).
size and pos can be reset to 0 using the reseti() and reseto() routines respectively. seeki() and seeko() allows arbitrary positioning of the streams -- the seek offset in this case is relative to the current position.
The constructor takes an integer argument which specifies the size of an initial buffer. For example:
pack_t b(N); b.size=N; fread(b,N,1,f); b>>foo;is a common idiom for reading some data in from a file.
packraw
and unpackraw
allow arbitrary byte data to be
pushed onto the buffer and taken off. This involves an extra copy
operation, but is the safest way of manipulating the buffer directly.