Add test to call mime lookup algorithms
authorWilfried Goesgens <dothebart@citadel.org>
Thu, 2 Dec 2010 00:11:41 +0000 (01:11 +0100)
committerWilfried Goesgens <dothebart@citadel.org>
Thu, 2 Dec 2010 00:11:41 +0000 (01:11 +0100)
libcitadel/tests/.gitignore
libcitadel/tests/Makefile.in
libcitadel/tests/mime_xdg_lookup_type.c [new file with mode: 0644]

index 04f444e727c51992bb6907527e9ff05b05cfccfb..63237e89a478c8fb78e1d6abee3c51992a834fe4 100644 (file)
@@ -4,3 +4,4 @@ stringbuf_IO_test
 stringbuf_conversion_test
 stringbuf_test
 mimeparser_test
+mime_xdg_lookup_test
index 1c602c090a356f892f88ec42b7670b35a39d872e..78570bf652f1a97cd789d496b2d4fce072b72b6b 100644 (file)
@@ -14,7 +14,7 @@ top_builddir=`pwd`
 
 # End of configuration section
 
-TARGETS=stringbuf_test stringbuf_IO_test stringbuf_conversion_test hashlist_test mimeparser_test
+TARGETS=stringbuf_test stringbuf_IO_test stringbuf_conversion_test hashlist_test mimeparser_test mime_xdg_lookup_test
 all: $(TARGETS)
 
 
@@ -58,6 +58,15 @@ mimeparser_test:     $(LIBOBJS) mimeparser_test.o
        ../.libs/libcitadel.a \
        -o mimeparser_test 
 
+
+mime_xdg_lookup_test:  $(LIBOBJS) mime_xdg_lookup_type.o 
+       $(CC) $(LDFLAGS) $(LIBOBJS) $(LIBS) \
+       mime_xdg_lookup_type.o \
+       ../.libs/libcitadel.a \
+       -o mime_xdg_lookup_test 
+
+
+
 .c.o:
        $(CC) $(CFLAGS) $(DEFS) -c  $<
 
diff --git a/libcitadel/tests/mime_xdg_lookup_type.c b/libcitadel/tests/mime_xdg_lookup_type.c
new file mode 100644 (file)
index 0000000..589f3fd
--- /dev/null
@@ -0,0 +1,79 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <errno.h>
+
+#include <fcntl.h>
+
+#include <unistd.h>
+#include <stddef.h>
+
+
+#include "../lib/libcitadel.h"
+
+int main(int argc, char* argv[])
+{
+       char a;
+       int fd;
+       char *filename = NULL;
+       struct stat statbuf;
+       const char *Err;
+
+       StrBuf *MimeBuf;
+       long MimeLen;
+       char *MimeStr;
+       int by_extension = 0;
+
+       setvbuf(stdout, NULL, _IONBF, 0);
+
+
+       while ((a = getopt(argc, argv, "xf:")) != EOF)
+       {
+               switch (a) {
+               case 'x':
+                       by_extension = 1;
+                       break;
+               case 'f':
+                       filename = optarg;
+                       break;
+               }
+       }
+       StartLibCitadel(8);
+
+       if (filename == NULL) {
+               printf("Filename requried! -f\n");
+               return 1;
+       }
+
+       if (by_extension) {
+               printf("Mimetype: %s\n", GuessMimeByFilename(filename, strlen(filename)));
+               return 0;
+       }
+
+       fd = open(filename, 0);
+       if (fd < 0) {
+               printf("Error opening file [%s] %d [%s]\n", filename, errno, strerror(errno));
+               return 1;
+       }
+       if (fstat(fd, &statbuf) == -1) {
+               printf("Error stating file [%s] %d [%s]\n", filename, errno, strerror(errno));
+               return 1;
+       }
+       MimeBuf = NewStrBufPlain(NULL, statbuf.st_size + 1);
+       if (StrBufReadBLOB(MimeBuf, &fd, 1, statbuf.st_size, &Err) < 0) {
+               printf("Error reading file [%s] %d [%s] [%s]\n", filename, errno, strerror(errno), Err);
+               FreeStrBuf(&MimeBuf);
+               return 1;
+       }
+       MimeLen = StrLength(MimeBuf);
+       MimeStr = SmashStrBuf(&MimeBuf);
+
+       printf("Mimetype: %s\n", GuessMimeType(MimeStr, MimeLen));
+
+       free(MimeStr);
+       return 0;
+}