Conoscere la data di accesso e di modifica di un file con le funzioni esterne del API di libgio-2.0
Da Gambas-it.org - Wikipedia.
Versione del 16 feb 2018 alle 20:29 di Vuott (Discussione | contributi)
Utilizzando alcune funzioni esterne del API di libgio-2.0, è possibile conoscere l'ultima data di accesso e l'ultima data di modifica di un file.
E' necessario avere installata nel sistema e richiamare in Gambas la libreria dinamica condivisa: "libgio-2.0"
Mostriamo un esempio pratico:
Library "libgio-2.0" Public Struct GFileAttributeInfoList infos As Pointer n_infos As Integer End Struct Private Enum G_FILE_QUERY_INFO_NONE = 0, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS Private Const G_FILE_ATTRIBUTE_TIME_ACCESS As String = "time::access" Private Const G_FILE_ATTRIBUTE_TIME_MODIFIED As String = "time::modified" ' GFile * g_file_new_for_path (const char *path) ' Constructs a GFile for a given path. Private Extern g_file_new_for_path(path As String) As Pointer ' GFileAttributeInfoList * g_file_query_settable_attributes (GFile *file, GCancellable *cancellable, GError **error) ' Obtain the list of settable attributes for the file. Private Extern g_file_query_settable_attributes(gfile As Pointer, cancellable As Pointer, gerror As Pointer) As GFileAttributeInfoList ' GFileInfo * g_file_query_info (GFile *file, const char *attributes, GFileQueryInfoFlags flags, GCancellable *cancellable, GError **error) ' Gets the requested information about specified file. Private Extern g_file_query_info(gfile As Pointer, attributes As String, flags As Integer, cancellable As Pointer, gerror As Pointer) As Pointer ' guint32 g_file_info_get_attribute_uint32 (GFileInfo *info, const char *attribute) ' Gets an unsigned 32-bit integer contained within the attribute. Private Extern g_file_info_get_attribute_uint64(info As Pointer, attribute As String) As Long ' GDateTime * g_date_time_new_from_unix_local (gint64 t) ' Creates a GDateTime corresponding to the given Unix time t in the local time zone. Private Extern g_date_time_new_from_unix_local(t As Long) As Pointer ' void g_date_time_get_ymd (GDateTime *datetime, gint *year, gint *month, gint *day) ' Retrieves the Gregorian day, month, and year of a given GDateTime. Private Extern g_date_time_get_ymd(datetime As Pointer, gyear As Pointer, gmonth As Pointer, gday As Pointer) ' gint g_date_time_get_hour (GDateTime *datetime) ' Retrieves the hour of the day represented by datetime. Private Extern g_date_time_get_hour(datetime As Pointer) As Integer ' gint g_date_time_get_minute (GDateTime *datetime) ' Retrieves the minute of the hour represented by datetime. Private Extern g_date_time_get_minute(datetime As Pointer) As Integer ' gint g_date_time_get_second (GDateTime *datetime) ' Retrieves the second of the minute represented by datetime. Private Extern g_date_time_get_second(datetime As Pointer) As Integer ' void g_file_attribute_info_list_unref (GFileAttributeInfoList *list) ' Removes a reference from the given list. Private Extern g_file_attribute_info_list_unref(list As GFileAttributeInfoList) ' void g_object_unref (gpointer object) ' Decreases the reference count of object. Private Extern g_object_unref(gobject As Pointer) Public Sub Main() Dim p As Pointer Dim infl As GFileAttributeInfoList Dim i As Integer Dim nomefile, att As String nomefile = "/percorso/del/file" Print "Percorso file: "; nomefile p = g_file_new_for_path(nomefile) infl = g_file_query_settable_attributes(p, 0, 0) For i = 0 To infl.n_infos - 1 att = String@(Pointer@(infl.infos + (i * (SizeOf(gb.Pointer) * 2)))) Select Case att Case G_FILE_ATTRIBUTE_TIME_ACCESS Attributo(p, G_FILE_ATTRIBUTE_TIME_ACCESS, "Ultimo accesso") Case G_FILE_ATTRIBUTE_TIME_MODIFIED Attributo(p, G_FILE_ATTRIBUTE_TIME_MODIFIED, "Ultima modifica") End Select Next ' Libera la memoria precedentemente allocata da libgio-2.0: g_file_attribute_info_list_unref(infl) g_object_unref(p) End Private Procedure Attributo(fl As Pointer, at As String, tp As String) Dim gfi, gdt As Pointer Dim lo As Long Dim y, m, d As Integer gfi = g_file_query_info(fl, at, G_FILE_QUERY_INFO_NONE, 0, 0) If gfi = 0 Then Error.Raise("ERRORE !") lo = g_file_info_get_attribute_uint64(gfi, at) gdt = g_date_time_new_from_unix_local(lo) g_date_time_get_ymd(gdt, VarPtr(y), VarPtr(m), VarPtr(d)) Print tp; ": "; Date(y, m, d, g_date_time_get_hour(gdt), g_date_time_get_minute(gdt), g_date_time_get_second(gdt), 0) ' Libera la memoria precedentemente allocata da libgio-2.0: g_object_unref(gfi) End