
#include "array.h"

#include "text.h"

#include "file.h"

#include "fileSystem.h"


class cache {

	struct array * files	= new array();
	
	text * getFile( char * filePath ) {

		struct file * currentFile = this->getCachedFileByPath( filePath ); 

		if( currentFile == NULL ) {

			//printf("reading new file:	%s\n\n", filePath);

			struct text * content 		= filesystem->readFile( filePath, "binary" );

			this->addFile( filePath, content );

			return content;

		} else {

			return currentFile->content;

		}

	}

	addFile( char * filePath, struct text * content ) {

		struct file * newFile = new file();

		newFile->filePath		= filePath;

		newFile->content		= content; 

		this->files->add( newFile );

	}

	struct file * getCachedFileByPath( char * filePath ) {

		struct array * files 	= this->files;

		int count 				= files->length();

		for (int i = 0; i < count; ++i)
		{
			
			file * currentFile 	= files->get( i );

			if( currentFile->filePath == filePath ) {

				//printf("using cached file:	%s\n\n", filePath);

				return currentFile;

			}

		}

		return NULL;

	}


}