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

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:
2.13      frystyk    11: **        Mar 94       Written by Ari Luotonen, luotonen@dxcern.cern.ch
2.1       frystyk    12: **
                     13: */
                     14: 
                     15: /* Library include files */
2.8       frystyk    16: #include "tcp.h"
                     17: #include "HTUtils.h"
                     18: #include "HTString.h"
2.12      frystyk    19: #include "HTAAUtil.h"
2.1       frystyk    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: #include "HTVMSUtils.h"
                     28: #endif /* VMS */
                     29: 
2.1       frystyk    30: /* Globals */
                     31: PUBLIC BOOL HTDirShowBrackets = YES;
2.15    ! frystyk    32: PRIVATE HTIconNode * icon_unknown = NULL;      /* Unknown file type */
        !            33: PRIVATE HTIconNode * icon_blank = NULL;                /* Blank icon in heading */
        !            34: PRIVATE HTIconNode * icon_parent = NULL;       /* Parent directory icon */
        !            35: PRIVATE HTIconNode * icon_dir = NULL;          /* Directory icon */
2.1       frystyk    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);
2.8       frystyk   111:     if (PROT_TRACE)
                    112:        fprintf(TDEST, "AddIcon..... %s => SRC=\"%s\" ALT=\"%s\"\n",
                    113:                type_templ,url, alt ? alt : "");
2.1       frystyk   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);
2.8       frystyk   136:     if (PROT_TRACE)
                    137:        fprintf(TDEST, "AddHref..... %s => URL=\"%s\" \n",type_templ,url);
2.2       luotonen  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);
2.8       frystyk   155:     if (PROT_TRACE)
                    156:        fprintf(TDEST,"AddIcon..... UNKNOWN => SRC=\"%s\" ALT=\"%s\"\n",url,
                    157:                alt ? alt : "");
2.1       frystyk   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);
2.8       frystyk   174:     if (PROT_TRACE)
                    175:        fprintf(TDEST,"AddIcon..... BLANK => SRC=\"%s\" ALT=\"%s\"\n",url,
                    176:                alt ? alt : "");
2.1       frystyk   177: }
                    178: 
                    179: 
                    180: /*
                    181: **     HTAddParentIcon(url,alt) adds the parent directory icon.
                    182: */
                    183: PUBLIC void HTAddParentIcon ARGS2(char *, url,
                    184:                                  char *, alt)
                    185: {
                    186:     icon_parent = (HTIconNode*)calloc(1,sizeof(HTIconNode));
                    187:     if (!icon_parent) outofmem(__FILE__, "HTAddBlankIcon");
                    188: 
                    189:     if (url) StrAllocCopy(icon_parent->icon_url, url);
                    190:     if (alt) StrAllocCopy(icon_parent->icon_alt, alt);
                    191:     alt_resize(alt);
2.8       frystyk   192:     if (PROT_TRACE)
                    193:        fprintf(TDEST,"AddIcon..... PARENT => SRC=\"%s\" ALT=\"%s\"\n",url,
                    194:                alt ? alt : "");
2.1       frystyk   195: }
                    196: 
                    197: 
                    198: /*
                    199: **     HTAddDirIcon(url,alt) adds the directory icon.
                    200: */
                    201: PUBLIC void HTAddDirIcon ARGS2(char *, url,
                    202:                               char *, alt)
                    203: {
                    204:     icon_dir = (HTIconNode*)calloc(1,sizeof(HTIconNode));
                    205:     if (!icon_dir) outofmem(__FILE__, "HTAddBlankIcon");
                    206: 
                    207:     if (url) StrAllocCopy(icon_dir->icon_url, url);
                    208:     if (alt) StrAllocCopy(icon_dir->icon_alt, alt);
                    209:     alt_resize(alt);
2.8       frystyk   210:     if (PROT_TRACE)
                    211:        fprintf(TDEST,"AddIcon..... DIRECTORY => SRC=\"%s\" ALT=\"%s\"\n",url,
                    212:                alt ? alt : "");
2.1       frystyk   213: }
                    214: 
                    215: 
                    216: PRIVATE BOOL match ARGS2(char *, templ,
                    217:                         char *, actual)
                    218: {
                    219:     static char * c1 = NULL;
                    220:     static char * c2 = NULL;
                    221:     char * slash1;
                    222:     char * slash2;
                    223: 
                    224:     StrAllocCopy(c1,templ);
                    225:     StrAllocCopy(c2,actual);
                    226: 
                    227:     slash1 = strchr(c1,'/');
                    228:     slash2 = strchr(c2,'/');
                    229: 
                    230:     if (slash1 && slash2) {
                    231:        *slash1++ = 0;
                    232:        *slash2++ = 0;
                    233:        return HTAA_templateMatch(c1,c2) && HTAA_templateMatch(slash1,slash2);
                    234:     }
                    235:     else if (!slash1 && !slash2)
                    236:        return HTAA_templateMatch(c1,c2);
                    237:     else
                    238:        return NO;
                    239: }
                    240: 
                    241: 
2.4       frystyk   242: PRIVATE char * prefixed ARGS2(CONST char *,    prefix,
                    243:                              char *,           name)
2.1       frystyk   244: {
                    245:     static char * ret = NULL;
                    246:     FREE(ret); /* From previous call */
                    247: 
                    248:     ret = (char *)malloc(strlen(prefix) + strlen(name) + 2);
                    249:     if (!ret) outofmem(__FILE__, "prefixed");
                    250: 
                    251:     strcpy(ret,prefix);
                    252:     if (*prefix && prefix[strlen(prefix)-1] != '/')
                    253:        strcat(ret,"/");
                    254:     strcat(ret,name);
                    255:     return ret;
                    256: }
                    257: 
                    258: 
