Annotation of libwww/Library/src/HTFormat.c, revision 1.1.1.1

1.1       timbl       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: 
                     23: PUBLIC BOOL HTOutputSource = NO;       /* Flag: shortcut parser to stdout */
                     24: 
                     25: /*     File buffering
                     26: **     --------------
                     27: **
                     28: **     The input file is read using the macro which can read from
                     29: **     a socket or a file.
                     30: **     The input buffer size, if large will give greater efficiency and
                     31: **     release the server faster, and if small will save space on PCs etc.
                     32: */
                     33: #define INPUT_BUFFER_SIZE 4096         /* Tradeoff */
                     34: PRIVATE char input_buffer[INPUT_BUFFER_SIZE];
                     35: PRIVATE char * input_pointer;
                     36: PRIVATE char * input_limit;
                     37: PRIVATE int input_file_number;
                     38: 
                     39: 
                     40: /*     Set up the buffering
                     41: **
                     42: **     These routines are public because they are in fact needed by
                     43: **     many parsers, and on PCs and Macs we should not duplicate
                     44: **     the static buffer area.
                     45: */
                     46: PUBLIC void HTInitInput ARGS1 (int,file_number)
                     47: {
                     48:     input_file_number = file_number;
                     49:     input_pointer = input_limit = input_buffer;
                     50: }
                     51: 
                     52: 
                     53: PUBLIC char HTGetChararcter NOARGS
                     54: {
                     55:     char ch;
                     56:     do {
                     57:        if (input_pointer >= input_limit) {
                     58:            int status = NETREAD(
                     59:                    input_file_number, input_buffer, INPUT_BUFFER_SIZE);
                     60:            if (status <= 0) {
                     61:                if (status == 0) return (char)EOF;
                     62:                if (TRACE) fprintf(stderr,
                     63:                    "HTFormat: File read error %d\n", status);
                     64:                return (char)EOF; /* -1 is returned by UCX at end of HTTP link */
                     65:            }
                     66:            input_pointer = input_buffer;
                     67:            input_limit = input_buffer + status;
                     68:        }
                     69:        ch = *input_pointer++;
                     70:     } while (ch == (char) 13); /* Ignore ASCII carriage return */
                     71:     
                     72:     return FROMASCII(ch);
                     73: }
                     74: 
                     75: /*     Stream the data to an ouput file as binary
                     76: */
                     77: PUBLIC int HTOutputBinary ARGS2( int,          input,
                     78:                                  FILE *,       output)
                     79: {
                     80:     do {
                     81:            int status = NETREAD(
                     82:                    input, input_buffer, INPUT_BUFFER_SIZE);
                     83:            if (status <= 0) {
                     84:                if (status == 0) return 0;
                     85:                if (TRACE) fprintf(stderr,
                     86:                    "HTFormat: File read error %d\n", status);
                     87:                return 2;                       /* Error */
                     88:            }
                     89:            fwrite(input_buffer, sizeof(char), status, output);
                     90:     } while (YES);
                     91: }
                     92: 
                     93: 
                     94: 
                     95: /*     Parse a file given format and file number
                     96: **     ------------
                     97: **
                     98: **   This routine is responsible for ceating and presenting any
                     99: **   graphic (or other) objects described by the file.
                    100: */
                    101: PUBLIC void HTParseFormat ARGS3(
                    102:        HTFormat,format,
                    103:        HTParentAnchor *,anchor,
                    104:        int,file_number)
                    105: {
                    106: 
                    107: /*     Parse the file
                    108: */
                    109:     if (HTOutputSource) {      /* Shortcut dump binary from the net */
                    110:        exit(HTOutputBinary(file_number, stdout));  /* @@@cheat */
                    111:     }
                    112:     
                    113: #ifdef CURSES
                    114:      long    bytecount = 0;
                    115: #endif
                    116:     HTInitInput(file_number);
                    117: 
                    118:     switch (format) {
                    119: 
                    120:     case WWW_HTML:             /* Parse HTML */
                    121:       {                                /* Call SGML directly for speed */
                    122:        HTML_id html = HTML_new(anchor);
                    123:         HTSGMLContext context = HTML_SGMLContext(html);
                    124:        for(;;) {
                    125:            char character;
                    126:            character = HTGetChararcter();
                    127:            if (character == (char)EOF) break;
                    128: #ifdef CURSES
                    129:               if (++bytecount % 1024 == 0) prompt_count(bytecount / 1024);
                    130: #endif
                    131:     
                    132:            SGML_character(context, character);           
                    133:          }
                    134:        HTML_free(html);
                    135:       }
                    136:        break;
                    137: 
                    138:     default :                  /* unknown format -- Parse plain text */
                    139:       {
                    140:         HText * text = HText_new(anchor);
                    141:        HText_setStyle(text, HTStyleNamed(styleSheet, "Example"));
                    142:        HText_beginAppend(text);
                    143:        for(;;) {
                    144:            char character;
                    145:            character = HTGetChararcter();
                    146:            if (character == (char)EOF) break;
                    147: #ifdef CURSES
                    148:               if (++bytecount % 1024 == 0) prompt_count(bytecount / 1024);
                    149: #endif
                    150:            HText_appendCharacter(text, character);           
                    151:        }
                    152:         HText_endAppend(text);
                    153:       }
                    154:        break;
                    155:        
                    156:     } /* end of switch (format) */
                    157:     
                    158: }

Webmaster