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

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.
2.15    ! frystyk     6: **     @(#) $Id: Date Author State $
2.1       frystyk     7: **
                      8: ** Authors:
                      9: **     AL      Ari Luotonen <luotonen@dxcern.cern.ch>
                     10: **
                     11: **  History:
                     12: **     30 Mar 94  AL   Written from scratch.
                     13: **
                     14: **
                     15: */
                     16: 
                     17: /* Library include files */
2.14      frystyk    18: #include "sysdep.h"
2.1       frystyk    19: #include "HTUtils.h"
                     20: #include "HTString.h"
2.3       frystyk    21: #include "HTFormat.h"
2.1       frystyk    22: #include "HTList.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: 
2.12      frystyk    44:     if ((name = (char *) HT_MALLOC(strlen(dirname) + strlen(HTDescriptionFile) + 2)) == NULL)
                     45:         HT_OUTOFMEM("HTReadDescriptions");
2.1       frystyk    46: 
                     47:     sprintf(name, "%s/%s", dirname, HTDescriptionFile);
                     48:     fp = fopen(name, "r");
                     49:     if (!fp) {
                     50:        if (PROT_TRACE)
2.13      eric       51:            HTTrace("DirBrowse... No description file %s\n", name);
2.12      frystyk    52:        HT_FREE(name);
2.1       frystyk    53:        return NULL;
                     54:     } else {
2.5       frystyk    55:        if (WWWTRACE)
2.13      eric       56:            HTTrace("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) {
2.12      frystyk    87:            char * stuff;
                     88:            if ((stuff = (char  *) HT_MALLOC(strlen(t) + strlen(d) + 2)) == NULL)
                     89:                HT_OUTOFMEM("HTDirReadDescriptions");
2.1       frystyk    90:            sprintf(stuff, "%s %s", t, d);
                     91:            HTList_addObject(list, (void*)stuff);
                     92:            if (PROT_TRACE)
2.13      eric       93:                HTTrace("Description. %s\n", stuff);
2.1       frystyk    94:        }
                     95:     }
                     96:     fclose(fp);
2.12      frystyk    97:     HT_FREE(name);
2.1       frystyk    98:     return list;
                     99: }
                    100: 
                    101: 
2.9       frystyk   102: PUBLIC void HTFreeDescriptions (HTList * descriptions)
2.1       frystyk   103: {
                    104:     HTList * cur = descriptions;
                    105:     char * str;
                    106: 
                    107:     if (descriptions) {
                    108:        while ((str = (char*)HTList_nextObject(cur)))
2.12      frystyk   109:            HT_FREE(str);
2.1       frystyk   110:        HTList_delete(descriptions);
                    111:     }
                    112: }
                    113: 
                    114: 
2.9       frystyk   115: PRIVATE char * HTPeekTitle (char * dirname,
                    116:                                 char * filename)
2.1       frystyk   117: {
                    118: #define PEEK_BUF_SIZE 200
                    119:     char * name;
                    120:     FILE * fp;
                    121:     char buf[PEEK_BUF_SIZE + 1];
                    122:     int status;
                    123:     char * cur;
                    124:     char * end;
                    125:     static char * ret = NULL;
                    126:     char * p;
                    127:     BOOL space = YES;
                    128: 
2.12      frystyk   129:     HT_FREE(ret);      /* from previous call */
2.1       frystyk   130: 
                    131:     if (PROT_TRACE)
2.13      eric      132:        HTTrace("HTPeekTitle. called, dirname=%s filename=%s\n",
2.1       frystyk   133:                dirname ? dirname : "-null-",
                    134:                filename ? filename : "-null-");
                    135: 
                    136:     if (!dirname || !filename) return NULL;
                    137: 
2.12      frystyk   138:     if ((name = (char *) HT_MALLOC(strlen(dirname) + strlen(filename) + 2)) == NULL)
                    139:         HT_OUTOFMEM("HTPeekTitle");
2.1       frystyk   140:     sprintf(name, "%s/%s", dirname, filename);
                    141: 
                    142:     fp = fopen(name, "r");
                    143:     if (!fp) {
                    144:        if (PROT_TRACE)
2.13      eric      145:            HTTrace("HTPeekTitle. fopen failed\n");
2.1       frystyk   146:        goto cleanup;
                    147:     }
                    148: 
                    149:     status = fread(buf, 1, PEEK_BUF_SIZE, fp);
                    150:     fclose(fp);
                    151:     if (status <= 0) goto cleanup;
                    152:     buf[status] = 0;
                    153: 
                    154:     cur = buf;
                    155:     while ((cur = strchr(cur,'<'))) {
                    156:        if (!strncasecomp(cur+1,"TITLE>",6)) {
                    157:            cur += 7;
                    158:            end = strchr(cur,'<');
                    159:            while (end && strncasecomp(end+1, "/TITLE>", 7))
                    160:                end = strchr(end+1, '<');
                    161:            if (end) *end = 0;
2.12      frystyk   162:            if ((p = ret = (char*) HT_MALLOC(strlen(cur) + 1)) == NULL)
                    163:                HT_OUTOFMEM("HTPeekTitle");
2.1       frystyk   164:            while (*cur) {
                    165:                if (WHITE(*cur)) {
                    166:                    if (!space) {
                    167:                        space = YES;
                    168:                        *p++ = ' ';
                    169:                    }
                    170:                }
                    171:                else {
                    172:                    if (space) space = NO;
                    173:                    *p++ = *cur;
                    174:                }
                    175:                cur++;
                    176:            }
                    177:            *p = 0;
                    178:            goto cleanup;
                    179:        }
                    180:        cur++;
                    181:     }
                    182: 
                    183:   cleanup:
                    184:     if (PROT_TRACE)
2.13      eric      185:        HTTrace("HTPeekTitle. returning %c%s%c\n",
2.1       frystyk   186:                ret ? '"' : '-',  ret ? ret : "null",  ret ? '"' : '-');
2.12      frystyk   187:     HT_FREE(name);
2.1       frystyk   188:     return ret;
                    189: }
                    190: 
                    191: 
                    192: /*
2.14      frystyk   193:  *     Returns a description string (that must not be HT_FREEd!)
2.1       frystyk   194:  *     for a file with name name in directory dirname.
                    195:  *     Description file contents is in descriptions list.
                    196:  */
2.9       frystyk   197: PUBLIC char * HTGetDescription (HTList *       descriptions,
                    198:                                     char *     dirname,
                    199:                                     char *     filename,
                    200:                                     HTFormat   format)
2.1       frystyk   201: {
                    202:     HTList * cur = descriptions;
                    203:     char * t;
                    204: 
                    205:     if (!dirname || !filename) return NULL;
                    206:     /*
                    207:      * descriptions may well be NULL in which case we may still
                    208:      * want to peek the titles.
                    209:      */
                    210: 
                    211:     while ((t = (char*)HTList_nextObject(cur))) {
                    212:        char * d = strchr(t,' ');
                    213:        if (!d) continue;
                    214:        *d = 0;
2.11      frystyk   215: #if 0
2.1       frystyk   216:        if (HTAA_templateMatch(t,filename)) {
2.11      frystyk   217: #else
                    218:        if (HTStrMatch(t, filename)) {
                    219: #endif
2.1       frystyk   220:            *d = ' ';
                    221:            return d+1;
                    222:        }
                    223:        *d = ' ';
                    224:     }
                    225: 
                    226:     if (HTPeekTitles && format == WWW_HTML)
                    227:        return HTPeekTitle(dirname, filename);
                    228:     else
                    229:        return NULL;
                    230: }
                    231: 

Webmaster