// child.cpp (partial listing)

// Write one graphics object (tool) to file
// Called by WriteDocument.
// Local function. Not a member of TDrawChild.
void WriteTool(TGraphTool& tool, void * os)
{
  *(ofpstream *)os << &tool;
}

// Write document to named file. Closes file after.
// Assumes fileName is initialized.
// Returns TRUE for success; FALSE for failure
int
TDrawChild::WriteDocument()
{
  ofpstream os(fileName.c_str());
  if (os.bad())
    return FALSE;
  string signature(SIGNATURE);
  os << signature;
  unsigned count = image->GetItemsInContainer();
  os << count;
  image->ForEach(WriteTool, &os);
  fileSaved = TRUE;
  return TRUE;
}

// Read document from named file.
// Assumes fileName is initialized.
// Throw string exception for file error.
void
TDrawChild::ReadDocument()
{
  ifpstream is(fileName.c_str());
  if (is.bad())
    throw "Can't open file";
  string signature;
  is >> signature;
  if (signature != SIGNATURE)
    throw "Bad file format";
  unsigned count;
  is >> count;       // Get count of objects in file
  for (unsigned i = 0; i < count; i++) {
    TGraphTool* p;   // Pointer to each tool read from file
    is >> p;         // Read TGraphTool object
    image->Add(p);   // Add object to image collection
  }
}
