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

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

Webmaster