Conoscere caratteristiche generali dei file: differenze tra le versioni
Vai alla navigazione
Vai alla ricerca
Nessun oggetto della modifica |
Nessun oggetto della modifica |
||
| (5 versioni intermedie di uno stesso utente non sono mostrate) | |||
| Riga 1: | Riga 1: | ||
Per ottenere informazioni di tipo generale sui file, è possibile operare con risorse di Gambas o del linguaggio C. | 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 [[stat|questa pagina della Wiki]]. | |||
==Uso della funzione "stat()" di C== | |||
La funzione "stat()" di C potrà essere richiamata in modo <SPAN Style="text-decoration:underline">diretto</span> come funzione esterna mediante ''Extern'', come descritto nella [[Stat_()|seguente pagina della Wiki]], oppure in modo <SPAN Style="text-decoration:underline">indiretto</span> utilizzandola all'interno di una libreria esterna condivisa da noi creata ''ad hoc''. | |||
==Uso della funzione | |||
La funzione | |||
Per il secondo caso mostriamo un esempio pratico: | Per il secondo caso mostriamo un esempio pratico: | ||
Library "libc:6" | |||
Public Struct STATUS_File | Public Struct STATUS_File | ||
st_dev As Long | st_dev As Long | ||
| Riga 29: | Riga 28: | ||
__glibc_reserved[3] As Long | __glibc_reserved[3] As Long | ||
End Struct | End Struct | ||
<FONT Color=gray>' ''tm* localtime( const time_t *time )'' | <FONT Color=gray>' ''tm* localtime( const time_t *time )'' | ||
| Riga 42: | Riga 38: | ||
<FONT Color=gray>' ''int Chiama_stat(const char *s, struct stat *st)'' | <FONT Color=gray>' ''int Chiama_stat(const char *s, struct stat *st)'' | ||
' '' | ' ''Ha lo scopo di invocare la funzione stat() dichiarata nel file header /sys/stat.h.''</font> | ||
Private Extern Chiama_stat(nomefile As String, stFile As STATUS_File) As Integer In "/tmp/libSTATUS" | Private Extern Chiama_stat(nomefile As String, stFile As STATUS_File) As Integer In "/tmp/libSTATUS" | ||
Public Sub Main() | |||
Dim i As Integer | Dim i As Integer | ||
| Riga 56: | Riga 52: | ||
Creaso() | Creaso() | ||
s = "<FONT Color= | s = "<FONT Color=darkgreen>''/percorso/del/file''</font>" | ||
i = Chiama_stat(s, stS) | i = Chiama_stat(s, stS) | ||
| Riga 84: | Riga 80: | ||
Write asctime(localtime(VarPtr(ct))) | Write asctime(localtime(VarPtr(ct))) | ||
End | |||
Private Procedure Creaso() | |||
File.Save("/tmp/libSTATUS.c", "#include <sys/stat.h>\n\n" & | File.Save("/tmp/libSTATUS.c", "#include <sys/stat.h>\n\n" & | ||
| Riga 100: | Riga 96: | ||
Shell "gcc -o /tmp/libSTATUS.so /tmp/libSTATUS.c -shared -fPIC" Wait | Shell "gcc -o /tmp/libSTATUS.so /tmp/libSTATUS.c -shared -fPIC" Wait | ||
End | |||
==Uso della funzione | ==Uso della funzione "g_stat()" della libreria ''Glib''== | ||
Si potrà utilizzare anche la funzione | 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à: | Il codice Gambas sarà: | ||
<FONT Color=gray>' ''int g_stat (const gchar *filename, GStatBuf *buf)'' | |||
' ''Returns information about a file.''</font> | |||
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 | Public Struct STATUS_File | ||
st_dev As Long | st_dev As Long | ||
| Riga 126: | Riga 127: | ||
__glibc_reserved[3] As Long | __glibc_reserved[3] As Long | ||
End Struct | End Struct | ||
<FONT Color=gray>' ''tm* localtime( const time_t *time )'' | <FONT Color=gray>' ''tm* localtime( const time_t *time )'' | ||
| Riga 144: | Riga 137: | ||
Public Sub Main() | |||
Dim sta As New STATUS_File | |||
Dim at, mt, ct As Long | |||
<FONT Color=#B222222>g_stat("''/percorso/del/file/''", sta)</font> | <FONT Color=#B222222>g_stat("<FONT Color=darkgreen>''/percorso/del/file/''</font>", sta)</font> | ||
With sta | With sta | ||
| Riga 171: | Riga 164: | ||
Print asctime(localtime(VarPtr(ct))) | Print asctime(localtime(VarPtr(ct))) | ||
End | |||
Versione attuale delle 14:46, 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