r348: fix linefeed problem
[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.11 2001/01/02 16:02:12 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
23 #include "hashtable.h"
24 #include <stack>
25
26 class POL {
27   
28   public:
29     
30     // codes for pol_usefile 
31     enum {
32       P_USE_STR = 1,            // use string as input source 
33       P_USE_FILE,                 // use file as input source 
34     };
35
36   
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
61   enum {
62     MAXTOK = 200,               // maximum length of a token 
63       MAXLINE = 1024,       // maximum line length
64       MAXIDENT = 20,
65       MAXSKIPWORD = 20,
66       MAXSKIPCHAR = 20,
67       MIN_INT = -2000000000,
68       MAX_INT =  2000000000,
69   };
70   
71   // token types 
72   enum {
73     TT_STRING = 1,  // string token 
74       TT_INT,         // integer token 
75       TT_REAL,                  // floating point token 
76       TT_ALPHA,                 // alphabetic token 
77       TT_ALPNUM,                // alphanumeric token 
78       TT_NUMALPHA,
79       TT_SPECLCHAR,
80       TT_EOF,         // end of file reached 
81       TT_ERROR,       // error in token, caused by call to wrong type of token reader 
82       TT_BLANK,        // white space token.  pol_tok() skips these 
83       TT_USERTOK,     // user defined token 
84   };
85
86   
87 private:
88   
89   // codes for pol_int and pol_float 
90   // if in reject catagory, get new number from terminal 
91   enum {
92     P_FLTINT = 1,        // get a real or integer number 
93       P_BFLTINT,     // get a real or integer number, clip against bounds 
94       P_CBFLTINT,        // get real or int, reject if outside bounds 
95       P_FLT,               // get a real number 
96       P_BFLT,        // get a real, clip against bounds 
97       P_CBFLT,       // get a floating, reject if outside bounds 
98       P_INT,         // get a integer number 
99       P_BINT,        // get a integer, clip against bounds 
100       P_CBINT,       // get a integer, reject if outside bounds 
101   };
102   
103 #define LETTER   'a'
104 #define DIGIT    '0'
105   
106   
107 //  typedef std::map<std::string,int> KeywordCodeList;
108   
109   struct token_st {
110     int ready;                          // TRUE if token is ready 
111   //  std::string tokstr;       // token string 
112     char tokstr[MAXTOK+1];
113     int type;                             // type of token 'TT_' 
114     int code;                             // holds code for user defined tokens 
115     double fnum;                        // real value of token 
116     int inum;                       // integer value of token 
117   };
118   typedef struct token_st TOKEN;
119   struct token_st token;                                // current token 
120    
121   // Tables words stored with install() & found with lookup() 
122   KeywordCodeHashTable skiptable;               // words to ignore and skip 
123   KeywordCodeHashTable cmdtable;                // pol parameter commands 
124   KeywordCodeHashTable usertable;               // user defined symbols 
125   
126   struct metachar {
127     char eoc;           /* end of command character */
128     char str;           /* string delimiter */
129     char com;           /* comment character */
130     char cmd;           /* pol parameter command character */
131     char prg;           /* program load character */
132     char con;           /* continuation across newline character */
133     char out;           /* character that delimits output to terminal */
134     char ter;           /* character indicates insertion of input from terminal */
135     char inb;           /* input from graphics device */
136   } meta;
137   
138   
139   char m_szSkipChars [MAXSKIPCHAR]; // characters to skip
140   bool m_bTrace;
141   bool m_bNewlineIsEOC;
142   
143   struct KeywordCodeList {
144     char *keyword;
145     int  code;
146   };
147   
148   static const struct KeywordCodeList cmdlist[];
149   static const unsigned int NUMCMD;
150   
151   // Internal codes for pol commands 
152   enum {
153     PC_EOC = 1,
154       PC_STR,
155       PC_COM,
156       PC_CMD,
157       PC_PRG,
158       PC_CON,   
159       PC_OUT,
160       PC_TER,
161       PC_INB,      
162       PC_NL_EOC,
163       PC_NL_NEOC,
164       PC_TRON,
165       PC_TROFF,
166       PC_FILE,
167       PC_DUMP,
168   };
169   
170   enum {
171     INPUT_STREAM = 1,
172     INPUT_FILE,
173     INPUT_STRING,
174   };
175
176   int m_iInputType;
177   std::istream* m_pInputStream;
178   FILE* m_pInputFile;
179   char* m_pszInputString;
180
181   enum {
182     MAXFILE = 8,
183   };
184   
185   int currentf;         /* pointer to current fp */
186   FILE *filep[MAXFILE];         /* == NULL for string input */
187   char *fname[MAXFILE];         /* pointer to filename */
188   
189   char inputline[MAXLINE];              /* current input line */
190   int lineptr;                  /* current position in inputline */
191   
192   std::stack<int> m_stackPushBackInput;
193
194   bool skipSingleToken (char term[]);
195   int tok (struct token_st *token);
196   void dumptok (struct token_st *token);
197   
198   
199   int getpol_tok (struct token_st *token);
200   int getcmd ();
201   int gettok (TOKEN *tok);
202   void getblank (char *s, int toksiz);
203   int getalpha (char *s, int toksiz);
204   void getquote (char *qs, int toksiz);
205   void getescape (char *s, int delim, int toksiz);
206   int getnumber (char str[], int strsize, double *fnum, int *inum);
207   void eatline ();
208   int type (int c);
209   int getch (FILE *fp);
210   
211 };
212
213 #endif
214