Annotation of libwww/Library/src/HTDir.c, revision 2.7

2.1       frystyk     1: /*                                                                  HTDir.c
                      2: **     DIRECTORY BROWSING
                      3: **
                      4: **     (c) COPYRIGHT MIT 1995.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
                      6: **
                      7: **     This is unix-specific code in general
                      8: **     The module is intended for use in HTFile.c and HTFTP.c where
                      9: **     it replaces the old directory browsing routine.
                     10: **     The module is only compiled if GOT_READ_DIR is defined
                     11: **
                     12: ** Authors:
2.5       frystyk    13: **             HF      Henrik Frystyk, MIT, <frystyk@w3.org>
2.1       frystyk    14: ** History:
                     15: **        Sep 95  HFN  written
                     16: **
                     17: ** Note:
                     18: **     It could be a HTML table instead
                     19: */
                     20: 
                     21: /* Library include files */
                     22: #include "tcp.h"
                     23: #include "HTUtils.h"
                     24: #include "HTString.h"
2.3       frystyk    25: #include "HTMLPDTD.h"
2.1       frystyk    26: #include "HTMLGen.h"
                     27: #include "HTBind.h"
                     28: #include "HTEscape.h"
                     29: #include "HTParse.h"
                     30: #include "HTFormat.h"
                     31: #include "HTReq.h"
                     32: #include "HTIcons.h"
                     33: #include "HTStruct.h"
                     34: #include "HTDescpt.h"
                     35: #include "HTArray.h"
                     36: #include "HTError.h"
                     37: #include "HTDir.h"                                      /* Implemented here */
                     38: 
                     39: /* Macros and other defines */
                     40: #define PUTC(c)                (*target->isa->put_character)(target, c)
                     41: #define PUTS(s)                (*target->isa->put_string)(target, s)
                     42: #define START(e)       (*target->isa->start_element)(target, e, 0, 0)
                     43: #define END(e)         (*target->isa->end_element)(target, e)
                     44: #define FREE_TARGET    (*target->isa->_free)(target)
                     45: 
                     46: #define DEFAULT_MINFW  15
