Logo Search packages:      
Sourcecode: cbmconvert version File versions  Download package

static enum WrStatus writeFile ( const struct Filename name,
const byte_t data,
size_t  length 
) [static]

Write a file

Parameters:
name native (PETSCII) name of the file
data the contents of the file
length length of the file contents
Returns:
status of the operation

Definition at line 112 of file main.c.

References Always, archiveFilename, changeDisks, CloseImage(), Errors, Everything, ImFail, ImNoSpace, ImOK, OpenImage(), PATH_SEPARATOR, Sometimes, Filename::type, Warnings, WrFail, WrFileExists, WriteArchive(), writeFunc, writeLog(), WrNoSpace, and WrOK.

Referenced by main().

{
  enum WrStatus status = WrFail;

  if (!image && !writeFunc)
    return status;

  if (!length) {
    writeLog (Errors, name, "Not writing zero length file");
    return WrFail;
  }

  if (image) {
    status = (*writeImageFunc) (name, data, length, image, writeLog);
    switch (status) {
    case WrOK:
      writeLog (Everything, name, "Wrote %u bytes to image \"%s\"",
            length, image->name);
      return WrOK;
    case WrFail:
      writeLog (Errors, name, "Write failed!");
      return WrFail;
    case WrFileExists:
      if (changeDisks < Always) {
      writeLog (Errors, name, "non-unique file name!");
      return WrFileExists;
      }
      /* non-unique file name, fall through */
    case WrNoSpace:
      if (changeDisks < Sometimes) {
      writeLog (Errors, name, "out of space!");
      return WrNoSpace;
      }

      /* try to open a new disk image */
      writeLog (Warnings, name,
            status == WrFileExists
            ? "non-unique file name, changing disk images..."
            : "out of space, changing disk images...");
      switch (CloseImage (image)) {
      unsigned char* c;
      case ImNoSpace:
      writeLog (Errors, name, "out of space");
      free (image);
      image = 0;
      return WrNoSpace;
      case ImFail:
      writeLog (Errors, name, "failed");
      free (image);
      image = 0;
      return WrFail;
      case ImOK:
      writeLog (Everything, name, "wrote old image \"%s\"", image->name);
      /*
      ** Update the file name.  If there is a number in the first
      ** component of the file name (excluding any directory component),
      ** increment it.
      */
      for (c = image->name; *c; c++);
      for (; c >= image->name && *c != PATH_SEPARATOR; c--);
      for (c++; *c && *c != '.'; c++);
      while (--c >= image->name)
        if (*c >= '0' && *c < '9') {
          (*c)++;
          break;
        }
        else if (*c == '9')
          *c = '0';
        else
          goto notUnique;

      if (c < image->name) {
      notUnique:
        writeLog (Errors, name, "Could not generate unique image file name");
        free (image);
        image = 0;
        return WrFail;
      }

      writeLog (Everything, name, "Continuing to image \"%s\"...",
              image->name);
      {
        char* filename = (char*) image->name;
        enum ImageType type = image->type;
        enum DirEntOpts direntOpts = image->direntOpts;

        free (image);
        image = 0;

        status = OpenImage (filename, &image, type, direntOpts);
      }

      switch (status) {
      case ImOK:
        status = (*writeImageFunc) (name, data, length, image, writeLog);

        if (status == WrOK)
          writeLog (Everything, name, "OK, wrote %u bytes to image \"%s\"",
                  length, image->name);
        else
          writeLog (Errors, name, "%s while writing to \"%s\", giving up.",
                  status == WrNoSpace ? "out of space" :
                  status == WrFileExists ? "duplicate file name" :
                  "failed",
                  image->name);

        return status;

      default:
        writeLog (Errors, name, "%s while creating image \"%s\"",
                status == ImNoSpace ? "out of space" : "failed",
                image->name);
        return status;
      }
      }
    }
  }
  else if (archive) {
    status = WriteArchive (name, data, length, archive, writeLog);
    switch (status) {
    case WrOK:
      writeLog (Everything, name, "Wrote %u bytes to archive \"%s\"",
            length, archiveFilename);
      return WrOK;
    case WrFail:
      writeLog (Errors, name, "Write failed!");
      return WrFail;
    case WrFileExists:
      writeLog (Errors, name, "non-unique file name!");
      return WrFileExists;
    case WrNoSpace:
      writeLog (Errors, name, "out of space!");
      return WrNoSpace;
    }
  }
  else {
    char* newname = 0;

    status = (*writeFunc) (name, data, length, &newname, writeLog);

    if (status == WrOK)
      writeLog (Everything, name, "Writing %u bytes to \"%s\"",
            length, newname);
    else
      writeLog (Errors, name, "%s while writing to \"%s\"",
            status == ImNoSpace ? "out of space" : "failed",
            newname);

    free (newname);
    return status;
  }

  return status;
}


Generated by  Doxygen 1.6.0   Back to index