+void Document::Save(const string & filename)
+{
+ Debug("Saving document to file \"%s\"...", filename.c_str());
+ FILE * file = fopen(filename.c_str(), "w");
+ if (file == NULL)
+ Fatal("Couldn't open file \"%s\" - %s", filename.c_str(), strerror(errno));
+
+ size_t written;
+ Debug("Number of objects (%u)...", ObjectCount());
+ written = fwrite(&m_count, sizeof(m_count), 1, file);
+ if (written != 1)
+ 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);
+
+ 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);
+
+
+
+ int err = fclose(file);
+ if (err != 0)
+ Fatal("Failed to close file \"%s\" - %s", filename.c_str(), strerror(err));
+
+ Debug("Successfully saved %u objects to \"%s\"", ObjectCount(), filename.c_str());
+}
+