#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//
#include "proto.h"
#include "utils.h"
#include "bucket.h"



S32 write_file(char *file, U32 *size) {

  U32 i;

  if (*size == NOT_INLINE_FILE)
    if (load_file_into_memory(file, &file, size))           return 1;

  for (i = 0; i < *size; i++)   if (write_ubyte(file[i]))   return 1;

  return 0;
}


S32 load_file_into_memory(char *filename, char **buffer, U32 *size) {

  FILE *fh;
  U32 filesize;

  fh = fopen(filename, "rb");
  if (!fh)                              return 1;
  fseek(fh, 0, SEEK_END);
  filesize = (U32)ftell(fh);
  fseek(fh, 0, SEEK_SET);
  *buffer = malloc(filesize+1);
  if (!*buffer)                         return 1;
  fread(*buffer, 1, filesize, fh);
  fclose(fh);

  *size = filesize;

  return 0;
}
