]> gitweb.factorcode.org Git - factor.git/commitdiff
magic: bindings for libmagic.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 15 Oct 2014 03:50:38 +0000 (20:50 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 15 Oct 2014 03:50:38 +0000 (20:50 -0700)
extra/magic/authors.txt [new file with mode: 0644]
extra/magic/ffi/ffi.factor [new file with mode: 0644]
extra/magic/magic-tests.factor [new file with mode: 0644]
extra/magic/magic.factor [new file with mode: 0644]
extra/magic/platforms.txt [new file with mode: 0644]
extra/magic/summary.txt [new file with mode: 0644]

diff --git a/extra/magic/authors.txt b/extra/magic/authors.txt
new file mode 100644 (file)
index 0000000..e091bb8
--- /dev/null
@@ -0,0 +1 @@
+John Benediktsson
diff --git a/extra/magic/ffi/ffi.factor b/extra/magic/ffi/ffi.factor
new file mode 100644 (file)
index 0000000..8832e09
--- /dev/null
@@ -0,0 +1,52 @@
+! Copyright (C) 2014 John Benediktsson
+! See http://factorcode.org/license.txt for BSD license
+
+USING: alien alien.c-types alien.destructors alien.libraries
+alien.libraries.finder alien.syntax kernel literals ;
+
+IN: magic.ffi
+
+"libmagic" dup find-library cdecl add-library
+
+LIBRARY: libmagic
+
+CONSTANT: MAGIC_NONE 0x000000
+CONSTANT: MAGIC_DEBUG 0x000001
+CONSTANT: MAGIC_SYMLINK 0x000002
+CONSTANT: MAGIC_COMPRESS 0x000004
+CONSTANT: MAGIC_DEVICES 0x000008
+CONSTANT: MAGIC_MIME_TYPE 0x000010
+CONSTANT: MAGIC_CONTINUE 0x000020
+CONSTANT: MAGIC_CHECK 0x000040
+CONSTANT: MAGIC_PRESERVE_ATIME 0x000080
+CONSTANT: MAGIC_RAW 0x000100
+CONSTANT: MAGIC_ERROR 0x000200
+CONSTANT: MAGIC_MIME_ENCODING 0x000400
+CONSTANT: MAGIC_MIME flags{ MAGIC_MIME_TYPE MAGIC_MIME_ENCODING }
+CONSTANT: MAGIC_NO_CHECK_COMPRESS 0x001000
+CONSTANT: MAGIC_NO_CHECK_TAR 0x002000
+CONSTANT: MAGIC_NO_CHECK_SOFT 0x004000
+CONSTANT: MAGIC_NO_CHECK_APPTYPE 0x008000
+CONSTANT: MAGIC_NO_CHECK_ELF 0x010000
+CONSTANT: MAGIC_NO_CHECK_ASCII 0x020000
+CONSTANT: MAGIC_NO_CHECK_TROFF 0x040000
+CONSTANT: MAGIC_NO_CHECK_FORTRAN 0x080000
+CONSTANT: MAGIC_NO_CHECK_TOKENS 0x100000
+
+TYPEDEF: void* magic_t
+FUNCTION: magic_t magic_open ( int flags ) ;
+FUNCTION: void magic_close ( magic_t magic ) ;
+
+FUNCTION: c-string magic_file ( magic_t magic, c-string path ) ;
+FUNCTION: c-string magic_descriptor ( magic_t magic, int fd ) ;
+FUNCTION: c-string magic_buffer ( magic_t magic, void* buffer, size_t size ) ;
+
+FUNCTION: c-string magic_error ( magic_t magic ) ;
+FUNCTION: int magic_setflags ( magic_t magic, int flags ) ;
+
+FUNCTION: int magic_load ( magic_t magic, c-string path ) ;
+FUNCTION: int magic_compile ( magic_t magic, c-string path ) ;
+FUNCTION: int magic_check ( magic_t magic, c-string path ) ;
+FUNCTION: int magic_errno ( magic_t magic ) ;
+
+DESTRUCTOR: magic_close
diff --git a/extra/magic/magic-tests.factor b/extra/magic/magic-tests.factor
new file mode 100644 (file)
index 0000000..cb057e9
--- /dev/null
@@ -0,0 +1,6 @@
+USING: system tools.test ;
+IN: magic
+
+{ "data" } [ image guess-file ] unit-test
+{ "application/octet-stream" } [ image guess-mime-type ] unit-test
+{ "binary" } [ image guess-mime-encoding ] unit-test
diff --git a/extra/magic/magic.factor b/extra/magic/magic.factor
new file mode 100644 (file)
index 0000000..2b64067
--- /dev/null
@@ -0,0 +1,29 @@
+! Copyright (C) 2014 John Benediktsson
+! See http://factorcode.org/license.txt for BSD license
+
+USING: destructors io.backend kernel magic.ffi ;
+
+IN: magic
+
+<PRIVATE
+
+: check-magic-error ( magic num -- )
+    -1 = [ magic_error throw ] [ drop ] if ;
+
+: magic-file ( path flags -- result )
+    [
+        magic_open &magic_close
+        [ [ f magic_load ] keep check-magic-error ]
+        [ swap normalize-path magic_file ] bi
+    ] with-destructors ;
+
+PRIVATE>
+
+: guess-file ( path -- file-type )
+    MAGIC_NONE magic-file ;
+
+: guess-mime-type ( path -- mime-type )
+    MAGIC_MIME_TYPE magic-file ;
+
+: guess-mime-encoding ( path -- encoding )
+    MAGIC_MIME_ENCODING magic-file ;
diff --git a/extra/magic/platforms.txt b/extra/magic/platforms.txt
new file mode 100644 (file)
index 0000000..509143d
--- /dev/null
@@ -0,0 +1 @@
+unix
diff --git a/extra/magic/summary.txt b/extra/magic/summary.txt
new file mode 100644 (file)
index 0000000..b84de69
--- /dev/null
@@ -0,0 +1 @@
+libmagic binding