#include <stdio.h>
#include <stdlib.h>
#include <fat.h>

#include "../io.h"

void io_init()
{
	fatInitDefault();
}
int io_file_read(char *filename, unsigned char *buffer, int size)
{
	int readsize=0, bytes_read;
	FILE *fp;

	if(!(fp = fopen(filename, "rb")))
		return 0;
	while(readsize < size)
	{
		bytes_read = fread(buffer + readsize, 1, 0x200, fp);
		if(bytes_read == 0)
			break;
		readsize += bytes_read;
	}
	fclose(fp);

	return readsize;
}
int io_file_write(char *filename, unsigned char *buffer, int size)
{
	FILE *fp;
	int writesize=0, bytes_write;

	if(!(fp = fopen(filename, "wb")))	
		return 0;
	
	while(writesize < size)
	{
		bytes_write = fwrite(buffer + writesize, 1, 0x200, fp);
		if(bytes_write == 0)
			break;
		writesize += bytes_write;
	}
	fclose(fp);

	return writesize;
}
