Differenze tra le versioni di "Ottenere alcune informazioni generali sui file"

Da Gambas-it.org - Wikipedia.
(Creata pagina con 'Per ottenere alcune informazioni generali sui file, è possibile operare con almeno due risorse. ===Usando la funzione ''Stat()'' propria di Gambas=== Le modalità di utiliz...')
 
Riga 7: Riga 7:
  
 
===Usando la funzione ''stat()'' di C===
 
===Usando la funzione ''stat()'' di C===
Per utilizzare la funzione ''stat()'' di C, bisognerà porla in una libreria condivisa .so esterna, appositamente realizzata da noi. La funzione contenuta da tale libreria passerà al programma principale Gambas un ''Puntatore'' alla ''Struttura'' che viene riempita dalla funzione ''stat()''.
+
Per utilizzare la funzione ''stat()'' di C, bisognerà richiamare la funzione ''__xstat()''.  in quanto, per consentire alla Struttura "''struct stat''" di variare senza cambiare numero di versione della libreria condivisa .so, il gruppo di funzioni 'stat' sono traslate nelle chiamate alle funzioni 'xstat', 'fxstat', 'lxstat', alle quali è necessario passare come primo argomento il numero-versione che designa la struttura dei dati e i bit utilizzati.
 
+
Dunque, in fase di linking la chiamata a ''stat()'' viene convertita in ''__xstat()''.
 
 
Dunque la libreria esterna .so potrà essere del seguente tenore:
 
#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;
 
 
 
}
 
  
  
Riga 34: Riga 15:
 
   st_dev As Long
 
   st_dev As Long
 
   st_ino As Long
 
   st_ino As Long
 +
  st_nlink As Long
 
   st_mode As Integer
 
   st_mode As Integer
  st_nlink As Integer
 
 
   st_uid As Integer
 
   st_uid As Integer
   st_gid As Integer
+
   st_gid As Long
 
   st_rdev As Long
 
   st_rdev As Long
  __pad1 As Long
 
 
   st_size As Long
 
   st_size As Long
   st_blksize As Integer
+
   st_blksize As Long
  __pad2 As Integer
 
 
   st_blocks As Long
 
   st_blocks As Long
 
   st_atime As Long
 
   st_atime As Long
   st_atime_nsec As Long
+
   __unused1 As Integer
 
   st_mtime As Long
 
   st_mtime As Long
 +
  __unused2 As Long
 
   st_mtime_nsec As Long  
 
   st_mtime_nsec As Long  
 
   st_ctime As Long
 
   st_ctime As Long
   st_ctime_nsec As Long  
+
   __glibc_reserved[3] As Long
  __unused4 As Integer
 
  __unused5 As Integer
 
 
  End Struct
 
  End Struct
 
   
 
   
   
+
  <FONT Color=gray>' ''int __xstat (int __ver, const char *__filename, struct stat *__stat_buf)''
  Private Extern Chiama_stat(s As String, st As STATUS_File) As Integer In "/tmp/libSTATUS"
+
' ''Get file attributes for FILE and put them in BUF.''</font>
 +
  Private Extern __xstat(ver As Integer, s As String, st As STATUS_File) As Integer In "libc:6"
 
   
 
   
 
   
 
   
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
   
 
   
  Dim p As Pointer
 
 
   Dim stS As New STATUS_File
 
   Dim stS As New STATUS_File
 
   Dim i As Integer
 
   Dim i As Integer
 
    
 
    
<FONT Color=gray>' ''Crea la libreria esterna .so da noi scritta:''</font>
+
   i = __xstat(1, "''/percorso/del/file''", stS)
  Shell "gcc -o /tmp/libSTATUS.so " & Application.Path &/ "libSTATUS.c -shared -fPIC" Wait
+
   If i <> 0 Then Error.Raise("Errore alla funzione '__xstat()' !")
 
+
    
  p = Alloc(144)
 
 
 
   i = Chiama_stat("''/percorso/del/file''", p)
 
   If i <> 0 Then Error.Raise("Errore alla funzione 'stat()' !")
 
 
 
   stS = p
 
 
 
<FONT Color=gray>' ''Vediamo come esempio qualche valore del file:''</font>
 
 
   With stS
 
   With stS
 
     Print .st_dev
 
     Print .st_dev
 +
    Print .st_ino
 +
    Print .st_nlink
 +
    Print .st_mode
 
     Print .st_uid
 
     Print .st_uid
 
     Print .st_gid
 
     Print .st_gid
 +
    Print .st_rdev
 
     Print .st_size; " byte"
 
     Print .st_size; " byte"
 +
    Print .st_blksize
 +
    Print .st_blocks
 +
    Print .st_atime
 +
    Print .st_mtime
 +
    Print .st_ctime
 
   End With
 
   End With
 
  Free(p)
 
 
   
 
   
 
  '''End'''
 
  '''End'''

Versione delle 22:05, 5 nov 2014

Per ottenere alcune informazioni generali sui file, è possibile operare con almeno due risorse.


Usando la funzione Stat() propria di Gambas

Le modalità di utilizzo della funzione di Gambas Stat() è descritta nella seguente pagina.


Usando la funzione stat() di C

Per utilizzare la funzione stat() di C, bisognerà richiamare la funzione __xstat(). in quanto, per consentire alla Struttura "struct stat" di variare senza cambiare numero di versione della libreria condivisa .so, il gruppo di funzioni 'stat' sono traslate nelle chiamate alle funzioni 'xstat', 'fxstat', 'lxstat', alle quali è necessario passare come primo argomento il numero-versione che designa la struttura dei dati e i bit utilizzati. Dunque, in fase di linking la chiamata a stat() viene convertita in __xstat().


Il codice Gambas potrà essere il seguente:

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_mtime_nsec As Long 
  st_ctime As Long
  __glibc_reserved[3] As Long
End Struct

' int __xstat (int __ver, const char *__filename, struct stat *__stat_buf)
' Get file attributes for FILE and put them in BUF.
Private Extern __xstat(ver As Integer, s As String, st As STATUS_File) As Integer In "libc:6"


Public Sub Main()

 Dim stS As New STATUS_File
 Dim i As Integer
 
  i = __xstat(1, "/percorso/del/file", stS)
  If i <> 0 Then Error.Raise("Errore alla funzione '__xstat()' !")
  
  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
    Print .st_atime
    Print .st_mtime
    Print .st_ctime
  End With

End