00001 // 00002 // SfStream.h 00003 // 00004 // Colbert stream class definitions 00005 00006 // 00007 // Copyright 2000 by Kurt Konolige 00008 // 00009 // The author hereby grants to SRI permission to use this software. 00010 // The author also grants to SRI permission to distribute this software 00011 // to schools for non-commercial educational use only. 00012 // 00013 // The author hereby grants to other individuals or organizations 00014 // permission to use this software for non-commercial 00015 // educational use only. This software may not be distributed to others 00016 // except by SRI, under the conditions above. 00017 // 00018 // Other than these cases, no part of this software may be used or 00019 // distributed without written permission of the author. 00020 // 00021 // Neither the author nor SRI make any representations about the 00022 // suitability of this software for any purpose. It is provided 00023 // "as is" without express or implied warranty. 00024 // 00025 // Kurt Konolige 00026 // Senior Computer Scientist 00027 // SRI International 00028 // 333 Ravenswood Avenue 00029 // Menlo Park, CA 94025 00030 // E-mail: konolige@ai.sri.com 00031 00032 // 00033 // Classes and functions for manipulating Colbert input/output streams 00034 00035 #ifndef SFSTREAM_H 00036 #define SFSTREAM_H 00037 00038 00039 #undef SFEXPORT 00040 #if defined(MS_WINDOWS) && !defined(SF_STATIC) 00041 #ifdef MAKE_LIBRARY 00042 #define _declspec( dllexport ) 00043 #else 00044 #define _declspec( dllimport ) 00045 #endif 00046 #else 00047 #define SFEXPORT 00048 #endif 00049 00050 // struct stream_entry is used to hold {string, stream index} pairs 00051 00052 struct stream_entry 00053 { 00054 char * str; 00055 int index; 00056 }; 00057 00058 // 00059 // SfColbertStream is the underlying class for sending input to Colbert, 00060 // and displaying output. Should be subclassed by a window or console 00061 // for displaying and receiving characters. 00062 // 00063 // Colbert streams are kept in an array of max COLBERT_STREAM_MAX entries, 00064 // similar to Unix FILE streams. Streams are referred to by their entry 00065 // index. Writing via sfMessage is to a specified stream, or the default 00066 // stream, which can be specified. 00067 // 00068 // Each stream keeps its own history list of input. 00069 // 00070 // Stream input is put on a global circular buffer for consumption 00071 // by the Colbert parsing process. 00072 // 00073 // Haven't figured out issues of multithreading: AddInput and 00074 // GetNextColbertString should have protected access. 00075 // 00076 00077 // number of open Colbert streams 00078 #define COLBERT_STREAM_MAX 20 00079 // number of strings in global buffer, to be parsed 00080 #define COLBERT_PARSE_BUFFER_MAX 100 00081 // number of chars allowed per (continued) Colbert line 00082 #define COLBERT_STRING_MAX 1000 00083 00084 class SfColbertStream 00085 { 00086 public: 00087 SfColbertStream(int n); // create one with history list size n 00088 virtual ~SfColbertStream(); 00089 int StreamIndex() { return index; } // gets index of stream 00090 static SfColbertStream *GetStream(int i); // returns stream of index 00091 static SfColbertStream *GetDefaultStream(); // returns default stream 00092 static void DeleteStream(int i); // deletes stream with this index 00093 static int SetDefaultStream(int i); // sets the default stream to this index 00094 static char *GetNextColbertString(int *i); // for the parser 00095 void AddInput(const char *str); // copies str; if ends with "\", 00096 // concatenates with next line. 00097 virtual void AddOutput(const char *str); // saves output, this must be overridden 00098 virtual void AddOutputFlush(); // writes all output, must be overridden 00099 char *GetHistory(int n); // n is +1 or -1 00100 // printing on a stream 00101 static void sfFPrintf(int i, char *, ...); 00102 static void sfFPrint(int i, char *); 00103 static void sfPrintf(char *, ...); 00104 00105 private: 00106 // input strings, in a circular buffer 00107 char **history_list; 00108 int hfirst, hlast, hcur, hsize; 00109 char continuation[COLBERT_STRING_MAX]; // storage for a continued input line 00110 int csize; // where we are in the continuation buffer 00111 00112 // stream repository 00113 static SfColbertStream *streams[COLBERT_STREAM_MAX]; 00114 static int default_stream; // default to this one 00115 int index; // where this one is in streams buffer 00116 00117 // parse strings, in a circular buffer 00118 static struct stream_entry parse_list[COLBERT_PARSE_BUFFER_MAX]; 00119 static int pfirst, plast; 00120 }; 00121 00122 /* Window messages */ 00123 00124 char *sfGetColbertString(int *i); 00125 int sfSetDefaultOutput(int i); 00126 void sfMessage(char *s, ...); 00127 00128 #endif // SFSTREAM_H