io.c: Add Remove_directory() and Remove_path()

Also implement WIN32 version of File_length()
This commit is contained in:
Thomas Bernard 2018-02-20 15:34:05 +01:00
parent 60beebc9ac
commit 854d787e59
4 changed files with 69 additions and 16 deletions

View File

@ -1743,10 +1743,10 @@ byte Button_Load_or_Save(T_Selector_settings *settings, byte load, T_IO_Context
// Si c'est un fichier // Si c'est un fichier
if (Selector->Position+Selector->Offset>=Filelist.Nb_directories) if (Selector->Position+Selector->Offset>=Filelist.Nb_directories)
// On efface le fichier (si on peut) // On efface le fichier (si on peut)
temp=(!remove(Selector_filename)); temp=(!Remove_path(Selector_filename));
else // Si c'est un repertoire else // Si c'est un repertoire
// On efface le repertoire (si on peut) // On efface le repertoire (si on peut)
temp=(!rmdir(Selector_filename)); temp=(!Remove_directory(Selector_filename));
if (temp) // temp indique si l'effacement s'est bien passé if (temp) // temp indique si l'effacement s'est bien passé
{ {

View File

@ -360,20 +360,43 @@ int File_is_hidden(const char *fname, const char *full_name)
return fname[0]=='.' && strcmp(fname, PARENT_DIR); return fname[0]=='.' && strcmp(fname, PARENT_DIR);
#endif #endif
} }
// Taille de fichier, en octets
int File_length(const char * fname) // File size in bytes
unsigned long File_length(const char * fname)
{ {
struct stat infos_fichier; #if defined(WIN32)
if (stat(fname,&infos_fichier)) WIN32_FILE_ATTRIBUTE_DATA infos;
return 0; if (GetFileAttributesExA(fname, GetFileExInfoStandard, &infos))
return infos_fichier.st_size; {
return (unsigned long)(((DWORD64)infos.nFileSizeHigh << 32) + (DWORD64)infos.nFileSizeLow);
}
else
return 0;
#else
struct stat infos_fichier;
if (stat(fname,&infos_fichier))
return 0;
return infos_fichier.st_size;
#endif
} }
int File_length_file(FILE * file)
unsigned long File_length_file(FILE * file)
{ {
struct stat infos_fichier; #if defined(WIN32)
if (fstat(fileno(file),&infos_fichier)) // revert to old school way of finding file size
return 0; long offset_backup;
return infos_fichier.st_size; long file_length;
offset_backup = ftell(file);
fseek(file, 0, SEEK_END);
file_length = ftell(file);
fseek(file, offset_backup, SEEK_SET);
return (unsigned long)file_length;
#else
struct stat infos_fichier;
if (fstat(fileno(file),&infos_fichier))
return 0;
return infos_fichier.st_size;
#endif
} }
void For_each_file(const char * directory_name, void Callback(const char *, const char *)) void For_each_file(const char * directory_name, void Callback(const char *, const char *))
@ -683,3 +706,25 @@ int Change_directory(const char * path)
return chdir(path); return chdir(path);
#endif #endif
} }
int Remove_path(const char * path)
{
#if defined(WIN32)
return (DeleteFileA(path) ? 0 : -1);
#elif defined(__linux__)
return unlink(path);
#else
return remove(path);
#endif
}
///
/// Remove the directory
int Remove_directory(const char * path)
{
#if defined(WIN32)
return RemoveDirectoryA(path) ? 0 : -1;
#else
return rmdir(path);
#endif
}

View File

@ -82,10 +82,10 @@ char * Find_last_separator(const char * str);
#endif #endif
/// Size of a file, in bytes. Returns 0 in case of error. /// Size of a file, in bytes. Returns 0 in case of error.
int File_length(const char *fname); unsigned long File_length(const char *fname);
/// Size of a file, in bytes. Takes an open file as argument, returns 0 in case of error. /// Size of a file, in bytes. Takes an open file as argument, returns 0 in case of error.
int File_length_file(FILE * file); unsigned long File_length_file(FILE * file);
/// Returns true if a file passed as a parameter exists in the current directory. /// Returns true if a file passed as a parameter exists in the current directory.
int File_exists(const char * fname); int File_exists(const char * fname);
@ -135,3 +135,11 @@ const char * Get_current_directory(char * buf, word * buf_unicode, size_t size);
/// ///
/// Change current directory. return 0 for success, -1 in case of error /// Change current directory. return 0 for success, -1 in case of error
int Change_directory(const char * path); int Change_directory(const char * path);
///
/// Remove the file
int Remove_path(const char * path);
///
/// Remove the directory
int Remove_directory(const char * path);

View File

@ -1941,5 +1941,5 @@ void Remove_file(T_IO_Context *context)
Get_full_filename(filename, context->File_name, context->File_directory); Get_full_filename(filename, context->File_name, context->File_directory);
remove(filename); Remove_path(filename);
} }