o
odinpkg.dev
packages / binding / libmagic-odin

libmagic-odin

674cdf5binding

Odin bindings for magic.h

BSD-3-Clause · updated 12 months ago

magic

Odin bindings for libmagic, the library behind the Unix file command.

This package allows you to identify file types and MIME types using libmagic directly from Odin.

Requirements

  • libmagic installed (usually via your system package manager)

Usage

import "magic"
import "core:fmt"

main :: proc() {
    cookie := magic.open(magic.NONE)
    if cookie == nil {
        fmt.eprintln("Error allocating cookie")
        return
    }
    defer magic.close(cookie)

    if magic.load(cookie, nil) != 0 {
        fmt.eprintfln("Error loading magic database: %s", magic.error(cookie))
        return
    }

    result := magic.file(cookie, "example.png")

    if result == nil {
        fmt.eprintfln("Error: %s\n", magic.error(cookie))
        return
    }

    fmt.printfln("File type: %s", result)
}