2.4       frystyk    47: #define DEFAULT_MAXFW  25
2.1       frystyk    48: 
                     49: /* Type definitions and global variables etc. local to this module */
                     50: 
                     51: struct _HTStructured {
                     52:     CONST HTStructuredClass *  isa;
                     53:     /* ... */
                     54: };
                     55: 
                     56: struct _HTDir {
                     57:     HTStructured *     target;
                     58:     HTRequest *                request;
                     59:     HTArray *          array;                  /* Array for sorted listings */
                     60:     char *             fnbuf;                           /* File name buffer */
                     61:     char *             lnbuf;                               /* Rest of line */
                     62:     char *             base;                             /* base url is any */
                     63:     HTDirShow          show;                     /* What do we want to show */
                     64:     HTDirKey           key;                              /* Key for sorting */
                     65:     int                        size;                             /* Number of files */
                     66:     int                curfw;               /* Max file name length in list */
                     67: };
                     68: 
                     69: typedef struct _HTDirNode {
                     70:     char *     fname;
                     71:     char *     date;
                     72:     char *     size;
                     73:     char *     note;
                     74:     HTFileMode mode;
                     75: } HTDirNode;
                     76: 
                     77: typedef enum _HTShowLength {                        /* Width of each collumn */
                     78:     HT_DLEN_SIZE  = 6,
                     79:     HT_DLEN_DATE  = 15,
                     80:     HT_DLEN_SPACE = 1,
                     81:     HT_DLEN_DES          = 25
                     82: } HTShowLength;
                     83: 
                     84: PRIVATE int MinFileW = DEFAULT_MINFW;
                     85: PRIVATE int MaxFileW = DEFAULT_MAXFW;
                     86: 
                     87: /* ------------------------------------------------------------------------- */
                     88: /*                             LINE JUSTIFICATION                           */
                     89: /* ------------------------------------------------------------------------- */
                     90: 
                     91: /*
                     92: **     Left-justifies str_in into str_out expecting str_out having size length
                     93: **     l is number of chars. No 0 termination, rest of line filled with ' '
                     94: */
                     95: PRIVATE void LeftStr (char **outstr, char * instr, int l)
                     96: {
                     97:     char *out = *outstr;
                     98:     while (l-- > 0 && *instr && (*out++ = *instr++));
                     99:     while (l-- > 0) *out++ = ' ';
                    100:     *outstr = out;
                    101: }
                    102: 
                    103: /*
                    104: **     Like LeftStr(), but result is right-justified.
                    105: **     l is number of chars. No 0 termination
                    106: */
                    107: PRIVATE void RightStr (char **outstr, char * instr, int l)
                    108: {
                    109:     char *start = *outstr+l-strlen(instr);
                    110:     char *out = *outstr;
                    111:     while (out<start) *out++ = ' ';
                    112:     while (*instr && (*out++ = *instr++));
                    113:     *outstr = out;
                    114: }
                    115: 
                    116: /* ------------------------------------------------------------------------- */
                    117: /*                             NODE  MANAGEMENT                             */
                    118: /* ------------------------------------------------------------------------- */
                    119: 
                    120: /*
                    121: **     Create a sort key node
                    122: */
                    123: PRIVATE HTDirNode * HTDirNode_new (void)
                    124: {
                    125:     HTDirNode *node;
                    126:     if ((node = (HTDirNode *) calloc(1, sizeof(HTDirNode))) == NULL)
                    127:        outofmem(__FILE__, "HTDirNode_new");
                    128:     return node;
                    129: }
                    130: 
                    131: /*
                    132: **     Free a sort key node
                    133: */
                    134: PRIVATE BOOL HTDirNode_free (HTDirNode *node)
                    135: {
                    136:     if (node) {
                    137:        FREE(node->fname);
                    138:        FREE(node->date);
                    139:        FREE(node->size);
                    140:        FREE(node->note);
                    141:        free(node);
                    142:        return YES;
                    143:     }
                    144:     return NO;
                    145: }
                    146: 
                    147: /*
                    148: **     Output an element in HTML
                    149: **     Returns YES if OK, else NO
                    150: */
                    151: PRIVATE BOOL HTDirNode_print (HTDir *dir, HTDirNode *node)
                    152: {
                    153:     char *tp = NULL;
                    154:     HTStructured *target = dir->target;
                    155:     if (dir->show & HT_DS_ICON) {
                    156:        HTFormat format = NULL;
                    157:        HTEncoding encoding = NULL;
                    158:        double q=1.0;
                    159:        HTIconNode *icon;
                    160:        HTHrefNode *href;
                    161:        if (node->mode == HT_IS_FILE)
                    162:            HTBind_getFormat(node->fname, &format, &encoding, NULL, &q);
                    163:        icon = HTGetIcon(node->mode, format, encoding);
                    164:        href = HTGetHref(node->fname);
                    165: 
                    166:        /* Are we having a hot or a cold icon? */
                    167:        if (!(dir->show & HT_DS_HOTI)) {
                    168:            HTMLPutImg(target, icon->icon_url,
                    169:                       HTIcon_alt_string(icon->icon_alt, YES), NULL);
                    170:            PUTC(' ');
                    171:        }
                    172: 
                    173:        /* Start the anchor element */
                    174:        if (dir->base) {
                    175:            char *escaped = HTEscape(node->fname, URL_XPALPHAS);
                    176:            char *full = malloc(strlen(escaped)+strlen(dir->base)+1);
                    177:            if (!full) outofmem(__FILE__, "HTDirNode_print");
                    178:            strcpy(full, dir->base);
                    179:            strcat(full, escaped);
                    180:            HTStartAnchor(target, NULL, full);
                    181:            free(escaped);
                    182:            free(full);
                    183:        } else {
                    184:            char *escaped = HTEscape(node->fname, URL_XPALPHAS);
                    185:            HTStartAnchor(target, NULL, escaped);
                    186:            free(escaped);
                    187:        }
                    188: 
                    189:        if (dir->show & HT_DS_HOTI) {
                    190:            HTMLPutImg(target, icon->icon_url,
                    191:                       HTIcon_alt_string(icon->icon_alt, YES), NULL);
                    192:            PUTC(' ');
                    193:        }
                    194:     } else {
                    195:        if (dir->base) {
                    196:            char *escaped = HTEscape(node->fname, URL_XPALPHAS);
                    197:            char *full = malloc(strlen(escaped)+strlen(dir->base)+1);
                    198:            if (!full) outofmem(__FILE__, "HTDirNode_print");
                    199:            strcpy(full, dir->base);
                    200:            strcat(full, escaped);
                    201:            HTStartAnchor(target, NULL, escaped);
                    202:            free(escaped);
                    203:            free(full);
                    204:        } else {
                    205:            char *escaped = HTEscape(node->fname, URL_XPALPHAS);
                    206:            HTStartAnchor(target, NULL, escaped);
                    207:            free(escaped);
                    208:        }
                    209:     }
                    210:     
                    211:     /* Insert the anchor text and end anchor */
                    212:     {
                    213:        char *in = node->fname;
                    214:        char *out = dir->fnbuf;
                    215:        int l = dir->curfw;
                    216:        while (l-- > 0 && *in && (*out++ = *in++));
                    217:        if (*in)
                    218:            *(out-1) = '>';
                    219:        else if (node->mode == HT_IS_DIR) {
                    220:            *out++ = '/';
                    221:            l--;
                    222:        }
                    223:        *out = '\0';
                    224:        PUTS(dir->fnbuf);
                    225:        END(HTML_A);
                    226:        out = dir->fnbuf;
                    227:        while (l-- >= 0) *out++ = ' ';
                    228:        LeftStr(&out, " ", HT_DLEN_SPACE);
                    229:        *out = '\0';
                    230:        PUTS(dir->fnbuf);
                    231:     }
                    232: 
                    233:     /* Print the rest of it */
                    234:     tp = dir->lnbuf;
                    235:     if (node->date) {
                    236:        RightStr(&tp, node->date, HT_DLEN_DATE);
                    237:        LeftStr(&tp, " ", HT_DLEN_SPACE);
                    238:     }
                    239:     if (node->size) {
                    240:        RightStr(&tp, node->size, HT_DLEN_SIZE);
                    241:        LeftStr(&tp, " ", HT_DLEN_SPACE);
                    242:     }
                    243:     if (node->note) {
                    244:        LeftStr(&tp, node->note, HT_DLEN_DES);
                    245:        LeftStr(&tp, " ", HT_DLEN_SPACE);
                    246:     }
                    247:     *tp = '\0';
                    248:     PUTS(dir->lnbuf);
                    249:     PUTC('\n');
                    250:     return YES;
                    251: }
                    252: 
                    253: /* ------------------------------------------------------------------------- */
                    254: /*                             DIRECTORY MANAGEMENT                         */
                    255: /* ------------------------------------------------------------------------- */
                    256: 
                    257: /*     HTDir_headLine
                    258: **     --------------
                    259: **     Puts out the header line of the list itself
                    260: **     Returns YES if OK, else NO
                    261: */
                    262: PRIVATE BOOL HTDir_headLine (HTDir *dir)
                    263: {
                    264:     if (dir) {
                    265:        char *tp;
                    266:        HTStructured *target = dir->target;
                    267:        START(HTML_PRE);
                    268:        if (dir->show & HT_DS_ICON) {
                    269:            HTIconNode *icon = HTGetIcon(HT_IS_BLANK, NULL, NULL);
                    270:            HTMLPutImg(target, icon->icon_url,
                    271:                       HTIcon_alt_string(icon->icon_alt, YES), NULL);
                    272:            PUTC(' ');
                    273:        }
                    274: 
                    275:        tp = dir->fnbuf;
                    276:        LeftStr(&tp, "Name", dir->curfw);
                    277:        LeftStr(&tp, " ", HT_DLEN_SPACE);
                    278:        *tp = '\0';
                    279:        PUTS(dir->fnbuf);
                    280: 
                    281:        tp = dir->lnbuf;
                    282:        if (dir->show & HT_DS_DATE) {
2.2       frystyk   283:            LeftStr(&tp, "Last Modified", HT_DLEN_DATE);
2.1       frystyk   284:            LeftStr(&tp, " ", HT_DLEN_SPACE);
                    285:        }
                    286:        if (dir->show & HT_DS_SIZE) {
                    287:            RightStr(&tp, "Size", HT_DLEN_SIZE);
                    288:            LeftStr(&tp, " ", HT_DLEN_SPACE);
                    289:        }
                    290:        if (dir->show & HT_DS_DES) {
                    291:            LeftStr(&tp, "Description", HT_DLEN_DATE);
                    292:            LeftStr(&tp, " ", HT_DLEN_SPACE);
                    293:        }
                    294:        *tp = '\0';
                    295:        PUTS(dir->lnbuf);
                    296:        START(HTML_HR);
                    297:        PUTC('\n');
                    298:        return YES;
                    299:     }
                    300:     return NO;
                    301: }
                    302: 
                    303: /*     HTDir_setWidth
                    304: **     --------------
                    305: **     The module automatically ajusts the width of the directory listing as
                    306: **     a function of the file name. The width can flows dynamically between
                    307: **     an upper and a lower limit.
                    308: */
                    309: PUBLIC BOOL HTDir_setWidth (int minfile, int maxfile)
                    310: {
                    311:     MinFileW = (minfile>=0) ? minfile : 0;
                    312:     MaxFileW = (maxfile>minfile) ? maxfile : minfile+1;
                    313:     return YES;
                    314: }
                    315: 
                    316: /*     HTDir_new
                    317: **     ---------
                    318: **     Creates a structured stream object and sets up the initial HTML stuff
                    319: **     Returns the dir object if OK, else NULL
                    320: */
                    321: PUBLIC HTDir * HTDir_new (HTRequest * request, HTDirShow show, HTDirKey key)
                    322: {    
                    323:     HTDir *dir;
                    324:     char *title = NULL;
                    325:     if (!request) return NULL;
                    326: 
                    327:     /* Create object */
                    328:     if ((dir = (HTDir *) calloc(1, sizeof (HTDir))) == NULL ||
                    329:        (dir->fnbuf = (char *) malloc(MaxFileW+HT_DLEN_SPACE)) == NULL)
                    330:        outofmem(__FILE__, "HTDir_new");
                    331:     dir->target = HTMLGenerator(request, NULL, WWW_HTML,
                    332:                               HTRequest_outputFormat(request),
                    333:                               HTRequest_outputStream(request));
2.7     ! frystyk   334:     HTAnchor_setFormat(HTRequest_anchor(request), WWW_HTML);
2.1       frystyk   335:     dir->request = request;
                    336:     dir->show = show;
                    337:     dir->key = key;
                    338:     if (key==HT_DK_NONE)
                    339:        dir->curfw = MaxFileW;
                    340:     else {
                    341:        dir->curfw = MinFileW;
                    342:        dir->array = HTArray_new(256);
                    343:     }
                    344: 
                    345:     /* Find the length of the fields */
                    346:     {
                    347:        int len = HT_DLEN_SPACE+1;
                    348:        if (show & HT_DS_SIZE) len += (HT_DLEN_SIZE+HT_DLEN_SPACE);
                    349:        if (show & HT_DS_DATE) len += (HT_DLEN_DATE+HT_DLEN_SPACE);
                    350:        if (show & HT_DS_DES) len += HT_DLEN_DES;
                    351:        if ((dir->lnbuf = (char *) malloc(len)) == NULL)
                    352:            outofmem(__FILE__, "HTDir_new");
                    353:     }
                    354: 
                    355:     /* Find the title and the base URL */
                    356:     {
                    357:        char *addr = HTAnchor_address((HTAnchor *) HTRequest_anchor(request));
                    358:        char *path = HTParse(addr, "", PARSE_PATH+PARSE_PUNCTUATION);
                    359:        char *ptr;
                    360:        if ((ptr = strchr(path, ';')) || (ptr = strchr(path, '?')))
                    361:            *ptr = '\0';
                    362:        StrAllocCopy(title, path);
                    363:        HTUnEscape(title);                                          /* Title */
                    364:        if((ptr=strrchr(path, '/')) && (ptr<path+strlen(path)-1 || ptr==path)){
                    365:            StrAllocCopy(dir->base, ++ptr);
                    366:            StrAllocCat(dir->base, "/");
                    367:        }
2.6       frystyk   368:        if (PROT_TRACE) TTYPrint(TDEST, "HTDir_new... base is `%s\'\n",
2.1       frystyk   369:                                dir->base ? dir->base : "");
                    370:        free(addr);
                    371:        free(path);
                    372:     }
                    373: 
                    374:     /* Start the HTML stuff */
                    375:     {
                    376:        HTStructured *target = dir->target;
                    377:        START(HTML_HTML);
                    378:        START(HTML_HEAD);
                    379:        START(HTML_TITLE);
                    380:        PUTS("Current index is ");
                    381:        PUTS(title);
                    382:        END(HTML_TITLE);
                    383:        END(HTML_HEAD);
                    384:        START(HTML_BODY);
                    385:        START(HTML_H1);
                    386:        PUTS("Index of ");
                    387:        PUTS(title);
                    388:        END(HTML_H1);
                    389:     }
                    390:     FREE(title);
                    391:     return dir;
                    392: }
                    393: 
                    394: /*     HTDir_addElement
                    395: **     ---------------
                    396: **     This function accepts a directory line. "data" and "size", and
                    397: **     "description" can all be NULL
                    398: **     Returns YES if OK, else NO
                    399: */
                    400: PUBLIC BOOL HTDir_addElement (HTDir *dir, char *name, char *date, char *size,
                    401:                              HTFileMode mode)
                    402: {
                    403:     HTDirNode *node = HTDirNode_new();
                    404:     if (!dir || !name) return NO;
                    405:     StrAllocCopy(node->fname, name);                           /* Mandatory */
                    406:     if (dir->show & HT_DS_DATE && date) StrAllocCopy(node->date, date);
                    407:     if (dir->show & HT_DS_SIZE && size) StrAllocCopy(node->size, size);
                    408:     if (dir->show & HT_DS_DES) {
                    409: #if 0
                    410: 
                    411:        /* FIND DESCRIPTION */
                    412: 
                    413: #endif
                    414:     }
                    415:     node->mode = mode;
                    416:     if (dir->key == HT_DK_NONE) {
                    417:        if (!dir->size++) HTDir_headLine(dir);
                    418:        HTDirNode_print(dir, node);
                    419:        HTDirNode_free(node);
                    420:     } else {
                    421:        int slen = strlen(name);
                    422:        if (slen > dir->curfw)
                    423:            dir->curfw = slen < MaxFileW ? slen : MaxFileW;
                    424:        HTArray_addObject(dir->array, (void *) node);
                    425:     }
                    426:     return YES;
                    427: }
                    428: 
                    429: PRIVATE int DirSort (CONST void *a, CONST void *b)
                    430: {
                    431:     HTDirNode *aa = *(HTDirNode **) a;
                    432:     HTDirNode *bb = *(HTDirNode **) b;
                    433:     return strcmp(aa->fname, bb->fname);
                    434: #if 0
                    435:     return strcmp((*(HTDirNode**)a)->fname, (*(HTDirNode**)a)->fname);
                    436: #endif
                    437: }
                    438: 
                    439: PRIVATE int DirCaseSort (CONST void *a, CONST void *b)
                    440: {
                    441:     HTDirNode *aa = *(HTDirNode **) a;
                    442:     HTDirNode *bb = *(HTDirNode **) b;
                    443:     return strcasecomp(aa->fname, bb->fname);
                    444: #if 0
                    445:     return strcasecomp((*(HTDirNode**)a)->fname, (*(HTDirNode**)a)->fname);
                    446: #endif
                    447: }
                    448: 
                    449: /*     HTDir_free
                    450: **     ----------
                    451: **     If we are sorting then do the sorting and put out the list,
                    452: **     else just append the end of the list.
                    453: */
                    454: PUBLIC BOOL HTDir_free (HTDir * dir)
                    455: {
                    456:     if (!dir) return NO;
                    457:     if (dir->key != HT_DK_NONE) {
                    458:        HTArray *array = dir->array;
                    459:        void **data;
                    460:        HTDirNode *node;
                    461:        HTDir_headLine(dir);    
                    462:        HTArray_sort(array, (dir->key==HT_DK_CINS ? DirCaseSort : DirSort));
                    463:        node = (HTDirNode *) HTArray_firstObject(array, data);
                    464:        while (node) {
                    465:            HTDirNode_print(dir, node);
                    466:            HTDirNode_free(node);
                    467:            node = (HTDirNode *) HTArray_nextObject(array, data);
                    468:        }
                    469:        dir->size = HTArray_size(array);
                    470:        HTArray_delete(array);  
                    471:     }
                    472: 
                    473:     /* Put out the end of the HTML stuff */
                    474:     {
                    475:        HTStructured *target = dir->target;
                    476:        START(HTML_HR);
                    477:        if (!dir->size)
                    478:            PUTS("Empty directory");
                    479:        else if (dir->size == 1)
                    480:            PUTS("1 File");
                    481:        else {
                    482:            char buffer[20];
                    483:            sprintf(buffer, "%u files", dir->size);
                    484:            PUTS(buffer);
                    485:        }
                    486:        END(HTML_PRE);
                    487:        END(HTML_BODY);
                    488:        END(HTML_HTML);
                    489:        FREE_TARGET;
                    490:     }
                    491: 
                    492:     FREE(dir->fnbuf);
                    493:     FREE(dir->lnbuf);
                    494:     FREE(dir->base);
                    495:     free(dir);
                    496:     return YES;
                    497: }

Webmaster