Back to index

lightning-sunbird  0.9+nobinonly
ec.h
Go to the documentation of this file.
00001 # ifndef EC_H
00002 # define EC_H
00003 
00004 # ifndef CORD_H
00005 #  include "cord.h"
00006 # endif
00007 
00008 /* Extensible cords are strings that may be destructively appended to.       */
00009 /* They allow fast construction of cords from characters that are     */
00010 /* being read from a stream.                                          */
00011 /*
00012  * A client might look like:
00013  *
00014  *     {
00015  *         CORD_ec x;
00016  *         CORD result;
00017  *         char c;
00018  *         FILE *f;
00019  *
00020  *         ...
00021  *         CORD_ec_init(x);
00022  *         while(...) {
00023  *            c = getc(f);
00024  *            ...
00025  *            CORD_ec_append(x, c);
00026  *         }
00027  *         result = CORD_balance(CORD_ec_to_cord(x));
00028  *
00029  * If a C string is desired as the final result, the call to CORD_balance
00030  * may be replaced by a call to CORD_to_char_star.
00031  */
00032 
00033 # ifndef CORD_BUFSZ
00034 #   define CORD_BUFSZ 128
00035 # endif
00036 
00037 typedef struct CORD_ec_struct {
00038     CORD ec_cord;
00039     char * ec_bufptr;
00040     char ec_buf[CORD_BUFSZ+1];
00041 } CORD_ec[1];
00042 
00043 /* This structure represents the concatenation of ec_cord with        */
00044 /* ec_buf[0 ... (ec_bufptr-ec_buf-1)]                                 */
00045 
00046 /* Flush the buffer part of the extended chord into ec_cord.   */
00047 /* Note that this is almost the only real function, and it is  */
00048 /* implemented in 6 lines in cordxtra.c                        */
00049 void CORD_ec_flush_buf(CORD_ec x);
00050       
00051 /* Convert an extensible cord to a cord. */
00052 # define CORD_ec_to_cord(x) (CORD_ec_flush_buf(x), (x)[0].ec_cord)
00053 
00054 /* Initialize an extensible cord. */
00055 # define CORD_ec_init(x) ((x)[0].ec_cord = 0, (x)[0].ec_bufptr = (x)[0].ec_buf)
00056 
00057 /* Append a character to an extensible cord.     */
00058 # define CORD_ec_append(x, c) \
00059     {  \
00060        if ((x)[0].ec_bufptr == (x)[0].ec_buf + CORD_BUFSZ) { \
00061               CORD_ec_flush_buf(x); \
00062        } \
00063        *((x)[0].ec_bufptr)++ = (c); \
00064     }
00065 
00066 /* Append a cord to an extensible cord.  Structure remains shared with       */
00067 /* original.                                                   */
00068 void CORD_ec_append_cord(CORD_ec x, CORD s);
00069 
00070 # endif /* EC_H */