Differenze tra le versioni di "Conoscere caratteristiche generali dei file"

Da Gambas-it.org - Wikipedia.
Riga 105: Riga 105:
 
   <FONT Color=gray>' ''int g_stat (const gchar *filename, GStatBuf *buf)''
 
   <FONT Color=gray>' ''int g_stat (const gchar *filename, GStatBuf *buf)''
 
  ' ''Returns information about a file.''</font>
 
  ' ''Returns information about a file.''</font>
  Private Extern g_stat(filename As String, buf As STATUS_File) As Integer In "libglib-2.0:0.7200.4"
+
  Private Extern g_stat(filename As String, buf As STATUS_File) As Integer In "libglib-2.0:0.8000.2"
 
   
 
   
 
  Library "libc:6"
 
  Library "libc:6"
Riga 142: Riga 142:
 
   Dim at, mt, ct As Long
 
   Dim at, mt, ct As Long
 
   
 
   
   <FONT Color=#B222222>g_stat("FONT Color=darkgreen>''/percorso/del/file/''</font>", sta)</font>
+
   <FONT Color=#B222222>g_stat("<FONT Color=darkgreen>''/percorso/del/file/''</font>", sta)</font>
 
      
 
      
 
   With sta
 
   With sta

Versione delle 12:53, 23 giu 2024

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.

Per il secondo caso mostriamo un esempio pratico:

Library "libc:6"

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

' 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

' int Chiama_stat(const char *s, struct stat *st)
' Ha lo scopo di invocare la funzione stat() dichiarata nel file header /sys/stat.h.
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
 
' Chiama la procedura per creare la libreria esterna ad hoc:
 Creaso()
 
 s = "/percorso/del/file"
 
 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 Trim(asctime(localtime(VarPtr(at))))
 Write asctime(localtime(VarPtr(mt)))
 Write asctime(localtime(VarPtr(ct)))
  
End
 
 
Private Procedure Creaso()
 
 File.Save("/tmp/libSTATUS.c", "#include <sys/stat.h>\n\n" &
                               "struct stat status;\n\n" &
                               "int Chiama_stat(const char *s, struct stat *st) {\n" &
                               "   int r;\n" &
                               "   r = stat(s, &status);\n" &
                               "   *st = status;\n" &
                               "   return r;\n}")
 
' Genera la libreria esterna ad hoc:
 Shell "gcc -o /tmp/libSTATUS.so /tmp/libSTATUS.c -shared -fPIC" Wait
  
End


Uso della funzione "g_stat()" della libreria Glib

Si potrà utilizzare anche la funzione "g_stat()" della libreria "libglib-2.0.so.0.8000.2 ", la quale passerà per riferimento una Struttura identica a quella già vista sopra e definita nel file /bits/stat.h.

Il codice Gambas sarà:

 ' 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:0.8000.2"

Library "libc:6"

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

' 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