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

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

Webmaster