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

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: 
                     23: #define MAX_LINE_LEN 256
                     24: 
2.10      frystyk    25: PRIVATE char * HTDescriptionFile = ".www_descript";
                     26: PRIVATE BOOL HTPeekTitles = YES;
2.1       frystyk    27: 
                     28: /*
                     29:  *     Get the descriptions for files in the given directory.
                     30:  *     The return value is then later passed as an argument
                     31:  *     to HTGetDescription() which returns a description
                     32:  *     string for a single file.
                     33:  */
2.9       frystyk    34: PUBLIC HTList * HTReadDescriptions (char * dirname)
2.1       frystyk    35: {
                     36:     char * name = NULL;
                     37:     FILE * fp = NULL;
                     38:     HTList * list = NULL;
                     39:     char buf[MAX_LINE_LEN + 1];
                     40: 
                     41:     if (!dirname) return NULL;
                     42: 
2.12      frystyk    43:     if ((name = (char *) HT_MALLOC(strlen(dirname) + strlen(HTDescriptionFile) + 2)) == NULL)
                     44:         HT_OUTOFMEM("HTReadDescriptions");
2.1       frystyk    45: 
                     46:     sprintf(name, "%s/%s", dirname, HTDescriptionFile);
                     47:     fp = fopen(name, "r");
                     48:     if (!fp) {
                     49:        if (PROT_TRACE)
2.13    ! eric       50:            HTTrace("DirBrowse... No description file %s\n", name);
2.12      frystyk    51:        HT_FREE(name);
2.1       frystyk    52:        return NULL;
                     53:     } else {
2.5       frystyk    54:        if (WWWTRACE)
2.13    ! eric       55:            HTTrace("DirBrowse... Description file found %s\n", name);
2.1       frystyk    56:     }
                     57: 
                     58:     list = HTList_new();
                     59: 
                     60:     while (fgets(buf, MAX_LINE_LEN, fp)) {
                     61:        char * s = buf;
                     62:        char * t = NULL;
                     63:        char * d = NULL;
                     64: 
                     65:        while (*s && WHITE(*s)) s++;            /* Skip initial whitespace */
                     66:        if (*s!='d' && *s!='D') continue;       /* Junk non-description lines*/
                     67: 
                     68:        t = s+1;
                     69:        while (*t && !WHITE(*t)) t++;   /* Find the end of the keyword */
                     70:        while (*t && WHITE(*t)) t++;    /* Find the beginning of template */
                     71: 
                     72:        if (*t) {
                     73:            d = t+1;
                     74:            while (*d && !WHITE(*d)) d++;       /* Find end of template */
                     75:            if (*d) {
                     76:                *d++ = 0;                       /* Terminate template */
                     77:                while (*d && WHITE(*d)) d++;    /* Find start of description */
                     78:                if (*d) {
                     79:                    char * p = d;
                     80:                    while (*p && *p!='\r' && *p!='\n') p++;
                     81:                    *p = 0;                     /* Terminate description */
                     82:                }
                     83:            }
                     84:        }
                     85:        if (t && d && *t && *d) {
2.12      frystyk    86:            char * stuff;
                     87:            if ((stuff = (char  *) HT_MALLOC(strlen(t) + strlen(d) + 2)) == NULL)
                     88:                HT_OUTOFMEM("HTDirReadDescriptions");
2.1       frystyk    89:            sprintf(stuff, "%s %s", t, d);
                     90:            HTList_addObject(list, (void*)stuff);
                     91:            if (PROT_TRACE)
2.13    ! eric       92:                HTTrace("Description. %s\n", stuff);
2.1       frystyk    93:        }
                     94:     }
                     95:     fclose(fp);
2.12      frystyk    96:     HT_FREE(name);
2.1       frystyk    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)))
2.12      frystyk   108:            HT_FREE(str);
2.1       frystyk   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: 
2.12      frystyk   128:     HT_FREE(ret);      /* from previous call */
2.1       frystyk   129: 
                    130:     if (PROT_TRACE)
2.13    ! eric      131:        HTTrace("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: 
2.12      frystyk   137:     if ((name = (char *) HT_MALLOC(strlen(dirname) + strlen(filename) + 2)) == NULL)
                    138:         HT_OUTOFMEM("HTPeekTitle");
2.1       frystyk   139:     sprintf(name, "%s/%s", dirname, filename);
                    140: 
                    141:     fp = fopen(name, "r");
                    142:     if (!fp) {
                    143:        if (PROT_TRACE)
2.13    ! eric      144:            HTTrace("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;
2.12      frystyk   161:            if ((p = ret = (char*) HT_MALLOC(strlen(cur) + 1)) == NULL)
                    162:                HT_OUTOFMEM("HTPeekTitle");
2.1       frystyk   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.13    ! eric      184:        HTTrace("HTPeekTitle. returning %c%s%c\n",
2.1       frystyk   185:                ret ? '"' : '-',  ret ? ret : "null",  ret ? '"' : '-');
2.12      frystyk   186:     HT_FREE(name);
2.1       frystyk   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;
2.11      frystyk   214: #if 0
2.1       frystyk   215:        if (HTAA_templateMatch(t,filename)) {
2.11      frystyk   216: #else
                    217:        if (HTStrMatch(t, filename)) {
                    218: #endif
2.1       frystyk   219:            *d = ' ';
                    220:            return d+1;
                    221:        }
                    222:        *d = ' ';
                    223:     }
                    224: 
                    225:     if (HTPeekTitles && format == WWW_HTML)
                    226:        return HTPeekTitle(dirname, filename);
                    227:     else
                    228:        return NULL;
                    229: }
                    230: 

Webmaster