r328: *** empty log message ***
[ctsim.git] / include / pol.h
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: pol.h,v 1.10 2000/12/29 20:11:42 kevin Exp $
6 **
7 **  This program is free software; you can redistribute it and/or modify
8 **  it under the terms of the GNU General Public License (version 2) as
9 **  published by the Free Software Foundation.
10 **
11 **  This program is distributed in the hope that it will be useful,
12 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 **  GNU General Public License for more details.
15 **
16 **  You should have received a copy of the GNU General Public License
17 **  along with this program; if not, write to the Free Software
18 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 ******************************************************************************/
20 #ifndef __H_POL
21 #define __H_POL
22 \r
23 #include "hashtable.h"\r
24 #include <stack>\r
25
26 class POL {
27   
28   public:
29     
30     // codes for pol_usefile \r
31     enum {\r
32       P_USE_STR = 1,            // use string as input source \r
33       P_USE_FILE,                 // use file as input source \r
34     };\r
35 \r
36   \r
37     POL();
38     ~POL();
39     
40     void init ();
41     void addSkipWord (const char* const w);
42     void addSkipChar (int c);
43     void addKeyword (const char* const str, int code);
44     bool readWord (char *search, int nlet);
45     bool readUserToken (char *str, int *code);
46     bool readString (char *str);
47     bool readInteger (int *n, int typecode, bool boundcode, int bb1, int bb2);
48     bool readFloat (double *n, double typecode, bool boundcode, double bb1, double bb2);
49     bool skipTokens ();
50     void reader ();
51     bool readText (char *str, int lim);
52     void usefile (int source, char *fn);
53     void closefile ();
54     int lookchar ();
55     int inchar ();
56     void ungetch (int c);
57     int get_inputline (FILE *fp);
58     void set_inputline (const char* const line);
59     
60 \r
61   enum {\r
62     MAXTOK = 200,               // maximum length of a token \r
63       MAXLINE = 1024,       // maximum line length\r
64       MAXIDENT = 20,\r
65       MAXSKIPWORD = 20,\r
66       MAXSKIPCHAR = 20,\r
67       MIN_INT = -2000000000,\r
68       MAX_INT =  2000000000,\r
69   };\r
70   \r
71   // token types \r
72   enum {\r
73     TT_STRING = 1,  // string token \r
74       TT_INT,         // integer token \r
75       TT_REAL,                  // floating point token \r
76       TT_ALPHA,                 // alphabetic token \r
77       TT_ALPNUM,                // alphanumeric token \r
78       TT_NUMALPHA,\r
79       TT_SPECLCHAR,\r
80       TT_EOF,         // end of file reached \r
81       TT_ERROR,       // error in token, caused by call to wrong type of token reader \r
82       TT_BLANK,        // white space token.  pol_tok() skips these \r
83       TT_USERTOK,     // user defined token \r
84   };\r
85 \r
86   \r
87 private:\r
88   \r
89   // codes for pol_int and pol_float \r
90   // if in reject catagory, get new number from terminal \r
91   enum {\r
92     P_FLTINT = 1,        // get a real or integer number \r
93       P_BFLTINT,     // get a real or integer number, clip against bounds \r
94       P_CBFLTINT,        // get real or int, reject if outside bounds \r
95       P_FLT,               // get a real number \r
96       P_BFLT,        // get a real, clip against bounds \r
97       P_CBFLT,       // get a floating, reject if outside bounds \r
98       P_INT,         // get a integer number \r
99       P_BINT,        // get a integer, clip against bounds \r
100       P_CBINT,       // get a integer, reject if outside bounds \r
101   };\r
102   \r
103 #define LETTER   'a'\r
104 #define DIGIT    '0'\r
105   \r
106   \r
107 //  typedef std::map<std::string,int> KeywordCodeList;\r
108   \r
109   struct token_st {\r
110     int ready;                          // TRUE if token is ready \r
111   //  std::string tokstr;       // token string \r
112     char tokstr[MAXTOK+1];\r
113     int type;                             // type of token 'TT_' \r
114     int code;                             // holds code for user defined tokens \r
115     double fnum;                        // real value of token \r
116     int inum;                       // integer value of token \r
117   };\r
118   typedef struct token_st TOKEN;\r
119   struct token_st token;                                // current token \r
120    \r
121   // Tables words stored with install() & found with lookup() \r
122   KeywordCodeHashTable skiptable;               // words to ignore and skip \r
123   KeywordCodeHashTable cmdtable;                // pol parameter commands \r
124   KeywordCodeHashTable usertable;               // user defined symbols \r
125   \r
126   struct metachar {\r
127     char eoc;           /* end of command character */\r
128     char str;           /* string delimiter */\r
129     char com;           /* comment character */\r
130     char cmd;           /* pol parameter command character */\r
131     char prg;           /* program load character */\r
132     char con;           /* continuation across newline character */\r
133     char out;           /* character that delimits output to terminal */\r
134     char ter;           /* character indicates insertion of input from terminal */\r
135     char inb;           /* input from graphics device */\r
136   } meta;\r
137   \r
138   \r
139   char m_szSkipChars [MAXSKIPCHAR]; // characters to skip\r
140   bool m_bTrace;\r
141   bool m_bNewlineIsEOC;\r
142   \r
143   struct KeywordCodeList {\r
144     char *keyword;\r
145     int  code;\r
146   };\r
147   \r
148   static const struct KeywordCodeList cmdlist[];\r
149   static const unsigned int NUMCMD;\r
150   \r
151   // Internal codes for pol commands \r
152   enum {\r
153     PC_EOC = 1,\r
154       PC_STR,\r
155       PC_COM,\r
156       PC_CMD,\r
157       PC_PRG,\r
158       PC_CON,   \r
159       PC_OUT,\r
160       PC_TER,\r
161       PC_INB,      \r
162       PC_NL_EOC,\r
163       PC_NL_NEOC,\r
164       PC_TRON,\r
165       PC_TROFF,\r
166       PC_FILE,\r
167       PC_DUMP,\r
168   };\r
169   \r
170   enum {\r
171     INPUT_STREAM = 1,\r
172     INPUT_FILE,\r
173     INPUT_STRING,\r
174   };\r
175 \r
176   int m_iInputType;\r
177   std::istream* m_pInputStream;\r
178   FILE* m_pInputFile;\r
179   char* m_pszInputString;\r
180 \r
181   enum {\r
182     MAXFILE = 8,\r
183   };\r
184   \r
185   int currentf;         /* pointer to current fp */\r
186   FILE *filep[MAXFILE];         /* == NULL for string input */\r
187   char *fname[MAXFILE];         /* pointer to filename */\r
188   \r
189   char inputline[MAXLINE];              /* current input line */\r
190   int lineptr;                  /* current position in inputline */\r
191   \r
192   std::stack<int> m_stackPushBackInput;\r
193 \r
194   bool skipSingleToken (char term[]);\r
195   int tok (struct token_st *token);\r
196   void dumptok (struct token_st *token);\r
197   \r
198   \r
199   int getpol_tok (struct token_st *token);\r
200   int getcmd ();\r
201   int gettok (TOKEN *tok);\r
202   void getblank (char *s, int toksiz);\r
203   int getalpha (char *s, int toksiz);\r
204   void getquote (char *qs, int toksiz);\r
205   void getescape (char *s, int delim, int toksiz);\r
206   int getnumber (char str[], int strsize, double *fnum, int *inum);\r
207   void eatline ();\r
208   int type (int c);\r
209   int getch (FILE *fp);\r
210   \r
211 };
212
213 #endif
214