Conoscere caratteristiche generali dei file
Per ottenere informazioni di tipo generale sui file, è possibile operare con risorse di Gambas o del linguaggio C.
Uso della Classe Stat() di Gambas
Per l'uso della risorsa Stat() di Gambas rimandiamo a questa pagina della Wiki.
Uso della funzione stat() di C
La funzione stat() di C potrà essere richiamata in modo diretto come funzione esterna mediante Extern, come descritto nella seguente pagina della Wiki, oppure in modo indiretto utilizzandola all'interno di una libreria esterna condivisa da noi creata ad hoc.
In questo secondo caso, il sorgente della libreria condivisa ad hoc, che salveremo ad esempio nella cartella del sorgente del programma Gambas, potrà essere il seguente:
#include <sys/stat.h> struct stat status; int Chiama_stat(const char *s, struct stat *st) { int r; r = stat(s, &status); *st = status; return r; }
Il codice Gambas del programma principale sarà:
Public Struct STATUS_File st_dev As Long st_ino As Long st_nlink As Long st_mode As Integer st_uid As Integer st_gid As Long st_rdev As Long st_size As Long st_blksize As Long st_blocks As Long st_atime As Long unused1 As Integer st_mtime As Long unused2 As Long st_ctime As Long __glibc_reserved[3] As Long End Struct Library "libc:6" ' tm* localtime( const time_t *time ) ' Converts given time since epoch as time_t value into calendar time, expressed in local time. Private Extern localtime(tm As Pointer) As Pointer ' char* asctime( const tm* time_ptr ) ' Converts given calendar time tm to a textual representation. Private Extern asctime(time_ptr As Pointer) As String Private Extern Chiama_stat(nomefile As String, stFile As STATUS_File) As Integer In "/tmp/libSTATUS" Public Sub Main() Dim i As Integer Dim stS As New STATUS_File Dim at, mt, ct As Long Dim s As String = "/percorso/del/file" ' Generiamo la nostra libreria esterna ad hoc: Shell "gcc -o /tmp/libSTATUS.so " & Application.Path &/ "libSTATUS.c -shared -fPIC" Wait i = Chiama_stat(s, stS) If i <> 0 Then Error.Raise("Errore nella chiamata della funzione 'Chiama_stat()' !") Print "File controllato: "; s Print With stS Print .st_dev Print .st_ino Print .st_nlink Print .st_mode Print .st_uid Print .st_gid Print .st_rdev Print .st_size; " byte" Print .st_blksize Print .st_blocks at = .st_atime mt = .st_mtime ct = .st_ctime End With Print asctime(localtime(VarPtr(at))) Print asctime(localtime(VarPtr(mt))) Print asctime(localtime(VarPtr(ct))) End
Uso della funzione g_stat() della libreria Glib
Si potrà utilizzare anche la funzione g_stat() della libreria "libglib-2.0.so", la quale passerà per riferimento una Struttura identica a quella già vista sopra e definita nel file /bits/stat.h.
Il codice Gambas sarà:
Public Struct STATUS_File st_dev As Long st_ino As Long st_nlink As Long st_mode As Integer st_uid As Integer st_gid As Long st_rdev As Long st_size As Long st_blksize As Long st_blocks As Long st_atime As Long unused1 As Integer st_mtime As Long unused2 As Long st_ctime As Long __glibc_reserved[3] As Long End Struct ' int g_stat (const gchar *filename, GStatBuf *buf) ' Returns information about a file. Private Extern g_stat(filename As String, buf As STATUS_File) As Integer In "libglib-2.0" Library "libc:6" ' tm* localtime( const time_t *time ) ' Converts given time since epoch as time_t value into calendar time, expressed in local time. Private Extern localtime(tm As Pointer) As Pointer ' char* asctime( const tm* time_ptr ) ' Converts given calendar time tm to a textual representation. Private Extern asctime(time_ptr As Pointer) As String Public Sub Main() Dim sta As New STATUS_File Dim at, mt, ct As Long g_stat("/percorso/del/file/", sta) With sta Print .st_dev Print .st_ino Print .st_nlink Print .st_mode Print .st_uid Print .st_gid Print .st_rdev Print .st_size; " byte" Print .st_blksize Print .st_blocks at = .st_atime mt = .st_mtime ct = .st_ctime End With Print asctime(localtime(VarPtr(at))) Print asctime(localtime(VarPtr(mt))) Print asctime(localtime(VarPtr(ct))) End