[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Extern libmagic output like command File?


Hello

Take the libmagic example from the Italian forum [1] to convert it into a class that returns the file type.

It works, more or less because it returns the file type [2] but not like the file command does [3].

The question: Could someone tell me how to get the same output of the file command but with libmagic?

Regards.

Martin.

[1] https://www.gambas-it.org/wiki/index.php/Conoscere_il_mime-type_di_un_file_mediante_le_funzioni_esterne_del_API_di_libmagic

[2] image/vnd.dwg; charset=binary

[3] "DWG AutoDesk AutoCAD 2018/2019/2020"

' Gambas class file

Export

Library "libmagic:1"

Private MAGIC_MIME_TYPE As Integer = &10
Private MAGIC_MIME_ENCODING As Integer = &400
Private MAGIC_MIME As Integer = MAGIC_MIME_TYPE Or MAGIC_MIME_ENCODING

' magic_t magic_open(int)
' Creates a magic cookie pointer and returns it.
Private Extern magic_open(mg As Integer) As Pointer

' int magic_load(magic_t, const char *)
' Loads the the colon separated list of database files passed in as file name.
Private Extern magic_load(magic_t As Pointer, filename As String) As Integer

' const char *magic_file(magic_t, const char *)
' Returns a textual description of the contents of the file.
Private Extern magic_file(magic_t As Pointer, filename As String) As String

' void magic_close(magic_t)
' Closes the magic database and deallocates any resources used.
Private Extern magic_close(magic_t As Pointer)

Property Version As String

Private $Version As String

Private Function Version_Read() As String

  Return $Version

End

Private Sub Version_Write(Value As String)

  Dim magic As Pointer
  Dim err As Integer

  If Exist(Value) Then
    If Stat(Value).Type = gb.File Then
      magic = magic_open(MAGIC_MIME)
      If magic == 0 Then Error.Raise("Impossibile inizializzare la libreria 'magic' !")
      err = magic_load(magic, Null)
      If err <> 0 Then
        ChiudeMagic(magic)
        Error.Raise("Impossibile caricare il database di 'magic' !")
      Endif
      $Version = magic_file(magic, Value)
      ChiudeMagic(magic)
    Endif
  Endif

End

Private Procedure ChiudeMagic(mgc As Pointer)

  magic_close(mgc)

End


Follow-Ups:
Re: Extern libmagic output like command File?Bruce Steers <bsteers4@xxxxxxxxx>
Re: Extern libmagic output like command File?vuott@xxxxxxxxxxxx
Re: Extern libmagic output like command File?T Lee Davidson <t.lee.davidson@xxxxxxxxx>