Initial outline rendering.
[ipdf/code.git] / src / document.cpp
1 #include "document.h"
2
3 #include <cstdio>
4
5 using namespace IPDF;
6 using namespace std;
7
8 void Document::Save(const string & filename)
9 {
10         Debug("Saving document to file \"%s\"...", filename.c_str());
11         FILE * file = fopen(filename.c_str(), "w");
12         if (file == NULL)
13                 Fatal("Couldn't open file \"%s\" - %s", filename.c_str(), strerror(errno));
14
15         size_t written;
16         Debug("Number of objects (%u)...", ObjectCount());
17         written = fwrite(&m_count, sizeof(m_count), 1, file);
18         if (written != 1)
19                 Fatal("Failed to write number of objects!");
20
21         Debug("Object bounds...");
22         written = fwrite(m_objects.bounds.data(), sizeof(Rect), m_objects.bounds.size(), file);
23         if (written != ObjectCount())
24                 Fatal("Only wrote %u objects!", written);
25
26         int err = fclose(file);
27         if (err != 0)
28                 Fatal("Failed to close file \"%s\" - %s", filename.c_str(), strerror(err));
29
30         Debug("Successfully saved %u objects to \"%s\"", ObjectCount(), filename.c_str());
31 }
32
33 void Document::Load(const string & filename)
34 {
35         m_objects.bounds.clear();
36         m_count = 0;
37         if (filename == "")
38         {
39                 Debug("Loaded empty document.");
40                 return;
41         }
42         Debug("Loading document from file \"%s\"", filename.c_str());
43         FILE * file = fopen(filename.c_str(), "r");
44         if (file == NULL)
45                 Fatal("Couldn't open file \"%s\"", filename.c_str(), strerror(errno));
46
47         size_t read;
48         read = fread(&m_count, sizeof(m_count), 1, file);
49         if (read != 1)
50                 Fatal("Failed to read number of objects!");
51         Debug("Number of objects: %u", ObjectCount());
52
53         m_objects.bounds.resize(ObjectCount());
54         
55         Debug("Object bounds...");
56         read = fread(m_objects.bounds.data(), sizeof(Rect), m_objects.bounds.size(), file);
57         if (read != ObjectCount())
58                 Fatal("Only read %u objects!", read);
59         
60         Debug("Successfully loaded %u objects from \"%s\"", ObjectCount(), filename.c_str());
61 }
62
63 void Document::Add(Real x, Real y, Real w, Real h)
64 {
65         m_objects.bounds.push_back(Rect(x, y, w, h));
66         m_count++;
67 }
68
69 void Document::DebugDumpObjects()
70 {
71         Debug("Objects for Document %p are:", this);
72         for (unsigned id = 0; id < ObjectCount(); ++id)
73         {
74                 Debug("%u.\t%s", id, m_objects.bounds[id].Str().c_str());
75         }
76 }
77
78 bool Document::operator==(const Document & equ) const
79 {
80         return (ObjectCount() == equ.ObjectCount() && memcmp(m_objects.bounds.data(), equ.m_objects.bounds.data(), ObjectCount() * sizeof(Rect)) == 0);
81 }

UCC git Repository :: git.ucc.asn.au