Annotation of libwww/Library/src/HTIcons.c, revision 2.8.2.1

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

Webmaster