1 /* HTDescpt.c
2 ** FILE DESCRIPTIONS
3 **
4 ** (c) COPYRIGHT MIT 1995.
5 ** Please first read the full copyright statement in the file COPYRIGH.
6 ** @(#) $Id$
7 **
8 ** Authors:
9 ** AL Ari Luotonen <luotonen@dxcern.cern.ch>
10 **
11 ** History:
12 ** 30 Mar 94 AL Written from scratch.
13 **
14 **
15 */
16
17 /* Library include files */
18 #include "wwwsys.h"
19 #include "HTUtils.h"
20 #include "HTString.h"
21 #include "HTFormat.h"
22 #include "HTList.h"
23
24 #define MAX_LINE_LEN 256
25
26 PRIVATE char * HTDescriptionFile = ".www_descript";
27 PRIVATE BOOL HTPeekTitles = YES;
28
29 /*
30 * Get the descriptions for files in the given directory.
31 * The return value is then later passed as an argument
32 * to HTGetDescription() which returns a description
33 * string for a single file.
34 */
HTReadDescriptions(char * dirname)35 PUBLIC HTList * HTReadDescriptions (char * dirname)
36 {
37 char * name = NULL;
38 FILE * fp = NULL;
39 HTList * list = NULL;
40 char buf[MAX_LINE_LEN + 1];
41
42 if (!dirname) return NULL;
43
44 if ((name = (char *) HT_MALLOC(strlen(dirname) + strlen(HTDescriptionFile) + 2)) == NULL)
45 HT_OUTOFMEM("HTReadDescriptions");
46
47 sprintf(name, "%s/%s", dirname, HTDescriptionFile);
48 fp = fopen(name, "r");
49 if (!fp) {
50 HTTRACE(PROT_TRACE, "DirBrowse... No description file %s\n" _ name);
51 HT_FREE(name);
52 return NULL;
53 } else {
54 HTTRACE(PROT_TRACE, "DirBrowse... Description file found %s\n" _ name);
55 }
56
57 list = HTList_new();
58
59 while (fgets(buf, MAX_LINE_LEN, fp)) {
60 char * s = buf;
61 char * t = NULL;
62 char * d = NULL;
63
64 while (*s && isspace((int) *s)) s++; /* Skip initial whitespace */
65 if (*s!='d' && *s!='D') continue; /* Junk non-description lines*/
66
67 t = s+1;
68 while (*t && !isspace((int) *t)) t++; /* Find the end of the keyword */
69 while (*t && isspace((int) *t)) t++; /* Find the beginning of template */
70
71 if (*t) {
72 d = t+1;
73 while (*d && !isspace((int) *d)) d++; /* Find end of template */
74 if (*d) {
75 *d++ = 0; /* Terminate template */
76 while (*d && isspace((int) *d)) d++; /* Find start of description */
77 if (*d) {
78 char * p = d;
79 while (*p && *p!='\r' && *p!='\n') p++;
80 *p = 0; /* Terminate description */
81 }
82 }
83 }
84 if (t && d && *t && *d) {
85 char * stuff;
86 if ((stuff = (char *) HT_MALLOC(strlen(t) + strlen(d) + 2)) == NULL)
87 HT_OUTOFMEM("HTDirReadDescriptions");
88 sprintf(stuff, "%s %s", t, d);
89 HTList_addObject(list, (void*)stuff);
90 HTTRACE(PROT_TRACE, "Description. %s\n" _ stuff);
91 }
92 }
93 fclose(fp);
94 HT_FREE(name);
95 return list;
96 }
97
98
HTFreeDescriptions(HTList * descriptions)99 PUBLIC void HTFreeDescriptions (HTList * descriptions)
100 {
101 HTList * cur = descriptions;
102 char * str;
103
104 if (descriptions) {
105 while ((str = (char*)HTList_nextObject(cur)))
106 HT_FREE(str);
107 HTList_delete(descriptions);
108 }
109 }
110
111
HTPeekTitle(char * dirname,char * filename)112 PRIVATE char * HTPeekTitle (char * dirname,
113 char * filename)
114 {
115 #define PEEK_BUF_SIZE 200
116 char * name;
117 FILE * fp;
118 char buf[PEEK_BUF_SIZE + 1];
119 int status;
120 char * cur;
121 char * end;
122 static char * ret = NULL;
123 char * p;
124 BOOL space = YES;
125
126 HT_FREE(ret); /* from previous call */
127
128 HTTRACE(PROT_TRACE, "HTPeekTitle. called, dirname=%s filename=%s\n" _
129 dirname ? dirname : "-null-" _
130 filename ? filename : "-null-");
131
132 if (!dirname || !filename) return NULL;
133
134 if ((name = (char *) HT_MALLOC(strlen(dirname) + strlen(filename) + 2)) == NULL)
135 HT_OUTOFMEM("HTPeekTitle");
136 sprintf(name, "%s/%s", dirname, filename);
137
138 fp = fopen(name, "r");
139 if (!fp) {
140 HTTRACE(PROT_TRACE, "HTPeekTitle. fopen failed\n");
141 goto cleanup;
142 }
143
144 status = fread(buf, 1, PEEK_BUF_SIZE, fp);
145 fclose(fp);
146 if (status <= 0) goto cleanup;
147 buf[status] = 0;
148
149 cur = buf;
150 while ((cur = strchr(cur,'<'))) {
151 if (!strncasecomp(cur+1,"TITLE>",6)) {
152 cur += 7;
153 end = strchr(cur,'<');
154 while (end && strncasecomp(end+1, "/TITLE>", 7))
155 end = strchr(end+1, '<');
156 if (end) *end = 0;
157 if ((p = ret = (char*) HT_MALLOC(strlen(cur) + 1)) == NULL)
158 HT_OUTOFMEM("HTPeekTitle");
159 while (*cur) {
160 if (isspace((int) *cur)) {
161 if (!space) {
162 space = YES;
163 *p++ = ' ';
164 }
165 }
166 else {
167 if (space) space = NO;
168 *p++ = *cur;
169 }
170 cur++;
171 }
172 *p = 0;
173 goto cleanup;
174 }
175 cur++;
176 }
177
178 cleanup:
179 HTTRACE(PROT_TRACE, "HTPeekTitle. returning %c%s%c\n" _
180 ret ? '"' : '-' _ ret ? ret : "null" _ ret ? '"' : '-');
181 HT_FREE(name);
182 return ret;
183 }
184
185
186 /*
187 * Returns a description string (that must not be HT_FREEd!)
188 * for a file with name name in directory dirname.
189 * Description file contents is in descriptions list.
190 */
HTGetDescription(HTList * descriptions,char * dirname,char * filename,HTFormat format)191 PUBLIC char * HTGetDescription (HTList * descriptions,
192 char * dirname,
193 char * filename,
194 HTFormat format)
195 {
196 HTList * cur = descriptions;
197 char * t;
198
199 if (!dirname || !filename) return NULL;
200 /*
201 * descriptions may well be NULL in which case we may still
202 * want to peek the titles.
203 */
204
205 while ((t = (char*)HTList_nextObject(cur))) {
206 char * d = strchr(t,' ');
207 if (!d) continue;
208 *d = 0;
209 #if 0
210 if (HTAA_templateMatch(t,filename)) {
211 #else
212 if (HTStrMatch(t, filename)) {
213 #endif
214 *d = ' ';
215 return d+1;
216 }
217 *d = ' ';
218 }
219
220 if (HTPeekTitles && format == WWW_HTML)
221 return HTPeekTitle(dirname, filename);
222 else
223 return NULL;
224 }
225
226