More debianization
[erwise] / Cl / WWWLibrary / HTFormat.c
1 /*              Manage different file formats                   HTFormat.c
2 **              =============================
3 **
4 ** Bugs:
5 **      Not reentrant.
6 **
7 **      Assumes the incoming stream is ASCII, rather than a local file
8 **      format, and so ALWAYS converts from ASCII on non-ASCII machines.
9 **      Therefore, non-ASCII machines can't read local files.
10 */
11
12 #include "HTUtils.h"
13 #include "tcp.h"
14 #include "HTFormat.h"
15
16 #include "HTML.h"
17 #include "HText.h"
18 #include "HTStyle.h"
19
20 extern HTStyleSheet * styleSheet;
21
22 /*      File buffering
23 **      --------------
24 **
25 **      The input file is read using the macro which can read from
26 **      a socket or a file.
27 **      The input buffer size, if large will give greater efficiency and
28 **      release the server faster, and if small will save space on PCs etc.
29 */
30 #define INPUT_BUFFER_SIZE 4096          /* Tradeoff */
31 PRIVATE char input_buffer[INPUT_BUFFER_SIZE];
32 PRIVATE char * input_pointer;
33 PRIVATE char * input_limit;
34 PRIVATE int input_file_number;
35
36
37 /*      Set up the buffering
38 **
39 **      These routines are public because they are in fact needed by
40 **      many parsers, and on PCs and Macs we should not duplicate
41 **      the static buffer area.
42 */
43 PUBLIC void HTInitInput ARGS1 (int,file_number)
44 {
45     input_file_number = file_number;
46     input_pointer = input_limit = input_buffer;
47 }
48
49
50 PUBLIC char HTGetChararcter NOARGS
51 {
52     char ch;
53     do {
54         if (input_pointer >= input_limit) {
55 #ifdef ERWISE
56             int status = cl_read_data(
57                     input_file_number, input_buffer, INPUT_BUFFER_SIZE);
58 #else
59             int status = NETREAD(
60                     input_file_number, input_buffer, INPUT_BUFFER_SIZE);
61 #endif
62             if (status <= 0) {
63                 if (status == 0) return (char)EOF;
64                 if (TRACE) fprintf(stderr,
65                     "HTFormat: File read error %d\n", status);
66                 return (char)EOF; /* -1 is returned by UCX at end of HTTP link */
67             }
68             input_pointer = input_buffer;
69             input_limit = input_buffer + status;
70         }
71         ch = *input_pointer++;
72     } while (ch == (char) 13); /* Ignore ASCII carriage return */
73     
74     return FROMASCII(ch);
75 }
76
77
78 /*      Parse a file given format and file number
79 **      ------------
80 */
81 PUBLIC void HTParseFormat ARGS3(
82         HTFormat,format,
83         HTParentAnchor *,anchor,
84         int,file_number)
85 {
86 /*      Parse the file
87 */
88 #ifdef CURSES
89       long    bytecount = 0;
90 #endif
91     HTInitInput(file_number);
92
93     switch (format) {
94
95     case WWW_HTML:              /* Parse HTML */
96       {
97         HTML_begin(anchor);
98         SGML_begin(&HTML_dtd);
99         for(;;) {
100             char character;
101             character = HTGetChararcter();
102             if (character == (char)EOF) break;
103 #ifdef CURSES
104               if (++bytecount % 1024 == 0) prompt_count(bytecount / 1024);
105 #endif
106     
107             SGML_character(&HTML_dtd, character);           
108          }
109         SGML_end(&HTML_dtd);
110       }
111         break;
112
113     default :                   /* unknown format -- Parse plain text */
114       {
115         HText * text = HText_new(anchor);
116         HText_setStyle(text, HTStyleNamed(styleSheet, "Example"));
117         HText_beginAppend(text);
118         for(;;) {
119             char character;
120             character = HTGetChararcter();
121             if (character == (char)EOF) break;
122 #ifdef CURSES
123               if (++bytecount % 1024 == 0) prompt_count(bytecount / 1024);
124 #endif
125             HText_appendCharacter(text, character);           
126         }
127         HText_endAppend(text);
128       }
129         break;
130         
131     } /* end of switch (format) */
132     
133 }