pipe.h

Go to the documentation of this file.
00001 /*  
00002  *      Pipe.h - NT MCHAR Pipe wrapper for MAXScript
00003  *
00004  *          Copyright (c) John Wainwright 1996
00005  *
00006  */
00007 
00008 #pragma once
00009 
00010 #include "Strings.h"
00011 class FileStream;
00012 
00013 #define PIPE_BUF_SIZE   512
00014 
00015 // The undelivered data in the pipe is held in a linked list of
00016 // buffers, pointed into by read and write cursors.  
00017 // A side list is kept if writers supply info about sourcing files.
00018 // This is provided to readers like the compiler to add source
00019 // tags to generated code.  
00020 
00021 class src_info                      
00022 {
00023 public:
00024     src_info*    next;          // next marker
00025     MCHAR*       start;         // source start character in buffer chain
00026     Value*       file;          // source file name if any
00027     unsigned int offset;        // starting offset into source
00028     unsigned int linenumber;    // line number in the source
00029 
00030     //Constructor / Destructor
00031     src_info();
00032     ~src_info();
00033 };
00034 
00035 // Collectable::flags3 - bit 0 set if have attempted to load resource file
00036 class Pipe : public CharStream  
00037 {
00038 private:
00039     unsigned int read_source_offset; // running reader offset in source
00040     CRITICAL_SECTION pipe_update;   // for syncing pipe updates
00041 public:
00042     MCHAR*      write_buffer;       // pipe buffers & cursors
00043     MCHAR*      write_cursor;
00044     MCHAR*      read_buffer;
00045     MCHAR*      read_cursor;
00046     int         ungetch_count;
00047 
00048     HANDLE      pipe_event;         // for signalling data ready
00049     HANDLE      restart_event;      // used to restart a stopped pipe
00050     bool        waiting;            // reader is waiting for data
00051     bool        stopped;            // pipe reading is blocked
00052 
00053     FileStream* log;                // log stream if non-NULL
00054 
00055     src_info*   markers;            // marker list...
00056     src_info*   marker_tail;        
00057     MCHAR*      next_source_start;  // upcoming marker starting character
00058     Value*      write_source_file;  // current write source file, used to determine source change
00059     unsigned int write_source_offset;// running writer offset
00060 
00061     MCHAR       lastCharacterOfLastBuffer;  // used when looking back one character from current read cursor,
00062                                             // and read cursor is at the beginning of the read buffer
00063 
00064                 Pipe();
00065                 ~Pipe();
00066                 
00067 #   define      is_pipe(v) ((DbgVerify(!is_sourcepositionwrapper(v)), (v))->tag == INTERNAL_PIPE_TAG)
00068     void        collect();
00069     void        gc_trace();
00070 
00071     MCHAR       get_char();
00072     void        unget_char(MCHAR c);
00073     MCHAR       peek_char();
00074     int         at_eos();
00075     unsigned int pos(); // Gets the current position in the stream
00076     unsigned int line(); // Gets the current line number
00077     void        rewind();
00078     void        flush_to_eol();
00079     void        flush_to_eobuf();
00080 
00081     void        put_char(const MCHAR c, Value* source_file = NULL, unsigned int offset = 0, unsigned int line = 1);
00082     void        put_str(const MCHAR* str, Value* source_file = NULL, unsigned int  offset = 0, unsigned int line = 1);
00083     void        put_buf(const MCHAR* str, UINT count, Value* source_file = NULL, unsigned int offset = 0, unsigned int line = 1);
00084     void        new_write_buffer();
00085     void        check_write_source_change(Value* file, unsigned int offset, unsigned int line, int new_len);
00086     void        read_source_change();
00087     void        clear_source();
00088     void        stop();
00089     void        go();
00090 
00091     const MCHAR*        puts(const MCHAR* str);
00092     int         printf(const MCHAR *format, ...);
00093     void        flag_eos(); // sets the end of script marker in the pipe's stream
00094 
00095     void        log_to(FileStream* log);
00096     void        close_log();
00097     CharStream* get_log() { return log; }
00098 };
00099