Add ObjectType for Object Type
[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 types...");
22         written = fwrite(m_objects.types.data(), sizeof(ObjectType), m_objects.types.size(), file);
23         if (written != ObjectCount())
24                 Fatal("Only wrote %u objects!", written);
25
26         Debug("Object bounds...");
27         written = fwrite(m_objects.bounds.data(), sizeof(Rect), m_objects.bounds.size(), file);
28         if (written != ObjectCount())
29                 Fatal("Only wrote %u objects!", written);
30
31
32
33         int err = fclose(file);
34         if (err != 0)
35                 Fatal("Failed to close file \"%s\" - %s", filename.c_str(), strerror(err));
36
37         Debug("Successfully saved %u objects to \"%s\"", ObjectCount(), filename.c_str());
38 }
39
40 void Document::Load(const string & filename)
41 {
42         m_objects.bounds.clear();
43         m_count = 0;
44         if (filename == "")
45         {
46                 Debug("Loaded empty document.");
47                 return;
48         }
49         Debug("Loading document from file \"%s\"", filename.c_str());
50         FILE * file = fopen(filename.c_str(), "r");
51         if (file == NULL)
52                 Fatal("Couldn't open file \"%s\"", filename.c_str(), strerror(errno));
53
54         size_t read;
55         read = fread(&m_count, sizeof(m_count), 1, file);
56         if (read != 1)
57                 Fatal("Failed to read number of objects!");
58         Debug("Number of objects: %u", ObjectCount());
59
60         m_objects.types.resize(ObjectCount());
61         m_objects.bounds.resize(ObjectCount());
62
63         Debug("Object types...");
64         read = fread(m_objects.types.data(), sizeof(ObjectType), m_objects.types.size(), file);
65         if (read != ObjectCount())
66                 Fatal("Only read %u objects!", read);
67         
68         Debug("Object bounds...");
69         read = fread(m_objects.bounds.data(), sizeof(Rect), m_objects.bounds.size(), file);
70         if (read != ObjectCount())
71                 Fatal("Only read %u objects!", read);
72         
73         Debug("Successfully loaded %u objects from \"%s\"", ObjectCount(), filename.c_str());
74 }
75
76 void Document::Add(ObjectType type, const Rect & bounds)
77 {
78         m_objects.types.push_back(type);
79         m_objects.bounds.push_back(bounds);
80         m_count++;
81 }
82
83 void Document::DebugDumpObjects()
84 {
85         Debug("Objects for Document %p are:", this);
86         for (unsigned id = 0; id < ObjectCount(); ++id)
87         {
88                 Debug("%u. \tType: %u\tBounds: %s", id, m_objects.types[id], m_objects.bounds[id].Str().c_str());
89         }
90 }
91
92 bool Document::operator==(const Document & equ) const
93 {
94         return (ObjectCount() == equ.ObjectCount() && memcmp(m_objects.bounds.data(), equ.m_objects.bounds.data(), ObjectCount() * sizeof(Rect)) == 0);
95 }

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