2.3       timbl     259: PUBLIC void HTStdIconInit ARGS1(CONST char *, url_prefix)
2.1       frystyk   260: {
2.4       frystyk   261:     CONST char * p = url_prefix ? url_prefix : "/internal-icon/";
2.1       frystyk   262: 
                    263:     HTAddBlankIcon  (prefixed(p,"blank.xbm"),  NULL    );
                    264:     HTAddDirIcon    (prefixed(p,"directory.xbm"),"DIR" );
                    265:     HTAddParentIcon (prefixed(p,"back.xbm"),   "UP"    );
                    266:     HTAddUnknownIcon(prefixed(p,"unknown.xbm"),        NULL    );
                    267:     HTAddIcon(prefixed(p,"unknown.xbm"),       NULL,   "*/*");
                    268:     HTAddIcon(prefixed(p,"binary.xbm"),                "BIN",  "binary");
                    269:     HTAddIcon(prefixed(p,"unknown.xbm"),       NULL,   "www/unknown");
                    270:     HTAddIcon(prefixed(p,"text.xbm"),          "TXT",  "text/*");
                    271:     HTAddIcon(prefixed(p,"image.xbm"),         "IMG",  "image/*");
                    272:     HTAddIcon(prefixed(p,"movie.xbm"),         "MOV",  "video/*");
                    273:     HTAddIcon(prefixed(p,"sound.xbm"),         "AU",   "audio/*");
                    274:     HTAddIcon(prefixed(p,"tar.xbm"),           "TAR",  "multipart/x-tar");
                    275:     HTAddIcon(prefixed(p,"tar.xbm"),           "TAR",  "multipart/x-gtar");
                    276:     HTAddIcon(prefixed(p,"compressed.xbm"),    "CMP",  "x-compress");
                    277:     HTAddIcon(prefixed(p,"compressed.xbm"),    "GZP",  "x-gzip");
                    278:     HTAddIcon(prefixed(p,"index.xbm"),         "IDX",  "application/x-gopher-index");
                    279:     HTAddIcon(prefixed(p,"index2.xbm"),                "CSO",  "application/x-gopher-cso");
                    280:     HTAddIcon(prefixed(p,"telnet.xbm"),                "TEL",  "application/x-gopher-telnet");
                    281:     HTAddIcon(prefixed(p,"unknown.xbm"),               "DUP",  "application/x-gopher-duplicate");
                    282:     HTAddIcon(prefixed(p,"unknown.xbm"),       "TN",   "application/x-gopher-tn3270");
                    283: }
                    284: 
                    285: 
                    286: /*                                                              HTGetIcon()
                    287: ** returns the icon corresponding to content_type or content_encoding.
                    288: */
2.15    ! frystyk   289: PUBLIC HTIconNode * HTGetIcon ARGS3(HTFileMode,        mode,
2.1       frystyk   290:                                    HTFormat,   content_type,
2.9       frystyk   291:                                    HTEncoding, content_encoding)
2.1       frystyk   292: {
                    293:     if (!icon_unknown) icon_unknown = icon_blank;
2.15    ! frystyk   294:     if (mode == HT_IS_FILE) {
2.1       frystyk   295:        char * ct = content_type ? HTAtom_name(content_type) : NULL;
                    296:        char * ce = content_encoding ? HTAtom_name(content_encoding) : NULL;
                    297:        HTList * cur = icons;
                    298:        HTIconNode * node;
                    299: 
                    300:        while ((node = (HTIconNode*)HTList_nextObject(cur))) {
                    301:            char * slash = strchr(node->type_templ,'/');
                    302:            if ((ct && slash && match(node->type_templ,ct)) ||
                    303:                (ce && !slash && HTAA_templateMatch(node->type_templ,ce))) {
                    304:                return node;
                    305:            }
                    306:        }
2.15    ! frystyk   307:     } else if (mode == HT_IS_DIR) {
2.1       frystyk   308:        return icon_dir ? icon_dir : icon_unknown;
2.15    ! frystyk   309:     } else if (mode == HT_IS_BLANK) {
        !           310:        return icon_blank ? icon_blank : icon_unknown;
        !           311:     } else if (mode == HT_IS_PARENT) {
        !           312:        return icon_parent ? icon_parent : icon_unknown;
2.1       frystyk   313:     }
                    314:     return icon_unknown;
2.2       luotonen  315: }
                    316: 
                    317: 
                    318: /*
                    319:  * Find the URL for a given type. Called from HTDirBrw.c
                    320:  */
                    321: PUBLIC HTHrefNode * HTGetHref ARGS1( char *,   filename)
                    322: {
                    323:     HTHrefNode * node;
                    324:     char *c;
                    325: 
                    326:     HTList * cur = hrefs;
                    327: 
                    328:     c = strrchr(filename, '.');
                    329:     if (c) {
                    330:        while ((node = (HTHrefNode*)HTList_nextObject(cur))) {
                    331:            if ((!strcmp(node->type_templ,c)) ) {
                    332:                return node;
                    333:            }
                    334:        }
                    335:     }
                    336:     return NULL;
2.1       frystyk   337: }
                    338: 
                    339: /* END OF MODULE */
                    340: 
                    341: 

Webmaster