

#include <http.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <array.h>

#include <console.h>



#include <char.h>

#include <dirent.h> // "DIR *"

#include <unistd.h> // access

#include <sys/types.h>

#include <sys/stat.h>


extern struct fileSystem * filesystem;

class fileSystem{

	void writeFile( char * filepath, char * data )
	{
	    FILE *fp = fopen( filepath, "wb" );

	    if (fp != NULL)
	    {
	        fputs( data, fp );
	        
	        fclose( fp );
	    }

	}

	struct array * readDir( char * filePath ) {

		DIR * dir;

		struct dirent * entry;

		struct array * files = new array();

		if ( ( dir = opendir( filePath ) ) == NULL ){

			 perror("opendir() error");

		} else {

			while( ( entry = readdir( dir ) ) != NULL ) {

				char * filename = (char *)entry->d_name;

				if ( strcmp( filename, ".." ) != 0 && strcmp( filename, "." ) != 0 ) {

					array_add( files, filename );

					//printf("%s\n", filename);

				}
			}
		}

		return files;
		//closedir(dir);
	}


	struct text * readFile( char * name, char * mode ) {

		char * readMode;

		if( mode == "utf8" ) {

			readMode	= "r";

		} else {

			readMode	= "rb";

		}

		FILE * file = fopen( name, readMode );

		if ( file == NULL ) {

			fprintf( stderr, "Error: Can't open file '%s'.", name );

			exit( EXIT_FAILURE );

		}
		
		fseek( file, 0, SEEK_END );

		long length = ftell( file );

		//printf("buffer length is '%i'	\n\n", length);

		fseek( file, 0, SEEK_SET );

		char * buffer = malloc( sizeof( char ) * ( length + 1 ) );

		fread( buffer, sizeof( char ), length, file );

		fclose( file );

		if( mode == "utf8" ) {

			buffer[ length ] = 0;

		}

		text * output	= new text( "" );

		output->length	= 0;

		output->appendBinary( buffer, length );

		return output;

	}

	

	char * readBinaryFile( char * name, char * mode, int * size ) {

		char * readMode;

		if( mode == "utf8" ) {

			readMode	= "r";

		} else {

			readMode	= "rb";

			printf("readmode = rb  binary\n\n");

		}

		FILE * file = fopen( name, readMode );

		if ( file == NULL ) {

			fprintf( stderr, "Error: Can't open file '%s'.", name );

			exit( EXIT_FAILURE );

		}
		
		fseek( file, 0, SEEK_END );

		long length = ftell( file );

		printf("buffer length is '%i'	\n\n", length);

		fseek( file, 0, SEEK_SET );

		char * buffer = malloc( sizeof( char ) * ( length + 1 ) );

		buffer[ length ] = '\0';

		fread( buffer, sizeof( char ), length, file );

		fclose( file );


		//buffer[ length ] = 0;

		*size = length;

		printf("strlen:		%i \n\n", length);

		return buffer;

	}

	int ensureDirectory( char * path ) {

		char *	pathCopy		= path->copy();

		struct array * parts	= pathCopy->split( "/" );

		int count				= parts->length(  );

		for ( int i = 1; i < count; ++i )
		{
			struct array * tempParts	= pathCopy->split( "/" );

			for ( int j = 0; j < count-i-1; ++j )
			{
				tempParts->pop();
			}

			char * tempPath		= tempParts->join( "/" );

			if( this->exists( tempPath ) ) {

			} else {

				this->makeDirectory( tempPath );

			}

		}

		return 1;

	}

	int makeDirectory( char * path ) {

		if ( this->exists( path ) == NULL ) {

			mkdir( path, 0700 );

		}

		return 1;

	}

	int exists( char * path ) {

		if ( access( path, F_OK ) == 0 ) {

			return 1;

		} else {

			return 0;

		}

	}

	int isDirectory( char * path ) {

		struct stat path_stat;

		stat( path, & path_stat );

		if( S_ISREG( path_stat.st_mode ) ) {

			return 0;

		} else {

			return 1;

		}

	}

}