#include "file.h"

/* Reads a file, given its path, a buffer to copy into, the buffer size, and mode. */
bool
file_read(const char* path, void* buffer, size_t size, const char* mode)
{
	SDL_RWops* io;

	printf(STRING_FILE_READ_OPEN, path);

	io = SDL_RWFromFile(path, mode);

	if (!io)
	{
		printf(STRING_FILE_READ_ERROR, path);
		return false;
	}

	SDL_RWread(io, buffer, size, 1);

	printf(STRING_FILE_READ_SUCCESS, path);

	SDL_RWclose(io);

	printf(STRING_FILE_CLOSED, path);

	return true;
}

/* Writes data to a file. */
bool
file_write(const char* path, void* data, size_t size, const char* mode)
{
	SDL_RWops* io;

	printf(STRING_FILE_WRITE_OPEN, path);

	io = SDL_RWFromFile(path, mode);

	if (!io)
	{
		printf(STRING_FILE_WRITE_ERROR, path);
		return false;
	}

	SDL_RWwrite(io, data, size, 1);

	printf(STRING_FILE_WRITE_SUCCESS, path);

	SDL_RWclose(io);

	printf(STRING_FILE_CLOSED, path);

	return true;
}
