Annotation of libwww/Library/src/HTDir.c, revision 2.16
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.16 ! frystyk 6: ** @(#) $Id: HTDir.c,v 2.15 1996/06/28 16:30:59 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.16 ! frystyk 341: HTRequest_setOutputConnected(request, YES);
2.7 frystyk 342: HTAnchor_setFormat(HTRequest_anchor(request), WWW_HTML);
2.1 frystyk 343: dir->request = request;
344: dir->show = show;
345: dir->key = key;
346: if (key==HT_DK_NONE)
347: dir->curfw = MaxFileW;
348: else {
349: dir->curfw = MinFileW;
350: dir->array = HTArray_new(256);
351: }
2.16 ! frystyk 352:
! 353: /* We're all OK */
! 354: HTRequest_addError(request, ERR_INFO, NO, HTERR_OK, NULL, 0, "HTDir_new");
2.1 frystyk 355:
356: /* Find the length of the fields */
357: {
358: int len = HT_DLEN_SPACE+1;
359: if (show & HT_DS_SIZE) len += (HT_DLEN_SIZE+HT_DLEN_SPACE);
360: if (show & HT_DS_DATE) len += (HT_DLEN_DATE+HT_DLEN_SPACE);
361: if (show & HT_DS_DES) len += HT_DLEN_DES;
2.10 frystyk 362: if ((dir->lnbuf = (char *) HT_MALLOC(len)) == NULL)
363: HT_OUTOFMEM("HTDir_new");
2.1 frystyk 364: }
365:
366: /* Find the title and the base URL */
367: {
368: char *addr = HTAnchor_address((HTAnchor *) HTRequest_anchor(request));
369: char *path = HTParse(addr, "", PARSE_PATH+PARSE_PUNCTUATION);
370: char *ptr;
371: if ((ptr = strchr(path, ';')) || (ptr = strchr(path, '?')))
372: *ptr = '\0';
373: StrAllocCopy(title, path);
374: HTUnEscape(title); /* Title */
375: if((ptr=strrchr(path, '/')) && (ptr<path+strlen(path)-1 || ptr==path)){
376: StrAllocCopy(dir->base, ++ptr);
377: StrAllocCat(dir->base, "/");
378: }
2.11 eric 379: if (PROT_TRACE) HTTrace("HTDir_new... base is `%s\'\n",
2.1 frystyk 380: dir->base ? dir->base : "");
2.10 frystyk 381: HT_FREE(addr);
382: HT_FREE(path);
2.1 frystyk 383: }
384:
385: /* Start the HTML stuff */
386: {
387: HTStructured *target = dir->target;
388: START(HTML_HTML);
389: START(HTML_HEAD);
390: START(HTML_TITLE);
391: PUTS("Current index is ");
392: PUTS(title);
393: END(HTML_TITLE);
394: END(HTML_HEAD);
395: START(HTML_BODY);
396: START(HTML_H1);
397: PUTS("Index of ");
398: PUTS(title);
399: END(HTML_H1);
400: }
2.10 frystyk 401: HT_FREE(title);
2.1 frystyk 402: return dir;
403: }
404:
405: /* HTDir_addElement
406: ** ---------------
407: ** This function accepts a directory line. "data" and "size", and
408: ** "description" can all be NULL
409: ** Returns YES if OK, else NO
410: */
411: PUBLIC BOOL HTDir_addElement (HTDir *dir, char *name, char *date, char *size,
412: HTFileMode mode)
413: {
414: HTDirNode *node = HTDirNode_new();
415: if (!dir || !name) return NO;
416: StrAllocCopy(node->fname, name); /* Mandatory */
417: if (dir->show & HT_DS_DATE && date) StrAllocCopy(node->date, date);
418: if (dir->show & HT_DS_SIZE && size) StrAllocCopy(node->size, size);
419: if (dir->show & HT_DS_DES) {
420: #if 0
421:
422: /* FIND DESCRIPTION */
423:
424: #endif
425: }
426: node->mode = mode;
427: if (dir->key == HT_DK_NONE) {
428: if (!dir->size++) HTDir_headLine(dir);
429: HTDirNode_print(dir, node);
430: HTDirNode_free(node);
431: } else {
432: int slen = strlen(name);
433: if (slen > dir->curfw)
434: dir->curfw = slen < MaxFileW ? slen : MaxFileW;
435: HTArray_addObject(dir->array, (void *) node);
436: }
437: return YES;
438: }
439:
2.13 frystyk 440: PRIVATE int DirSort (const void *a, const void *b)
2.1 frystyk 441: {
2.8 frystyk 442: #if 0
2.1 frystyk 443: HTDirNode *aa = *(HTDirNode **) a;
444: HTDirNode *bb = *(HTDirNode **) b;
445: return strcmp(aa->fname, bb->fname);
2.8 frystyk 446: #else
2.15 frystyk 447: return strcmp((*((HTDirNode **) a))->fname,
448: (*((HTDirNode **) b))->fname);
2.1 frystyk 449: #endif
450: }
451:
2.13 frystyk 452: PRIVATE int DirCaseSort (const void *a, const void *b)
2.1 frystyk 453: {
2.8 frystyk 454: #if 0
2.1 frystyk 455: HTDirNode *aa = *(HTDirNode **) a;
456: HTDirNode *bb = *(HTDirNode **) b;
457: return strcasecomp(aa->fname, bb->fname);
2.8 frystyk 458: #else
2.15 frystyk 459: return strcasecomp((*((HTDirNode **) a))->fname,
460: (*((HTDirNode **) b))->fname);
2.1 frystyk 461: #endif
462: }
463:
464: /* HTDir_free
465: ** ----------
466: ** If we are sorting then do the sorting and put out the list,
467: ** else just append the end of the list.
468: */
469: PUBLIC BOOL HTDir_free (HTDir * dir)
470: {
471: if (!dir) return NO;
472: if (dir->key != HT_DK_NONE) {
473: HTArray *array = dir->array;
474: void **data;
475: HTDirNode *node;
476: HTDir_headLine(dir);
477: HTArray_sort(array, (dir->key==HT_DK_CINS ? DirCaseSort : DirSort));
478: node = (HTDirNode *) HTArray_firstObject(array, data);
479: while (node) {
480: HTDirNode_print(dir, node);
481: HTDirNode_free(node);
482: node = (HTDirNode *) HTArray_nextObject(array, data);
483: }
484: dir->size = HTArray_size(array);
485: HTArray_delete(array);
486: }
487:
488: /* Put out the end of the HTML stuff */
489: {
490: HTStructured *target = dir->target;
491: START(HTML_HR);
492: if (!dir->size)
493: PUTS("Empty directory");
494: else if (dir->size == 1)
495: PUTS("1 File");
496: else {
497: char buffer[20];
498: sprintf(buffer, "%u files", dir->size);
499: PUTS(buffer);
500: }
501: END(HTML_PRE);
502: END(HTML_BODY);
503: END(HTML_HTML);
504: FREE_TARGET;
505: }
506:
2.10 frystyk 507: HT_FREE(dir->fnbuf);
508: HT_FREE(dir->lnbuf);
509: HT_FREE(dir->base);
510: HT_FREE(dir);
2.1 frystyk 511: return YES;
512: }
Webmaster