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 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);
26 int err = fclose(file);
28 Fatal("Failed to close file \"%s\" - %s", filename.c_str(), strerror(err));
30 Debug("Successfully saved %u objects to \"%s\"", ObjectCount(), filename.c_str());
33 void Document::Load(const string & filename)
35 m_objects.bounds.clear();
39 Debug("Loaded empty document.");
42 Debug("Loading document from file \"%s\"", filename.c_str());
43 FILE * file = fopen(filename.c_str(), "r");
45 Fatal("Couldn't open file \"%s\"", filename.c_str(), strerror(errno));
48 read = fread(&m_count, sizeof(m_count), 1, file);
50 Fatal("Failed to read number of objects!");
51 Debug("Number of objects: %u", ObjectCount());
53 m_objects.bounds.resize(ObjectCount());
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);
60 Debug("Successfully loaded %u objects from \"%s\"", ObjectCount(), filename.c_str());
63 void Document::Add(Real x, Real y, Real w, Real h)
65 m_objects.bounds.push_back(Rect(x, y, w, h));
69 void Document::DebugDumpObjects()
71 Debug("Objects for Document %p are:", this);
72 for (unsigned id = 0; id < ObjectCount(); ++id)
74 Debug("%u.\t%s", id, m_objects.bounds[id].Str().c_str());
78 bool Document::operator==(const Document & equ) const
80 return (ObjectCount() == equ.ObjectCount() && memcmp(m_objects.bounds.data(), equ.m_objects.bounds.data(), ObjectCount() * sizeof(Rect)) == 0);