Beispiel #1
0
/**
 * Writes a Huffman code descriptor to file.
 *
 * @param filename  Path to file where descriptor is to be saved.
 * @param hc        Pointer to the descriptor block to save.
 * @return          Boolean: true for all OK, false for error.
 */
int
WriteHCD(char *filename, HCD *hc)
{
  FILE *fd;

  if ((fd = fopen(filename, "wb")) == NULL) {
    perror(filename);
    return 0;
  }
  else {
    NwriteInt(hc->size, fd);
    NwriteInt(hc->length, fd);
    NwriteInt(hc->min_codelen, fd);
    NwriteInt(hc->max_codelen, fd);

    NwriteInts(hc->lcount, MAXCODELEN, fd);
    NwriteInts(hc->symindex, MAXCODELEN, fd);
    NwriteInts(hc->min_code, MAXCODELEN, fd);

    assert(hc->symbols);
    NwriteInts(hc->symbols, hc->size, fd);

    fclose(fd);
    return 1;
  }
}
Beispiel #2
0
/**
 * Writes the data stored in a blob to file.
 *
 * In Windows, the write is always binary-mode.
 *
 * @param filename           The file to write to.
 * @param blob               The MemBlob to write to file.
 * @param convert_to_nbo     boolean: if true, data is converted to
 *                           network byte order before it's written.
 * @return                   0 on failure, 1 if everything went fine.
 */
int
write_file_from_blob(char *filename,
                     MemBlob *blob,
                     int convert_to_nbo)
{
  int result = 0;
  FILE *fd;

  assert("You must not pass a NULL blob!" && (blob != NULL));

  blob->changed = 0;

  if ((blob->data == NULL) || (blob->size == 0)) {
   Rprintf( "storage:write_file_from_blob():\n"
            "  no data in blob, nothing to write...\n");
    result = 0;
  }
  else {

    switch (blob->allocation_method) {
    case UNALLOCATED:
     Rprintf( "storage:write_file_from_blob():\n"
              "  tried to write unallocated blob...\n");
      result = 0;
      break;
    case MMAPPED:
    case MALLOCED:
      if ((fd = fopen(filename, "wb")) == NULL) {
       Rprintf( "storage:write_file_from_blob():\n"
                "  Can't open output file %s\n", filename);
        result = 0;
      }
      else {
        if (convert_to_nbo)
          NwriteInts((int *)blob->data, blob->size/4, fd);
        else
          fwrite(blob->data, 1, blob->size, fd);
        fclose(fd);
        result = 1;
      }
      break;
    default:
     Rprintf( "storage:write_file_from_blob():\n"
              "  unsupported allocation method %d...\n", blob->allocation_method);
      result = 0;
      break;
    }
  }

  return result;
}