removed StartLibCitadel()
[citadel.git] / libcitadel / tests / mime_xdg_lookup_type.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <dirent.h>
8 #include <errno.h>
9
10 #include <fcntl.h>
11
12 #include <unistd.h>
13 #include <stddef.h>
14
15
16 #include "../lib/libcitadel.h"
17
18 int main(int argc, char* argv[])
19 {
20         char a;
21         int fd;
22         char *filename = NULL;
23         char *IconDir = NULL;
24         struct stat statbuf;
25         const char *Err;
26
27         StrBuf *MimeBuf;
28         long MimeLen;
29         char *MimeStr;
30         int by_extension = 0;
31         const char *GuessedMimeType;
32         const char *GuessedMimeIcon;
33         char MimeTypeStr[SIZ];
34
35         setvbuf(stdout, NULL, _IONBF, 0);
36
37
38         while ((a = getopt(argc, argv, "xf:i:")) != EOF)
39         {
40                 switch (a) {
41                 case 'x':
42                         by_extension = 1;
43                         break;
44                 case 'f':
45                         filename = optarg;
46                         break;
47                 case 'i':
48                         IconDir = optarg;
49                         break;
50                 }
51         }
52
53         if (IconDir != NULL)
54                 LoadIconDir(IconDir);
55
56         if (filename == NULL) {
57                 ShutDownLibCitadel();
58                 printf("Filename requried! -f\n");
59                 return 1;
60         }
61
62         if (by_extension) {
63                 GuessedMimeType = GuessMimeByFilename(filename, strlen(filename));
64                 printf("Mimetype: %s\n", GuessedMimeType);
65                 if (IconDir != NULL) {
66                         strcpy(MimeTypeStr, GuessedMimeType);
67                         GuessedMimeIcon = GetIconFilename(MimeTypeStr, strlen(MimeTypeStr));
68                         if (GuessedMimeIcon != NULL)
69                                 printf("Associated Icon [%s]\n", GuessedMimeIcon);
70                         else 
71                                 printf("no icon associated.\n");
72                 }
73                 ShutDownLibCitadel();
74                 return 0;
75         }
76
77         fd = open(filename, 0);
78         if (fd < 0) {
79                 printf("Error opening file [%s] %d [%s]\n", filename, errno, strerror(errno));
80                 ShutDownLibCitadel();
81                 return 1;
82         }
83         if (fstat(fd, &statbuf) == -1) {
84                 printf("Error stating file [%s] %d [%s]\n", filename, errno, strerror(errno));
85                 ShutDownLibCitadel();
86                 return 1;
87         }
88         MimeBuf = NewStrBufPlain(NULL, statbuf.st_size + 1);
89         if (StrBufReadBLOB(MimeBuf, &fd, 1, statbuf.st_size, &Err) < 0) {
90                 printf("Error reading file [%s] %d [%s] [%s]\n", filename, errno, strerror(errno), Err);
91                 FreeStrBuf(&MimeBuf);
92                 ShutDownLibCitadel();
93                 return 1;
94         }
95         MimeLen = StrLength(MimeBuf);
96         MimeStr = SmashStrBuf(&MimeBuf);
97         GuessedMimeType = GuessMimeType(MimeStr, MimeLen);
98
99         printf("Mimetype: %s\n", GuessedMimeType);
100         if (IconDir != NULL) {
101                 strcpy(MimeTypeStr, GuessedMimeType);
102                 GuessedMimeIcon = GetIconFilename(MimeTypeStr, strlen(MimeTypeStr));
103                 if (GuessedMimeIcon != NULL)
104                         printf("Associated Icon [%s]\n", GuessedMimeIcon);
105                 else 
106                         printf("no icon associated.\n");
107         }
108
109
110         free(MimeStr);
111         ShutDownLibCitadel();
112         return 0;
113 }