mime.d (2956B)
1 module xdg.mime; 2 import std.string; 3 4 extern(C){ 5 enum MAGIC:int{ 6 NONE =0x0000000, 7 DEBUG =0x0000001, 8 SYMLINK =0x0000002, 9 COMPRESS =0x0000004, 10 DEVICES =0x0000008, 11 MIME_TYPE =0x0000010, 12 CONTINUE =0x0000020, 13 CHECK =0x0000040, 14 PRESERVE_ATIME =0x0000080, 15 RAW =0x0000100, 16 ERROR =0x0000200, 17 MIME_ENCODING =0x0000400, 18 APPLE =0x0000800, 19 EXTENSION =0x1000000, 20 COMPRESS_TRANSP =0x2000000, 21 NO_CHECK_COMPRESS =0x0001000, 22 NO_CHECK_TAR =0x0002000, 23 NO_CHECK_SOFT =0x0004000, 24 NO_CHECK_APPTYPE =0x0008000, 25 NO_CHECK_ELF =0x0010000, 26 NO_CHECK_TEXT =0x0020000, 27 NO_CHECK_CDF =0x0040000, 28 NO_CHECK_CSV =0x0080000, 29 NO_CHECK_TOKENS =0x0100000, 30 NO_CHECK_ENCODING =0x0200000, 31 NO_CHECK_JSON =0x0400000, 32 NODESC =(MAGIC.EXTENSION|MAGIC.MIME|MAGIC.APPLE), 33 MIME =(MAGIC.MIME_TYPE|MAGIC.MIME_ENCODING), 34 } 35 36 37 alias magic_t = void*; 38 39 magic_t magic_open(MAGIC); 40 void magic_close(magic_t); 41 42 const(char)* magic_getpath(const char *, int); 43 const(char)* magic_file(magic_t, const char *); 44 const(char)* magic_descriptor(magic_t, int); 45 const(char)* magic_buffer(magic_t, const void *, size_t); 46 47 const(char)* magic_error(magic_t); 48 int magic_getflags(magic_t); 49 int magic_setflags(magic_t, int); 50 51 int magic_version(); 52 int magic_load(magic_t, const char *); 53 int magic_load_buffers(magic_t, void **, size_t *, size_t); 54 55 int magic_compile(magic_t, const char *); 56 int magic_check(magic_t, const char *); 57 int magic_list(magic_t, const char *); 58 int magic_errno(magic_t); 59 60 } 61 62 class MagicException : Exception { 63 this(Magic m, string file=__FILE__, size_t line=__LINE__, Throwable next=null){ 64 auto errmsg = magic_error(m.cookie); 65 super(errmsg.fromStringz.idup,file,line,next); 66 } 67 } 68 69 struct Magic{ 70 private magic_t cookie = null; 71 bool loaded=false; 72 73 bool ready(){ 74 return cookie != null && loaded; 75 } 76 77 void open(MAGIC m){ 78 cookie = magic_open(m); 79 if(cookie == null){ 80 throw new MagicException(this); 81 } 82 } 83 84 void open(){ 85 open(MAGIC.NONE); 86 } 87 88 private void check(int val){ 89 if(val<0){ 90 throw new MagicException(this); 91 } 92 } 93 94 void load(){ 95 check(magic_load(cookie, null)); 96 loaded = true; 97 } 98 99 void load(in char[] path){ 100 check(magic_load(cookie, path.toStringz)); 101 loaded = true; 102 } 103 104 auto file(in char[] path) 105 in{ 106 assert(ready()); 107 } 108 do{ 109 auto res = magic_file(cookie, path.toStringz); 110 if(res == null){ 111 throw new MagicException(this); 112 } 113 return res.fromStringz; 114 } 115 116 void close(){ 117 if(cookie) 118 magic_close(cookie); 119 cookie = null; 120 loaded = false; 121 } 122 123 static int Version(){ 124 return magic_version(); 125 } 126 } 127 128 struct Mime{ 129 Magic m; 130 void init(){ 131 m.open(MAGIC.MIME_TYPE | MAGIC.SYMLINK); 132 m.load(); 133 } 134 auto file(in char[] path){ 135 return m.file(path); 136 } 137 void close(){ 138 m.close(); 139 } 140 }