Annotation of libwww/Library/src/HTIcons.c, revision 2.6
2.6 ! frystyk 1: /* HTIcon.c
! 2: ** ICON MANAGEMENT
! 3: **
! 4: ** (c) COPYRIGHT CERN 1994.
! 5: ** Please first read the full copyright statement in the file COPYRIGH.
2.1 frystyk 6: **
7: ** This module contains the functions for initializing, adding
8: ** and selecting the icon for local directory listings, FTP and Gopher.
9: **
10: ** History:
11: ** Mar 94 Written by Ari Luotonen, luotonen@ptsun00.cern.ch
12: **
13: */
14:
15: /* Library include files */
16: #include "HTMLPDTD.h"
17: #include "HTUtils.h"
18: #include "HTAnchor.h"
19: #include "HTParse.h"
20: #include "HTFile.h"
21: #include "HTFormat.h"
22: #include "HTChunk.h"
23: #include "HTIcons.h" /* Implemented here */
24:
2.5 duns 25: #ifdef VMS
26: typedef unsigned long mode_t;
27: #include "HTVMSUtils.h"
28: #endif /* VMS */
29:
2.1 frystyk 30: /* Globals */
31: PUBLIC BOOL HTDirShowBrackets = YES;
32: PUBLIC HTIconNode * icon_unknown = NULL; /* Unknown file type */
33: PUBLIC HTIconNode * icon_blank = NULL; /* Blank icon in heading */
34: PUBLIC HTIconNode * icon_parent = NULL; /* Parent directory icon */
35: PUBLIC HTIconNode * icon_dir = NULL; /* Directory icon */
36:
37: /* Type definitions and global variables etc. local to this module */
38: PRIVATE HTList * icons = NULL;
39: PRIVATE int alt_len = 0; /* Longest ALT text */
40:
2.2 luotonen 41: /*
42: * Global variable for the AddHref nodes
43: * AddHref URL suff1 suff2 ...
44: */
45: PRIVATE HTList * hrefs = NULL;
46:
47:
2.1 frystyk 48: /* ------------------------------------------------------------------------- */
49:
50: PRIVATE void alt_resize ARGS1(char *, alt)
51: {
52: if (alt) {
53: int len = strlen(alt);
54: if (len > alt_len) alt_len = len;
55: }
56: }
57:
58:
59: PUBLIC char * HTIcon_alt_string ARGS2(char *, alt,
60: BOOL, brackets)
61: {
62: static char * ret = NULL;
63: char * p = NULL;
64: int len = alt ? strlen(alt) : 0;
65:
66: if (ret) free(ret); /* From previous call */
67: p = ret = (char*)malloc(alt_len + 3);
68: if (!ret) outofmem(__FILE__, "HTIcon_alt_string");
69:
70: if (HTDirShowBrackets)
71: *p++ = brackets ? '[' : ' ';
72: if (alt) strcpy(p,alt);
73: p += len;
74: while (len++ < alt_len) *p++=' ';
75: if (HTDirShowBrackets)
76: *p++ = brackets ? ']' : ' ';
77: *p = 0;
78:
79: return ret;
80: }
81:
82:
83: /*
84: ** HTAddIcon(url, alt, type_templ) adds icon:
85: **
86: ** <IMG SRC="url" ALT="[alt]">
87: **
88: ** for files for which content-type or content-encoding matches
89: ** type_templ. If type_templ contains a slash, it is taken to be
90: ** a content-type template. Otherwise, it is a content-encoding
91: ** template.
92: */
93: PUBLIC void HTAddIcon ARGS3(char *, url,
94: char *, alt,
95: char *, type_templ)
96: {
97: HTIconNode * node;
98:
99: if (!url || !type_templ) return;
100:
101: node = (HTIconNode*)calloc(1,sizeof(HTIconNode));
102: if (!node) outofmem(__FILE__, "HTAddIcon");
103:
104: if (url) StrAllocCopy(node->icon_url, url);
105: if (alt) StrAllocCopy(node->icon_alt, alt);
106: if (type_templ) StrAllocCopy(node->type_templ, type_templ);
107:
108: if (!icons) icons = HTList_new();
109: HTList_addObject(icons, (void*)node);
110: alt_resize(alt);
111: CTRACE(stderr,
112: "AddIcon..... %s => SRC=\"%s\" ALT=\"%s\"\n",type_templ,url,
113: alt ? alt : "");
114: }
115:
116:
117: /*
2.2 luotonen 118: * Put the AddHrefs in a list. It can be used for indexing to
119: * present special filetypes through a CGI.
120: */
121: PUBLIC void HTAddHref ARGS2(char *, url,
122: char *, type_templ)
123: {
124: HTHrefNode * node;
125:
126: if (!url || !type_templ) return;
127:
128: node = (HTHrefNode*)calloc(1,sizeof(HTHrefNode));
129: if (!node) outofmem(__FILE__, "HTAddHref");
130:
131: if (url) StrAllocCopy(node->href_url, url);
132: if (type_templ) StrAllocCopy(node->type_templ, type_templ);
133:
134: if (!hrefs) hrefs = HTList_new();
135: HTList_addObject(hrefs, (void*)node);
136: CTRACE(stderr,
137: "AddHref..... %s => URL=\"%s\" \n",type_templ,url);
138: }
139:
140:
141:
142: /*
2.1 frystyk 143: ** HTAddUnknownIcon(url,alt) adds the icon used for files for which
144: ** no other icon seems appropriate (unknown type).
145: */
146: PUBLIC void HTAddUnknownIcon ARGS2(char *, url,
147: char *, alt)
148: {
149: icon_unknown = (HTIconNode*)calloc(1,sizeof(HTIconNode));
150: if (!icon_unknown) outofmem(__FILE__, "HTAddUnknownIcon");
151:
152: if (url) StrAllocCopy(icon_unknown->icon_url, url);
153: if (alt) StrAllocCopy(icon_unknown->icon_alt, alt);
154: alt_resize(alt);
155: CTRACE(stderr,"AddIcon..... UNKNOWN => SRC=\"%s\" ALT=\"%s\"\n",url,
156: alt ? alt : "");
157:
158: }
159:
160:
161: /*
162: ** HTAddBlankIcon(url,alt) adds the blank icon used in the
163: ** heading of the listing.
164: */
165: PUBLIC void HTAddBlankIcon ARGS2(char *, url,
166: char *, alt)
167: {
168: icon_blank = (HTIconNode*)calloc(1,sizeof(HTIconNode));
169: if (!icon_blank) outofmem(__FILE__, "HTAddBlankIcon");
170:
171: if (url) StrAllocCopy(icon_blank->icon_url, url);
172: if (alt) StrAllocCopy(icon_blank->icon_alt, alt);
173: alt_resize(alt);
174: CTRACE(stderr,"AddIcon..... BLANK => SRC=\"%s\" ALT=\"%s\"\n",url,
175: alt ? alt : "");
176: }
177:
178:
179: /*
180: ** HTAddParentIcon(url,alt) adds the parent directory icon.
181: */
182: PUBLIC void HTAddParentIcon ARGS2(char *, url,
183: char *, alt)
184: {
185: icon_parent = (HTIconNode*)calloc(1,sizeof(HTIconNode));
186: if (!icon_parent) outofmem(__FILE__, "HTAddBlankIcon");
187:
188: if (url) StrAllocCopy(icon_parent->icon_url, url);
189: if (alt) StrAllocCopy(icon_parent->icon_alt, alt);
190: alt_resize(alt);
191: CTRACE(stderr,"AddIcon..... PARENT => SRC=\"%s\" ALT=\"%s\"\n",url,
192: alt ? alt : "");
193: }
194:
195:
196: /*
197: ** HTAddDirIcon(url,alt) adds the directory icon.
198: */
199: PUBLIC void HTAddDirIcon ARGS2(char *, url,
200: char *, alt)
201: {
202: icon_dir = (HTIconNode*)calloc(1,sizeof(HTIconNode));
203: if (!icon_dir) outofmem(__FILE__, "HTAddBlankIcon");
204:
205: if (url) StrAllocCopy(icon_dir->icon_url, url);
206: if (alt) StrAllocCopy(icon_dir->icon_alt, alt);
207: alt_resize(alt);
208: CTRACE(stderr,
209: "AddIcon..... DIRECTORY => SRC=\"%s\" ALT=\"%s\"\n",url,
210: alt ? alt : "");
211: }
212:
213:
214: PRIVATE BOOL match ARGS2(char *, templ,
215: char *, actual)
216: {
217: static char * c1 = NULL;
218: static char * c2 = NULL;
219: char * slash1;
220: char * slash2;
221:
222: StrAllocCopy(c1,templ);
223: StrAllocCopy(c2,actual);
224:
225: slash1 = strchr(c1,'/');
226: slash2 = strchr(c2,'/');
227:
228: if (slash1 && slash2) {
229: *slash1++ = 0;
230: *slash2++ = 0;
231: return HTAA_templateMatch(c1,c2) && HTAA_templateMatch(slash1,slash2);
232: }
233: else if (!slash1 && !slash2)
234: return HTAA_templateMatch(c1,c2);
235: else
236: return NO;
237: }
238:
239:
2.4 frystyk 240: PRIVATE char * prefixed ARGS2(CONST char *, prefix,
241: char *, name)
2.1 frystyk 242: {
243: static char * ret = NULL;
244: FREE(ret); /* From previous call */
245:
246: ret = (char *)malloc(strlen(prefix) + strlen(name) + 2);
247: if (!ret) outofmem(__FILE__, "prefixed");
248:
249: strcpy(ret,prefix);
250: if (*prefix && prefix[strlen(prefix)-1] != '/')
251: strcat(ret,"/");
252: strcat(ret,name);
253: return ret;
254: }
255:
256:
2.3 timbl 257: PUBLIC void HTStdIconInit ARGS1(CONST char *, url_prefix)
2.1 frystyk 258: {
2.4 frystyk 259: CONST char * p = url_prefix ? url_prefix : "/internal-icon/";
2.1 frystyk 260:
261: HTAddBlankIcon (prefixed(p,"blank.xbm"), NULL );
262: HTAddDirIcon (prefixed(p,"directory.xbm"),"DIR" );
263: HTAddParentIcon (prefixed(p,"back.xbm"), "UP" );
264: HTAddUnknownIcon(prefixed(p,"unknown.xbm"), NULL );
265: HTAddIcon(prefixed(p,"unknown.xbm"), NULL, "*/*");
266: HTAddIcon(prefixed(p,"binary.xbm"), "BIN", "binary");
267: HTAddIcon(prefixed(p,"unknown.xbm"), NULL, "www/unknown");
268: HTAddIcon(prefixed(p,"text.xbm"), "TXT", "text/*");
269: HTAddIcon(prefixed(p,"image.xbm"), "IMG", "image/*");
270: HTAddIcon(prefixed(p,"movie.xbm"), "MOV", "video/*");
271: HTAddIcon(prefixed(p,"sound.xbm"), "AU", "audio/*");
272: HTAddIcon(prefixed(p,"tar.xbm"), "TAR", "multipart/x-tar");
273: HTAddIcon(prefixed(p,"tar.xbm"), "TAR", "multipart/x-gtar");
274: HTAddIcon(prefixed(p,"compressed.xbm"), "CMP", "x-compress");
275: HTAddIcon(prefixed(p,"compressed.xbm"), "GZP", "x-gzip");
276: HTAddIcon(prefixed(p,"index.xbm"), "IDX", "application/x-gopher-index");
277: HTAddIcon(prefixed(p,"index2.xbm"), "CSO", "application/x-gopher-cso");
278: HTAddIcon(prefixed(p,"telnet.xbm"), "TEL", "application/x-gopher-telnet");
279: HTAddIcon(prefixed(p,"unknown.xbm"), "DUP", "application/x-gopher-duplicate");
280: HTAddIcon(prefixed(p,"unknown.xbm"), "TN", "application/x-gopher-tn3270");
281: }
282:
283:
284: /* HTGetIcon()
285: ** returns the icon corresponding to content_type or content_encoding.
286: */
287: PUBLIC HTIconNode * HTGetIcon ARGS3(mode_t, mode,
288: HTFormat, content_type,
289: HTFormat, content_encoding)
290: {
291: if (!icon_unknown) icon_unknown = icon_blank;
292:
293: if ((mode & S_IFMT) == S_IFREG) {
294: char * ct = content_type ? HTAtom_name(content_type) : NULL;
295: char * ce = content_encoding ? HTAtom_name(content_encoding) : NULL;
296: HTList * cur = icons;
297: HTIconNode * node;
298:
299: while ((node = (HTIconNode*)HTList_nextObject(cur))) {
300: char * slash = strchr(node->type_templ,'/');
301: if ((ct && slash && match(node->type_templ,ct)) ||
302: (ce && !slash && HTAA_templateMatch(node->type_templ,ce))) {
303: return node;
304: }
305: }
306: } else if ((mode & S_IFMT) == S_IFDIR) {
307: return icon_dir ? icon_dir : icon_unknown;
308: } else if ((mode & S_IFMT) == S_IFLNK) {
309: return icon_dir ? icon_dir : icon_unknown; /* @@ */
310: }
311:
312: return icon_unknown;
2.2 luotonen 313: }
314:
315:
316: /*
317: * Find the URL for a given type. Called from HTDirBrw.c
318: */
319: PUBLIC HTHrefNode * HTGetHref ARGS1( char *, filename)
320: {
321: HTHrefNode * node;
322: char *c;
323:
324: HTList * cur = hrefs;
325:
326: c = strrchr(filename, '.');
327: if (c) {
328: while ((node = (HTHrefNode*)HTList_nextObject(cur))) {
329: if ((!strcmp(node->type_templ,c)) ) {
330: return node;
331: }
332: }
333: }
334: return NULL;
2.1 frystyk 335: }
336:
337: /* END OF MODULE */
338:
339:
Webmaster