Annotation of libwww/Library/src/HTDescpt.c, revision 2.10

2.1       frystyk     1: /*                                                                HTDescpt.c
                      2: **     FILE DESCRIPTIONS
                      3: **
2.2       frystyk     4: **     (c) COPYRIGHT MIT 1995.
2.1       frystyk     5: **     Please first read the full copyright statement in the file COPYRIGH.
                      6: **
                      7: ** Authors:
                      8: **     AL      Ari Luotonen <luotonen@dxcern.cern.ch>
                      9: **
                     10: **  History:
                     11: **     30 Mar 94  AL   Written from scratch.
                     12: **
                     13: **
                     14: */
                     15: 
                     16: /* Library include files */
                     17: #include "tcp.h"
                     18: #include "HTUtils.h"
                     19: #include "HTString.h"
2.3       frystyk    20: #include "HTFormat.h"
2.1       frystyk    21: #include "HTList.h"
                     22: #include "HTAAUtil.h"
                     23: 
                     24: #define MAX_LINE_LEN 256
                     25: 
2.10    ! frystyk    26: PRIVATE char * HTDescriptionFile = ".www_descript";
        !            27: PRIVATE BOOL HTPeekTitles = YES;
2.1       frystyk    28: 
                     29: /*
                     30:  *     Get the descriptions for files in the given directory.
                     31:  *     The return value is then later passed as an argument
                     32:  *     to HTGetDescription() which returns a description
                     33:  *     string for a single file.
                     34:  */
2.9       frystyk    35: PUBLIC HTList * HTReadDescriptions (char * dirname)
2.1       frystyk    36: {
                     37:     char * name = NULL;
                     38:     FILE * fp = NULL;
                     39:     HTList * list = NULL;
                     40:     char buf[MAX_LINE_LEN + 1];
                     41: 
                     42:     if (!dirname) return NULL;
                     43: 
                     44:     name = (char*)malloc(strlen(dirname) + strlen(HTDescriptionFile) + 2);
                     45:     if (!name) outofmem(__FILE__, "HTReadDescriptions");
                     46: 
                     47:     sprintf(name, "%s/%s", dirname, HTDescriptionFile);
                     48:     fp = fopen(name, "r");
                     49:     if (!fp) {
                     50:        if (PROT_TRACE)
2.6       frystyk    51:            TTYPrint(TDEST, "DirBrowse... No description file %s\n", name);
2.1       frystyk    52:        FREE(name);
                     53:        return NULL;
                     54:     } else {
2.5       frystyk    55:        if (WWWTRACE)
2.6       frystyk    56:            TTYPrint(TDEST, "DirBrowse... Description file found %s\n", name);
2.1       frystyk    57:     }
                     58: 
                     59:     list = HTList_new();
                     60: 
                     61:     while (fgets(buf, MAX_LINE_LEN, fp)) {
                     62:        char * s = buf;
                     63:        char * t = NULL;
                     64:        char * d = NULL;
                     65: 
                     66:        while (*s && WHITE(*s)) s++;            /* Skip initial whitespace */
                     67:        if (*s!='d' && *s!='D') continue;       /* Junk non-description lines*/
                     68: 
                     69:        t = s+1;
                     70:        while (*t && !WHITE(*t)) t++;   /* Find the end of the keyword */
                     71:        while (*t && WHITE(*t)) t++;    /* Find the beginning of template */
                     72: 
                     73:        if (*t) {
                     74:            d = t+1;
                     75:            while (*d && !WHITE(*d)) d++;       /* Find end of template */
                     76:            if (*d) {
                     77:                *d++ = 0;                       /* Terminate template */
                     78:                while (*d && WHITE(*d)) d++;    /* Find start of description */
                     79:                if (*d) {
                     80:                    char * p = d;
                     81:                    while (*p && *p!='\r' && *p!='\n') p++;
                     82:                    *p = 0;                     /* Terminate description */
                     83:                }
                     84:            }
                     85:        }
                     86:        if (t && d && *t && *d) {
                     87:            char * stuff = (char *) malloc(strlen(t) + strlen(d) + 2);
                     88:            if (!stuff) outofmem(__FILE__, "HTDirReadDescriptions");
                     89:            sprintf(stuff, "%s %s", t, d);
                     90:            HTList_addObject(list, (void*)stuff);
                     91:            if (PROT_TRACE)
2.6       frystyk    92:                TTYPrint(TDEST, "Description. %s\n", stuff);
2.1       frystyk    93:        }
                     94:     }
                     95:     fclose(fp);
                     96:     FREE(name);
                     97:     return list;
                     98: }
                     99: 
                    100: 
