
#include "application.h"


struct application * application_new() {

	struct application * applicationInstance	= malloc( sizeof( struct application ) );

	applicationInstance->classes				= array_new();

	applicationInstance->functions				= array_new();

	return applicationInstance;

}

void application_initializeGlobals() {

	allClasses						= array_new();

	allFunctions					= array_new();
	
}

void removeFilesFromPath( char * path ) {

	printf("Delete all files from this path:		%s\n\n", path);

}

array * application_extractIncludedFilePaths( application  * currentApplication ) {

	char * path										= currentApplication->input;

	struct array * unloadedFileNames				= array_new();

	struct array * loadedFileNames					= array_new();


	file_loadUnloadedFiles( unloadedFileNames, loadedFileNames, path );

	printf("%i\n", array_length( loadedFileNames ) );

	return loadedFileNames;

}

struct application * application_parseArguments( int argc, char * * argv ) {

	application * applicationInstance	= application_new();
	
	for ( int i = 0; i < argc; ++i )
	{
		
		char * currentArgument = argv[ i ];

		if( fileSystem_textIsFilename( currentArgument ) ) {

			applicationInstance->input = currentArgument;

		}

		if( strcmp( currentArgument, "-o" ) == 0 ) {

			i++;

			applicationInstance->output = argv[ i ];

		}

	}



	printf("\n\ninput:		%s\n", applicationInstance->input);

	printf("output:		%s\n\n\n", applicationInstance->output);


	fileSystem_ensureDirectory( applicationInstance->output );


	return applicationInstance;

}

struct class * application_getClassByClassName( array * classesArray, char * className ) {

	int count = array_length( classesArray );

	for (int i = 0; i < count; ++i)
	{
		struct class * currentClass = array_get( classesArray, i );

		if ( strcmp( currentClass->className, className ) == 0 ) {

			return currentClass;

		}

	}

	return NULL;

}

void application_printMemoryUsage() {

	struct rusage r_usage;
	
	getrusage( RUSAGE_SELF, &r_usage );

	printf("Memory usage: %ld kilobytes\n",r_usage.ru_maxrss);

}
