#include "asset.h"

char * assetLoadString(char *assetName) {
  // Open a buffer.
  FILE *fptr = assetBufferOpen(assetName);
  if(fptr == NULL) return NULL;

  // Read the count of bytes in the file
  fseek(fptr, 0, SEEK_END);// Seek to the end
  size_t length = ftell(fptr);// Get our current position (the end)
  fseek(fptr, 0, SEEK_SET);// Reset the seek 

  // Create the string buffer
  char *str = malloc(length + 1);// Add 1 for the null terminator.
  if(str == NULL) {
    assetBufferClose(fptr);
    return NULL;
  }

  // Read and seal the string.
  fread(str, 1, length, fptr);// Read all the bytes
  str[length] = '\0';// Null terminate.
  assetBufferClose(fptr); // Close the buffer.

  return str;
}

FILE * assetBufferOpen(char *assetName) {
  // Get the directory based on the raw input by creating a new string.
  size_t lenAsset = strlen(assetName);// Get the length of asset
  size_t lenPrefix = strlen(ASSET_PREFIX);// Get the length of the prefix
  
  // Create str to house both the prefix and asset, and null terminator
  char *joined = malloc(lenAsset + lenPrefix + 1);
  if(joined == NULL) return NULL;// Mem okay?

  joined[0] = '\0';//Start at null
  strcat(joined, ASSET_PREFIX);//Add prefix
  strcat(joined, assetName);//Add body

  // Open the file pointer now.
  FILE *fptr = fopen(joined, "rb");
  free(joined);// Free the string we just created
  if(!fptr) return NULL;// File available?
  return fptr;
}

bool assetBufferClose(FILE *buffer) {
  return fclose(buffer);
}

int32_t assetBufferRead(FILE *buffer, char *data, int32_t size) {
  return (int32_t)fread(data, 1, size, buffer);
}

int32_t assetBufferEnd(FILE *buffer) {
  return feof(buffer);
}

void assetBufferSkip(FILE *buffer, int32_t n) {
  fseek(buffer, n, SEEK_CUR);
}