/*                      QBCC Project

This source file is licensed under the GNU Lesser Public Licence which should be 
included with the distribution of this source.

*/
// General File operations

#include "../include/stdlibh.h"
#include "../include/stringh.h"
#include "../include/file.h"


///////////////////
// Initialize File System
int InitializeFileSystem(void)
{
	int n;

	// Allocate the room
	Files = (file_t *)malloc(sizeof(file_t) * MAX_FILES);

	if(!Files)
		return FALSE;

	// Initialize the data
	for(n=0;n<MAX_FILES;n++)
	{
		Files[n].used = FALSE;
		Files[n].ptr = NULL;
		Files[n].FMode = FL_INPUT;			// Default
		Files[n].reclen = 0;				// Default
	}

	return TRUE;
}

///////////////////
// Close FileSystem
int FreeFileSystem(void)
{
	int n = -1;

	// Close any open files	
	CLOSE(&n);

	free(Files);

	return TRUE;
}


///////////////////
// Get a free file slot
int *FREEFILE(void)
{
	int *n = (int *)GetPointer(sizeof(int));

	for(*n=0;*n<MAX_FILES;*n++)	
		if(!Files[*n].used)
			return n;

	*n = -1;
	return n;
}