Ottenere la directory di appartenenza di un file mediante le funzioni esterne del API di libgio

Da Gambas-it.org - Wikipedia.

La libreria Libgio fornisce un'astrazione del file system che permette alle applicazioni di accedere ai file locali e remoti.

E' possibile con alcune funzioni della libreria Libgio sapere se un file possiede una directory genitore (di appartenza) e quale è tale directory.

E' necessario avere installata nel sistema e richiamare la libreria condivisa: "libgio-2.0.so.0.7200.4 "

Mostriamo di seguito un semplice esempio pratico:

Library "libgio-2.0:0.7200.4"

' GFile * g_file_new_for_path (const char *path)
' Constructs a GFile for a given path. This operation never fails.
Private Extern g_file_new_for_path(path As String) As Pointer

' gboolean g_file_has_parent (GFile *file, GFile *parent)
' Checks if file has a parent, and optionally, if it is parent.
' If parent is NULL then this function returns TRUE if file has any parent at all.
' If parent is non-NULL then TRUE is only returned if file is a child of parent.
Private Extern g_file_has_parent(gfile As Pointer, parent As Pointer) As Boolean

' GFile * g_file_get_parent (GFile *file)
' Gets the parent directory for the file. If the file represents the root directory of the file system, then NULL will be returned.
Private Extern g_file_get_parent(gfile As Pointer) As Pointer

' char * g_file_get_path (GFile *file)
' Gets the local pathname for GFile, if one exists.
Private Extern g_file_get_path(gfile As Pointer) As String

' void g_object_unref (gpointer object)
' Decreases the reference count of object.
Private Extern g_object_unref(gobject As Pointer)


Public Sub Main()

 Dim gf, parent As Pointer
 Dim bo As Boolean
 Dim s As String

 gf = g_file_new_for_path("/percorso/del/file")
 If gf == 0 Then Error.Raise("Impossibile creare un oggetto GFile con il percorso del file impostato !")
  
 If g_file_has_parent(gf, 0) Then  
   parent = g_file_get_parent(gf)
   If parent == 0 Then Error.Raise("Impossibile creare un oggetto GFile con il percorso del primo file impostato !")
 Endif
 
 s = g_file_get_path(parent)

 Print s
  
 g_object_unref(gf)

End


Riferimenti