From: David Gow Date: Wed, 26 Mar 2014 16:15:01 +0000 (+0800) Subject: Crazy std::vector loader and saver. X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=efdd8b2a07dcf770e325ec1fc9b4b7d908331e23;p=ipdf%2Fcode.git Crazy std::vector loader and saver. --- diff --git a/src/document.cpp b/src/document.cpp index e8556ee..d745c15 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -5,6 +5,27 @@ using namespace IPDF; using namespace std; +// Loads an std::vector of size num_elements from a file. +template +static void LoadStructVector(FILE *src_file, size_t num_elems, std::vector& dest) +{ + size_t structsread = 0; + dest.resize(num_elems); + structsread = fread(dest.data(), sizeof(T), num_elems, src_file); + if (structsread != num_elems) + Fatal("Only read %u structs (expected %u)!", structsread, num_elems); +} + +// Saves an std::vector to a file. Size must be saves separately. +template +static void SaveStructVector(FILE *dst_file, std::vector& src) +{ + size_t written = 0; + written = fwrite(src.data(), sizeof(T), src.size(), dst_file); + if (written != src.size()) + Fatal("Only wrote %u structs (expected %u)!", written, src.size()); +} + void Document::Save(const string & filename) { Debug("Saving document to file \"%s\"...", filename.c_str()); @@ -19,16 +40,10 @@ void Document::Save(const string & filename) Fatal("Failed to write number of objects!"); Debug("Object types..."); - written = fwrite(m_objects.types.data(), sizeof(ObjectType), m_objects.types.size(), file); - if (written != ObjectCount()) - Fatal("Only wrote %u objects!", written); + SaveStructVector(file, m_objects.types); Debug("Object bounds..."); - written = fwrite(m_objects.bounds.data(), sizeof(Rect), m_objects.bounds.size(), file); - if (written != ObjectCount()) - Fatal("Only wrote %u objects!", written); - - + SaveStructVector(file, m_objects.bounds); int err = fclose(file); if (err != 0) @@ -57,18 +72,11 @@ void Document::Load(const string & filename) Fatal("Failed to read number of objects!"); Debug("Number of objects: %u", ObjectCount()); - m_objects.types.resize(ObjectCount()); - m_objects.bounds.resize(ObjectCount()); - Debug("Object types..."); - read = fread(m_objects.types.data(), sizeof(ObjectType), m_objects.types.size(), file); - if (read != ObjectCount()) - Fatal("Only read %u objects!", read); + LoadStructVector(file, ObjectCount(), m_objects.types); Debug("Object bounds..."); - read = fread(m_objects.bounds.data(), sizeof(Rect), m_objects.bounds.size(), file); - if (read != ObjectCount()) - Fatal("Only read %u objects!", read); + LoadStructVector(file, ObjectCount(), m_objects.bounds); Debug("Successfully loaded %u objects from \"%s\"", ObjectCount(), filename.c_str()); } diff --git a/src/ipdf.h b/src/ipdf.h index 4041bca..8ee30c3 100644 --- a/src/ipdf.h +++ b/src/ipdf.h @@ -16,6 +16,12 @@ namespace IPDF typedef unsigned ObjectID; typedef enum {RECT_FILLED, RECT_OUTLINE} ObjectType; + enum DocChunkTypes + { + CT_OBJTYPES, + CT_OBJBOUNDS + }; + struct Rect { Real x; Real y; Real w; Real h;