8 void Document::Save(const string & filename)
10 Debug("Saving document to file \"%s\"...", filename.c_str());
11 FILE * file = fopen(filename.c_str(), "w");
13 Fatal("Couldn't open file \"%s\" - %s", filename.c_str(), strerror(errno));
16 Debug("Number of objects (%u)...", ObjectCount());
17 written = fwrite(&m_count, sizeof(m_count), 1, file);
19 Fatal("Failed to write number of objects!");
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);
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);
33 int err = fclose(file);
35 Fatal("Failed to close file \"%s\" - %s", filename.c_str(), strerror(err));
37 Debug("Successfully saved %u objects to \"%s\"", ObjectCount(), filename.c_str());
40 void Document::Load(const string & filename)
42 m_objects.bounds.clear();
46 Debug("Loaded empty document.");
49 Debug("Loading document from file \"%s\"", filename.c_str());
50 FILE * file = fopen(filename.c_str(), "r");
52 Fatal("Couldn't open file \"%s\"", filename.c_str(), strerror(errno));
55 read = fread(&m_count, sizeof(m_count), 1, file);
57 Fatal("Failed to read number of objects!");
58 Debug("Number of objects: %u", ObjectCount());
60 m_objects.types.resize(ObjectCount());
61 m_objects.bounds.resize(ObjectCount());
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);
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);
73 Debug("Successfully loaded %u objects from \"%s\"", ObjectCount(), filename.c_str());
76 void Document::Add(ObjectType type, const Rect & bounds)
78 m_objects.types.push_back(type);
79 m_objects.bounds.push_back(bounds);
83 void Document::DebugDumpObjects()
85 Debug("Objects for Document %p are:", this);
86 for (unsigned id = 0; id < ObjectCount(); ++id)
88 Debug("%u. \tType: %u\tBounds: %s", id, m_objects.types[id], m_objects.bounds[id].Str().c_str());
92 bool Document::operator==(const Document & equ) const
94 return (ObjectCount() == equ.ObjectCount() && memcmp(m_objects.bounds.data(), equ.m_objects.bounds.data(), ObjectCount() * sizeof(Rect)) == 0);