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

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

Webmaster