HTDescpt.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:5k
源码类别:

浏览器

开发平台:

Unix_Linux

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