2.9       frystyk   101: PUBLIC void HTFreeDescriptions (HTList * descriptions)
2.1       frystyk   102: {
                    103:     HTList * cur = descriptions;
                    104:     char * str;
                    105: 
                    106:     if (descriptions) {
                    107:        while ((str = (char*)HTList_nextObject(cur)))
                    108:            free(str);
                    109:        HTList_delete(descriptions);
                    110:     }
                    111: }
                    112: 
                    113: 
2.9       frystyk   114: PRIVATE char * HTPeekTitle (char * dirname,
                    115:                                 char * filename)
2.1       frystyk   116: {
                    117: #define PEEK_BUF_SIZE 200
                    118:     char * name;
                    119:     FILE * fp;
                    120:     char buf[PEEK_BUF_SIZE + 1];
                    121:     int status;
                    122:     char * cur;
                    123:     char * end;
                    124:     static char * ret = NULL;
                    125:     char * p;
                    126:     BOOL space = YES;
                    127: 
                    128:     FREE(ret); /* From previous call */
                    129: 
                    130:     if (PROT_TRACE)
2.6       frystyk   131:        TTYPrint(TDEST, "HTPeekTitle. called, dirname=%s filename=%s\n",
2.1       frystyk   132:                dirname ? dirname : "-null-",
                    133:                filename ? filename : "-null-");
                    134: 
                    135:     if (!dirname || !filename) return NULL;
                    136: 
                    137:     name = (char*)malloc(strlen(dirname) + strlen(filename) + 2);
                    138:     if (!name) outofmem(__FILE__, "HTPeekTitle");
                    139:     sprintf(name, "%s/%s", dirname, filename);
                    140: 
                    141:     fp = fopen(name, "r");
                    142:     if (!fp) {
                    143:        if (PROT_TRACE)
2.6       frystyk   144:            TTYPrint(TDEST, "HTPeekTitle. fopen failed\n");
2.1       frystyk   145:        goto cleanup;
                    146:     }
                    147: 
                    148:     status = fread(buf, 1, PEEK_BUF_SIZE, fp);
                    149:     fclose(fp);
                    150:     if (status <= 0) goto cleanup;
                    151:     buf[status] = 0;
                    152: 
                    153:     cur = buf;
                    154:     while ((cur = strchr(cur,'<'))) {
                    155:        if (!strncasecomp(cur+1,"TITLE>",6)) {
                    156:            cur += 7;
                    157:            end = strchr(cur,'<');
                    158:            while (end && strncasecomp(end+1, "/TITLE>", 7))
                    159:                end = strchr(end+1, '<');
                    160:            if (end) *end = 0;
                    161:            p = ret = (char*)malloc(strlen(cur) + 1);
                    162:            if (!ret) outofmem(__FILE__, "HTPeekTitle");
                    163:            while (*cur) {
                    164:                if (WHITE(*cur)) {
                    165:                    if (!space) {
                    166:                        space = YES;
                    167:                        *p++ = ' ';
                    168:                    }
                    169:                }
                    170:                else {
                    171:                    if (space) space = NO;
                    172:                    *p++ = *cur;
                    173:                }
                    174:                cur++;
                    175:            }
                    176:            *p = 0;
                    177:            goto cleanup;
                    178:        }
                    179:        cur++;
                    180:     }
                    181: 
                    182:   cleanup:
                    183:     if (PROT_TRACE)
2.6       frystyk   184:        TTYPrint(TDEST, "HTPeekTitle. returning %c%s%c\n",
2.1       frystyk   185:                ret ? '"' : '-',  ret ? ret : "null",  ret ? '"' : '-');
                    186:     FREE(name);
                    187:     return ret;
                    188: }
                    189: 
                    190: 
                    191: /*
                    192:  *     Returns a description string (that must not be freed!)
                    193:  *     for a file with name name in directory dirname.
                    194:  *     Description file contents is in descriptions list.
                    195:  */
2.9       frystyk   196: PUBLIC char * HTGetDescription (HTList *       descriptions,
                    197:                                     char *     dirname,
                    198:                                     char *     filename,
                    199:                                     HTFormat   format)
2.1       frystyk   200: {
                    201:     HTList * cur = descriptions;
                    202:     char * t;
                    203: 
                    204:     if (!dirname || !filename) return NULL;
                    205:     /*
                    206:      * descriptions may well be NULL in which case we may still
                    207:      * want to peek the titles.
                    208:      */
                    209: 
                    210:     while ((t = (char*)HTList_nextObject(cur))) {
                    211:        char * d = strchr(t,' ');
                    212:        if (!d) continue;
                    213:        *d = 0;
                    214:        if (HTAA_templateMatch(t,filename)) {
                    215:            *d = ' ';
                    216:            return d+1;
                    217:        }
                    218:        *d = ' ';
                    219:     }
                    220: 
                    221:     if (HTPeekTitles && format == WWW_HTML)
                    222:        return HTPeekTitle(dirname, filename);
                    223:     else
                    224:        return NULL;
                    225: }
                    226: 

Webmaster