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

Re: Extern libmagic output like command File?


On 9/16/24 09:41, System64 Development wrote:
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

Try it with MAGIC_NONE. Also, magic_close frees the string pointer which turns the string stored in $Version to garbage.

[code]
' Gambas module file

Library "libmagic:1"

Public Extern MagicOpen(iFlags As Integer) As Pointer Exec "magic_open"
Public Extern MagicLoad(pMagic As Pointer, sFilename As String) As Exec "magic_load"
Public Extern MagicFile(pMagic As Pointer, sFilename As String) As String Exec "magic_file"
Public Extern MagicClose(pMagic As Pointer) Exec "magic_close"
Public Extern MagicError(pMagic As Pointer) As String Exec "magic_error"


Public Sub Main()

  Dim sPath As String = "filename.dwg"
  Dim pMagic As Pointer
  Dim sContentType, sContentTypeStatic As String

  pMagic = MagicOpen(0) 'MAGIC_NONE
  MagicLoad(pMagic, Null)
  sContentType = MagicFile(pMagic, sPath)
  If IsNull(sContentType) Then
    Print "error"
    Quit
  Endif

  For x As Integer = 0 To Len(sContentType) - 1
    sContentTypeStatic &= sContentType[x]
  Next
  MagicClose(pMagic)

  Print sContentTypeStatic

End
[/code]


--
Lee

--- Gambas User List Netiquette [https://gambaswiki.org/wiki/doc/netiquette] ----
--- Gambas User List Archive [https://lists.gambas-basic.org/archive/user] ----


References:
Extern libmagic output like command File?System64 Development <64xcode@xxxxxxxxx>