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

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: 
        !            22: /* Globals */
        !            23: PUBLIC BOOL HTDirShowBrackets = YES;
        !            24: PUBLIC HTIconNode * icon_unknown = NULL;       /* Unknown file type */
        !            25: PUBLIC HTIconNode * icon_blank = NULL;         /* Blank icon in heading */
        !            26: PUBLIC HTIconNode * icon_parent = NULL;                /* Parent directory icon */
        !            27: PUBLIC HTIconNode * icon_dir = NULL;           /* Directory icon */
        !            28: 
        !            29: /* Type definitions and global variables etc. local to this module */
        !            30: PRIVATE HTList * icons = NULL;
        !            31: PRIVATE int alt_len = 0;                       /* Longest ALT text */
        !            32: 
        !            33: /* ------------------------------------------------------------------------- */
        !            34: 
        !            35: PRIVATE void alt_resize ARGS1(char *, alt)
        !            36: {
        !            37:     if (alt) {
        !            38:        int len = strlen(alt);
        !            39:        if (len > alt_len) alt_len = len;
        !            40:     }
        !            41: }
        !            42: 
        !            43: 
        !            44: PUBLIC char * HTIcon_alt_string ARGS2(char *,  alt,
        !            45:                                      BOOL,     brackets)
        !            46: {
        !            47:     static char * ret = NULL;
        !            48:     char * p = NULL;
        !            49:     int len = alt ? strlen(alt) : 0;
        !            50: 
        !            51:     if (ret) free(ret);                        /* From previous call */
        !            52:     p = ret = (char*)malloc(alt_len + 3);
        !            53:     if (!ret) outofmem(__FILE__, "HTIcon_alt_string");
        !            54: 
        !            55:     if (HTDirShowBrackets)
        !            56:        *p++ = brackets ? '[' : ' ';
        !            57:     if (alt) strcpy(p,alt);
        !            58:     p += len;
        !            59:     while (len++ < alt_len) *p++=' ';
        !            60:     if (HTDirShowBrackets)
        !            61:        *p++ = brackets ? ']' : ' ';
        !            62:     *p = 0;
        !            63: 
        !            64:     return ret;
        !            65: }
        !            66: 
        !            67: 
        !            68: /*
        !            69: **     HTAddIcon(url, alt, type_templ) adds icon:
        !            70: **
        !            71: **             <IMG SRC="url" ALT="[alt]">
        !            72: **
        !            73: **     for files for which content-type or content-encoding matches
        !            74: **     type_templ.  If type_templ contains a slash, it is taken to be
        !            75: **     a content-type template.  Otherwise, it is a content-encoding
        !            76: **     template.
        !            77: */
        !            78: PUBLIC void HTAddIcon ARGS3(char *,    url,
        !            79:                            char *,     alt,
        !            80:                            char *,     type_templ)
        !            81: {
        !            82:     HTIconNode * node;
        !            83: 
        !            84:     if (!url || !type_templ) return;
        !            85: 
        !            86:     node = (HTIconNode*)calloc(1,sizeof(HTIconNode));
        !            87:     if (!node) outofmem(__FILE__, "HTAddIcon");
        !            88: 
        !            89:     if (url) StrAllocCopy(node->icon_url, url);
        !            90:     if (alt) StrAllocCopy(node->icon_alt, alt);
        !            91:     if (type_templ) StrAllocCopy(node->type_templ, type_templ);
        !            92: 
        !            93:     if (!icons) icons = HTList_new();
        !            94:     HTList_addObject(icons, (void*)node);
        !            95:     alt_resize(alt);
        !            96:     CTRACE(stderr,
        !            97:           "AddIcon..... %s => SRC=\"%s\" ALT=\"%s\"\n",type_templ,url,
        !            98:           alt ? alt : "");
        !            99: }
        !           100: 
        !           101: 
        !           102: /*
        !           103: **     HTAddUnknownIcon(url,alt) adds the icon used for files for which
        !           104: **     no other icon seems appropriate (unknown type).
        !           105: */
        !           106: PUBLIC void HTAddUnknownIcon ARGS2(char *, url,
        !           107:                                   char *, alt)
        !           108: {
        !           109:     icon_unknown = (HTIconNode*)calloc(1,sizeof(HTIconNode));
        !           110:     if (!icon_unknown) outofmem(__FILE__, "HTAddUnknownIcon");
        !           111: 
        !           112:     if (url) StrAllocCopy(icon_unknown->icon_url, url);
        !           113:     if (alt) StrAllocCopy(icon_unknown->icon_alt, alt);
        !           114:     alt_resize(alt);
        !           115:     CTRACE(stderr,"AddIcon..... UNKNOWN => SRC=\"%s\" ALT=\"%s\"\n",url,
        !           116:           alt ? alt : "");
        !           117: 
        !           118: }
        !           119: 
        !           120: 
        !           121: /*
        !           122: **     HTAddBlankIcon(url,alt) adds the blank icon used in the
        !           123: **     heading of the listing.
        !           124: */
        !           125: PUBLIC void HTAddBlankIcon ARGS2(char *, url,
        !           126:                                 char *, alt)
        !           127: {
        !           128:     icon_blank = (HTIconNode*)calloc(1,sizeof(HTIconNode));
        !           129:     if (!icon_blank) outofmem(__FILE__, "HTAddBlankIcon");
        !           130: 
        !           131:     if (url) StrAllocCopy(icon_blank->icon_url, url);
        !           132:     if (alt) StrAllocCopy(icon_blank->icon_alt, alt);
        !           133:     alt_resize(alt);
        !           134:     CTRACE(stderr,"AddIcon..... BLANK => SRC=\"%s\" ALT=\"%s\"\n",url,
        !           135:           alt ? alt : "");
        !           136: }
        !           137: 
        !           138: 
        !           139: /*
        !           140: **     HTAddParentIcon(url,alt) adds the parent directory icon.
        !           141: */
        !           142: PUBLIC void HTAddParentIcon ARGS2(char *, url,
        !           143:                                  char *, alt)
        !           144: {
        !           145:     icon_parent = (HTIconNode*)calloc(1,sizeof(HTIconNode));
        !           146:     if (!icon_parent) outofmem(__FILE__, "HTAddBlankIcon");
        !           147: 
        !           148:     if (url) StrAllocCopy(icon_parent->icon_url, url);
        !           149:     if (alt) StrAllocCopy(icon_parent->icon_alt, alt);
        !           150:     alt_resize(alt);
        !           151:     CTRACE(stderr,"AddIcon..... PARENT => SRC=\"%s\" ALT=\"%s\"\n",url,
        !           152:           alt ? alt : "");
        !           153: }
        !           154: 
        !           155: 
        !           156: /*
        !           157: **     HTAddDirIcon(url,alt) adds the directory icon.
        !           158: */
        !           159: PUBLIC void HTAddDirIcon ARGS2(char *, url,
        !           160:                               char *, alt)
        !           161: {
        !           162:     icon_dir = (HTIconNode*)calloc(1,sizeof(HTIconNode));
        !           163:     if (!icon_dir) outofmem(__FILE__, "HTAddBlankIcon");
        !           164: 
        !           165:     if (url) StrAllocCopy(icon_dir->icon_url, url);
        !           166:     if (alt) StrAllocCopy(icon_dir->icon_alt, alt);
        !           167:     alt_resize(alt);
        !           168:     CTRACE(stderr,
        !           169:           "AddIcon..... DIRECTORY => SRC=\"%s\" ALT=\"%s\"\n",url,
        !           170:           alt ? alt : "");
        !           171: }
        !           172: 
        !           173: 
        !           174: PRIVATE BOOL match ARGS2(char *, templ,
        !           175:                         char *, actual)
        !           176: {
        !           177:     static char * c1 = NULL;
        !           178:     static char * c2 = NULL;
        !           179:     char * slash1;
        !           180:     char * slash2;
        !           181: 
        !           182:     StrAllocCopy(c1,templ);
        !           183:     StrAllocCopy(c2,actual);
        !           184: 
        !           185:     slash1 = strchr(c1,'/');
        !           186:     slash2 = strchr(c2,'/');
        !           187: 
        !           188:     if (slash1 && slash2) {
        !           189:        *slash1++ = 0;
        !           190:        *slash2++ = 0;
        !           191:        return HTAA_templateMatch(c1,c2) && HTAA_templateMatch(slash1,slash2);
        !           192:     }
        !           193:     else if (!slash1 && !slash2)
        !           194:        return HTAA_templateMatch(c1,c2);
        !           195:     else
        !           196:        return NO;
        !           197: }
        !           198: 
        !           199: 
        !           200: PRIVATE char * prefixed ARGS2(char *,  prefix,
        !           201:                              char *,   name)
        !           202: {
        !           203:     static char * ret = NULL;
        !           204:     FREE(ret); /* From previous call */
        !           205: 
        !           206:     ret = (char *)malloc(strlen(prefix) + strlen(name) + 2);
        !           207:     if (!ret) outofmem(__FILE__, "prefixed");
        !           208: 
        !           209:     strcpy(ret,prefix);
        !           210:     if (*prefix && prefix[strlen(prefix)-1] != '/')
        !           211:        strcat(ret,"/");
        !           212:     strcat(ret,name);
        !           213:     return ret;
        !           214: }
        !           215: 
        !           216: 
        !           217: PUBLIC void HTStdIconInit ARGS1(char *, url_prefix)
        !           218: {
        !           219:     char * p = url_prefix ? url_prefix : "/internal-icon/";
        !           220: 
        !           221:     HTAddBlankIcon  (prefixed(p,"blank.xbm"),  NULL    );
        !           222:     HTAddDirIcon    (prefixed(p,"directory.xbm"),"DIR" );
        !           223:     HTAddParentIcon (prefixed(p,"back.xbm"),   "UP"    );
        !           224:     HTAddUnknownIcon(prefixed(p,"unknown.xbm"),        NULL    );
        !           225:     HTAddIcon(prefixed(p,"unknown.xbm"),       NULL,   "*/*");
        !           226:     HTAddIcon(prefixed(p,"binary.xbm"),                "BIN",  "binary");
        !           227:     HTAddIcon(prefixed(p,"unknown.xbm"),       NULL,   "www/unknown");
        !           228:     HTAddIcon(prefixed(p,"text.xbm"),          "TXT",  "text/*");
        !           229:     HTAddIcon(prefixed(p,"image.xbm"),         "IMG",  "image/*");
        !           230:     HTAddIcon(prefixed(p,"movie.xbm"),         "MOV",  "video/*");
        !           231:     HTAddIcon(prefixed(p,"sound.xbm"),         "AU",   "audio/*");
        !           232:     HTAddIcon(prefixed(p,"tar.xbm"),           "TAR",  "multipart/x-tar");
        !           233:     HTAddIcon(prefixed(p,"tar.xbm"),           "TAR",  "multipart/x-gtar");
        !           234:     HTAddIcon(prefixed(p,"compressed.xbm"),    "CMP",  "x-compress");
        !           235:     HTAddIcon(prefixed(p,"compressed.xbm"),    "GZP",  "x-gzip");
        !           236:     HTAddIcon(prefixed(p,"index.xbm"),         "IDX",  "application/x-gopher-index");
        !           237:     HTAddIcon(prefixed(p,"index2.xbm"),                "CSO",  "application/x-gopher-cso");
        !           238:     HTAddIcon(prefixed(p,"telnet.xbm"),                "TEL",  "application/x-gopher-telnet");
        !           239:     HTAddIcon(prefixed(p,"unknown.xbm"),               "DUP",  "application/x-gopher-duplicate");
        !           240:     HTAddIcon(prefixed(p,"unknown.xbm"),       "TN",   "application/x-gopher-tn3270");
        !           241: }
        !           242: 
        !           243: 
        !           244: /*                                                              HTGetIcon()
        !           245: ** returns the icon corresponding to content_type or content_encoding.
        !           246: */
        !           247: PUBLIC HTIconNode * HTGetIcon ARGS3(mode_t,    mode,
        !           248:                                    HTFormat,   content_type,
        !           249:                                    HTFormat,   content_encoding)
        !           250: {
        !           251:     if (!icon_unknown) icon_unknown = icon_blank;
        !           252: 
        !           253:     if ((mode & S_IFMT) == S_IFREG) {
        !           254:        char * ct = content_type ? HTAtom_name(content_type) : NULL;
        !           255:        char * ce = content_encoding ? HTAtom_name(content_encoding) : NULL;
        !           256:        HTList * cur = icons;
        !           257:        HTIconNode * node;
        !           258: 
        !           259:        while ((node = (HTIconNode*)HTList_nextObject(cur))) {
        !           260:            char * slash = strchr(node->type_templ,'/');
        !           261:            if ((ct && slash && match(node->type_templ,ct)) ||
        !           262:                (ce && !slash && HTAA_templateMatch(node->type_templ,ce))) {
        !           263:                return node;
        !           264:            }
        !           265:        }
        !           266:     } else if ((mode & S_IFMT) == S_IFDIR) {
        !           267:        return icon_dir ? icon_dir : icon_unknown;
        !           268:     } else if ((mode & S_IFMT) == S_IFLNK) {
        !           269:        return icon_dir ? icon_dir : icon_unknown;      /* @@ */
        !           270:     }
        !           271: 
        !           272:     return icon_unknown;
        !           273: }
        !           274: 
        !           275: /* END OF MODULE */
        !           276: 
        !           277: 

Webmaster