Annotation of libwww/Library/src/HTDir.c, revision 2.13
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 */
2.13 ! frystyk 22: #include "sysdep.h"
2.1 frystyk 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 {
2.13 ! frystyk 52: const HTStructuredClass * isa;
2.1 frystyk 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;
2.10 frystyk 126: if ((node = (HTDirNode *) HT_CALLOC(1, sizeof(HTDirNode))) == NULL)
127: HT_OUTOFMEM("HTDirNode_new");
2.1 frystyk 128: return node;
129: }
130:
131: /*
132: ** Free a sort key node
133: */
134: PRIVATE BOOL HTDirNode_free (HTDirNode *node)
135: {
136: if (node) {
2.10 frystyk 137: HT_FREE(node->fname);
138: HT_FREE(node->date);
139: HT_FREE(node->size);
140: HT_FREE(node->note);
141: HT_FREE(node);
2.1 frystyk 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)) {
2.9 frystyk 168: if (icon) {
169: HTMLPutImg(target, icon->icon_url,
170: HTIcon_alt_string(icon->icon_alt, YES), NULL);
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) {
193: HTMLPutImg(target, icon->icon_url,
194: HTIcon_alt_string(icon->icon_alt, YES), NULL);
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.9 frystyk 273: HTIconNode * icon = HTGetIcon(HT_IS_BLANK, NULL, NULL);
274: if (icon) {
275: HTMLPutImg(target, icon->icon_url,
276: HTIcon_alt_string(icon->icon_alt, YES), NULL);
277: PUTC(' ');
278: }
2.1 frystyk 279: }
280:
281: tp = dir->fnbuf;
282: LeftStr(&tp, "Name", dir->curfw);
283: LeftStr(&tp, " ", HT_DLEN_SPACE);
284: *tp = '\0';
285: PUTS(dir->fnbuf);
286:
287: tp = dir->lnbuf;
288: if (dir->show & HT_DS_DATE) {
2.2 frystyk 289: LeftStr(&tp, "Last Modified", HT_DLEN_DATE);
2.1 frystyk 290: LeftStr(&tp, " ", HT_DLEN_SPACE);
291: }
292: if (dir->show & HT_DS_SIZE) {
293: RightStr(&tp, "Size", HT_DLEN_SIZE);
294: LeftStr(&tp, " ", HT_DLEN_SPACE);
295: }
296: if (dir->show & HT_DS_DES) {
297: LeftStr(&tp, "Description", HT_DLEN_DATE);
298: LeftStr(&tp, " ", HT_DLEN_SPACE);
299: }
300: *tp = '\0';
301: PUTS(dir->lnbuf);
302: START(HTML_HR);
303: PUTC('\n');
304: return YES;
305: }
306: return NO;
307: }
308:
309: /* HTDir_setWidth
310: ** --------------
311: ** The module automatically ajusts the width of the directory listing as
312: ** a function of the file name. The width can flows dynamically between
313: ** an upper and a lower limit.
314: */
315: PUBLIC BOOL HTDir_setWidth (int minfile, int maxfile)
316: {
317: MinFileW = (minfile>=0) ? minfile : 0;
318: MaxFileW = (maxfile>minfile) ? maxfile : minfile+1;
319: return YES;
320: }
321:
322: /* HTDir_new
323: ** ---------
324: ** Creates a structured stream object and sets up the initial HTML stuff
325: ** Returns the dir object if OK, else NULL
326: */
327: PUBLIC HTDir * HTDir_new (HTRequest * request, HTDirShow show, HTDirKey key)
328: {
329: HTDir *dir;
330: char *title = NULL;
331: if (!request) return NULL;
332:
333: /* Create object */
2.10 frystyk 334: if ((dir = (HTDir *) HT_CALLOC(1, sizeof (HTDir))) == NULL ||
335: (dir->fnbuf = (char *) HT_MALLOC(MaxFileW+HT_DLEN_SPACE)) == NULL)
336: HT_OUTOFMEM("HTDir_new");
2.1 frystyk 337: dir->target = HTMLGenerator(request, NULL, WWW_HTML,
338: HTRequest_outputFormat(request),
339: HTRequest_outputStream(request));
2.7 frystyk 340: HTAnchor_setFormat(HTRequest_anchor(request), WWW_HTML);
2.1 frystyk 341: dir->request = request;
342: dir->show = show;
343: dir->key = key;
344: if (key==HT_DK_NONE)
345: dir->curfw = MaxFileW;
346: else {
347: dir->curfw = MinFileW;
348: dir->array = HTArray_new(256);
349: }
350:
351: /* Find the length of the fields */
352: {
353: int len = HT_DLEN_SPACE+1;
354: if (show & HT_DS_SIZE) len += (HT_DLEN_SIZE+HT_DLEN_SPACE);
355: if (show & HT_DS_DATE) len += (HT_DLEN_DATE+HT_DLEN_SPACE);
356: if (show & HT_DS_DES) len += HT_DLEN_DES;
2.10 frystyk 357: if ((dir->lnbuf = (char *) HT_MALLOC(len)) == NULL)
358: HT_OUTOFMEM("HTDir_new");
2.1 frystyk 359: }
360:
361: /* Find the title and the base URL */
362: {
363: char *addr = HTAnchor_address((HTAnchor *) HTRequest_anchor(request));
364: char *path = HTParse(addr, "", PARSE_PATH+PARSE_PUNCTUATION);
365: char *ptr;
366: if ((ptr = strchr(path, ';')) || (ptr = strchr(path, '?')))
367: *ptr = '\0';
368: StrAllocCopy(title, path);
369: HTUnEscape(title); /* Title */
370: if((ptr=strrchr(path, '/')) && (ptr<path+strlen(path)-1 || ptr==path)){
371: StrAllocCopy(dir->base, ++ptr);
372: StrAllocCat(dir->base, "/");
373: }
2.11 eric 374: if (PROT_TRACE) HTTrace("HTDir_new... base is `%s\'\n",
2.1 frystyk 375: dir->base ? dir->base : "");
2.10 frystyk 376: HT_FREE(addr);
377: HT_FREE(path);
2.1 frystyk 378: }
379:
380: /* Start the HTML stuff */
381: {
382: HTStructured *target = dir->target;
383: START(HTML_HTML);
384: START(HTML_HEAD);
385: START(HTML_TITLE);
386: PUTS("Current index is ");
387: PUTS(title);
388: END(HTML_TITLE);
389: END(HTML_HEAD);
390: START(HTML_BODY);
391: START(HTML_H1);
392: PUTS("Index of ");
393: PUTS(title);
394: END(HTML_H1);
395: }
2.10 frystyk 396: HT_FREE(title);
2.1 frystyk 397: return dir;
398: }
399:
400: /* HTDir_addElement
401: ** ---------------
402: ** This function accepts a directory line. "data" and "size", and
403: ** "description" can all be NULL
404: ** Returns YES if OK, else NO
405: */
406: PUBLIC BOOL HTDir_addElement (HTDir *dir, char *name, char *date, char *size,
407: HTFileMode mode)
408: {
409: HTDirNode *node = HTDirNode_new();
410: if (!dir || !name) return NO;
411: StrAllocCopy(node->fname, name); /* Mandatory */
412: if (dir->show & HT_DS_DATE && date) StrAllocCopy(node->date, date);
413: if (dir->show & HT_DS_SIZE && size) StrAllocCopy(node->size, size);
414: if (dir->show & HT_DS_DES) {
415: #if 0
416:
417: /* FIND DESCRIPTION */
418:
419: #endif
420: }
421: node->mode = mode;
422: if (dir->key == HT_DK_NONE) {
423: if (!dir->size++) HTDir_headLine(dir);
424: HTDirNode_print(dir, node);
425: HTDirNode_free(node);
426: } else {
427: int slen = strlen(name);
428: if (slen > dir->curfw)
429: dir->curfw = slen < MaxFileW ? slen : MaxFileW;
430: HTArray_addObject(dir->array, (void *) node);
431: }
432: return YES;
433: }
434:
2.13 ! frystyk 435: PRIVATE int DirSort (const void *a, const void *b)
2.1 frystyk 436: {
2.8 frystyk 437: #if 0
2.1 frystyk 438: HTDirNode *aa = *(HTDirNode **) a;
439: HTDirNode *bb = *(HTDirNode **) b;
440: return strcmp(aa->fname, bb->fname);
2.8 frystyk 441: #else
442: return strcmp((char *) (*((HTDirNode**)a))->fname,
443: (char *) (*((HTDirNode**)a))->fname);
2.1 frystyk 444: #endif
445: }
446:
2.13 ! frystyk 447: PRIVATE int DirCaseSort (const void *a, const void *b)
2.1 frystyk 448: {
2.8 frystyk 449: #if 0
2.1 frystyk 450: HTDirNode *aa = *(HTDirNode **) a;
451: HTDirNode *bb = *(HTDirNode **) b;
452: return strcasecomp(aa->fname, bb->fname);
2.8 frystyk 453: #else
454: return strcasecomp((char *) (*((HTDirNode**)a))->fname,
455: (char *) (*((HTDirNode**)a))->fname);
2.1 frystyk 456: #endif
457: }
458:
459: /* HTDir_free
460: ** ----------
461: ** If we are sorting then do the sorting and put out the list,
462: ** else just append the end of the list.
463: */
464: PUBLIC BOOL HTDir_free (HTDir * dir)
465: {
466: if (!dir) return NO;
467: if (dir->key != HT_DK_NONE) {
468: HTArray *array = dir->array;
469: void **data;
470: HTDirNode *node;
471: HTDir_headLine(dir);
472: HTArray_sort(array, (dir->key==HT_DK_CINS ? DirCaseSort : DirSort));
473: node = (HTDirNode *) HTArray_firstObject(array, data);
474: while (node) {
475: HTDirNode_print(dir, node);
476: HTDirNode_free(node);
477: node = (HTDirNode *) HTArray_nextObject(array, data);
478: }
479: dir->size = HTArray_size(array);
480: HTArray_delete(array);
481: }
482:
483: /* Put out the end of the HTML stuff */
484: {
485: HTStructured *target = dir->target;
486: START(HTML_HR);
487: if (!dir->size)
488: PUTS("Empty directory");
489: else if (dir->size == 1)
490: PUTS("1 File");
491: else {
492: char buffer[20];
493: sprintf(buffer, "%u files", dir->size);
494: PUTS(buffer);
495: }
496: END(HTML_PRE);
497: END(HTML_BODY);
498: END(HTML_HTML);
499: FREE_TARGET;
500: }
501:
2.10 frystyk 502: HT_FREE(dir->fnbuf);
503: HT_FREE(dir->lnbuf);
504: HT_FREE(dir->base);
505: HT_FREE(dir);
2.1 frystyk 506: return YES;
507: }
Webmaster