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