/*---------------------------------------------------------------------------------
 libBAG  Copyright (C) 2010 - 2010		
  BassAceGold - <BassAceGold@gmail.com>

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any
  damages arising from the use of this software.

  Permission is granted to anyone to use this software for any
  purpose, including commercial applications, and to alter it and
  redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you
     must not claim that you wrote the original software. If you use
     this software in a product, an acknowledgment in the product
     documentation would be appreciated but is not required.
	 
  2. Altered source versions must be plainly marked as such, and
     must not be misrepresented as being the original software.
	 
  3. Restricting the usage of or portions of code in this library
     from other software developers and or content creators
     is strictly prohibited in applications with software creation capabilities
     and or with the intent to profit from said restrictions.
	 
  4. This notice may not be removed or altered from any source
     distribution.
---------------------------------------------------------------------------------*/

#include "filesystem.h"
#include "fs_api.h"
#include <libBAG_Debug.h>

inline void rewind_ex( FILE * fp){
	fseek(fp, 0L, SEEK_SET);
	fat_clearerr( fp );
}



inline s8 BAG_Filesystem_FileExists(const char *path){
	struct stat st;
	return (lstat(path, &st) < 0) ? 0 : 1;
}


s8 BAG_Filesystem_GetMagicNumbers(const char *path, u8 *dataBuf, s8 numBytes){
	FILE *temp = fopen(path, "rb");
	if(temp == NULL){
    BAG_DBG_LibMsg(OPEN_FAIL_MSG, path);
		return ERR_END;
  }
		
	fread(dataBuf, numBytes, 1, temp); 
	fclose(temp);
	return ERR_NONE;
}



static inline s8 _checkFileExt(const char *path, const char *ext, char *newPath, int newPathLen){
	strncpy(newPath, path, newPathLen);
	strcat(newPath, ext);
	return BAG_Filesystem_FileExists(newPath);
}

s8 BAG_Filesystem_FindFileExt(const char *path, char *extList[], int extCount, char *newPath, int newPathLen){
    int i = 0;
    for(; i < extCount; i++){
        if(_checkFileExt(path, extList[i], newPath, newPathLen) == 1)
            return i;
    }
    BAG_DBG_LibMsg("BAG:Failed to find file with extensions\n");
    return -1;
}


inline u32 BAG_Filesystem_GetFileSize(const char *file){
	if(file == NULL){
    BAG_DBG_LibMsg(OPEN_FAIL_MSG, file);
		return 0;
  }
		
	struct stat st;
	memset(&st, 0, sizeof(struct stat));
	lstat(file, &st);
	return st.st_size;
}

u8 *BAG_Filesystem_LoadFileWithSize(const char *file, u32 *size){
    //get file size
    (*size) = BAG_Filesystem_GetFileSize(file);
    if(size == 0)
      return NULL;
        
    //open file
    FILE *temp = fopen(file, "rb");
    if(temp == NULL){
      BAG_DBG_LibMsg(OPEN_FAIL_MSG, file);
      return NULL;
    }

    //create buffer
    u8 *buffer = calloc(sizeof(u8), *size);
    if(buffer == NULL){
        BAG_DBG_LibMsg(BUFFER_FAILED_MSG, *size, "file");
        fclose(temp);
        return NULL;
    }
    
    fread(buffer, 1, *size, temp);
    fclose(temp);
    return buffer;
}

u8 *BAG_Filesystem_LoadFile(const char *file){
    u32 size = 0;
    return BAG_Filesystem_LoadFileWithSize(file, &size);
}



