Files
c-prime/application/target/fileSystem.c

264 lines
3.6 KiB
C
Raw Normal View History

2025-11-17 10:28:09 +01:00
/*
* This file is automaticaly generated, Please dont edit this file!
*/
#include <fileSystem.h>
struct fileSystem * filesystem;
void fileSystem_writeFile( fileSystem * this, char * filepath, char * data ) {
FILE *fp = fopen( filepath, "wb" );
if (fp != NULL)
{
fputs( data, fp );
fclose( fp );
}
}
struct array * fileSystem_readDir( fileSystem * this, char * filePath ) {
DIR * dir;
struct dirent * entry;
struct array * files = array_newPointer();
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 );
}
}
}
return files;
}
struct text * fileSystem_readFile( fileSystem * this, char * name, char * mode ) {
char * readMode;
if( char_operator_compare( 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 );
fseek( file, 0, SEEK_SET );
char * buffer = malloc( sizeof( char ) * ( length + 1 ) );
fread( buffer, sizeof( char ), length, file );
fclose( file );
if( char_operator_compare( mode , "utf8") ) {
buffer[ length ] = 0;
}
text * output = text_newPointer( "" );
output->length = 0;
text_appendBinary( output, buffer, length );
return output;
}
char * fileSystem_readBinaryFile( fileSystem * this, char * name, char * mode, int * size ) {
char * readMode;
if( char_operator_compare( 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 );
*size = length;
printf("strlen: %i \n\n", length);
return buffer;
}
int fileSystem_ensureDirectory( fileSystem * this, char * path ) {
char * pathCopy = char_copy( path );
struct array * parts = char_split( pathCopy, "/" );
int count = array_length( parts );
for ( int i = 1; i < count; ++i )
{
struct array * tempParts = char_split( pathCopy, "/" );
for ( int j = 0; j < count-i-1; ++j )
{
array_pop( tempParts );
}
char * tempPath = array_join( tempParts, "/" );
if( fileSystem_exists( this, tempPath ) ) {
} else {
fileSystem_makeDirectory( this, tempPath );
}
}
return 1;
}
int fileSystem_makeDirectory( fileSystem * this, char * path ) {
if ( fileSystem_exists( this, path ) == NULL ) {
mkdir( path, 0700 );
}
return 1;
}
int fileSystem_exists( fileSystem * this, char * path ) {
if ( access( path, F_OK ) == 0 ) {
return 1;
} else {
return 0;
}
}
fileSystem fileSystem_new() {
fileSystem instance;
instance.__classIndex = 11;
return instance;
}
fileSystem * fileSystem_newPointer() {
struct fileSystem * pointer = malloc( sizeof ( struct fileSystem ) );
pointer->__classIndex = 11;
return pointer;
}