Xbase Class Library  2.0.0
exp.h
Go to the documentation of this file.
1 /* $Id: exp.h,v 1.6 2000/11/10 19:04:17 dbryson Exp $
2 
3  Xbase project source code
4 
5  This file contains a header file for the EXP object, which is
6  used for expression processing.
7 
8  Copyright (C) 1997 Startech, Gary A. Kunkel
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Lesser General Public
12  License as published by the Free Software Foundation; either
13  version 2.1 of the License, or (at your option) any later version.
14 
15  This library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public
21  License along with this library; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 
24  Contact:
25 
26  Mail:
27 
28  Technology Associates, Inc.
29  XBase Project
30  1455 Deming Way #11
31  Sparks, NV 89434
32  USA
33 
34  Email:
35 
36  xbase@techass.com
37 
38  See our website at:
39 
40  xdb.sourceforge.net
41 
42 
43  V 1.0 10/10/97 - Initial release of software
44  V 1.5 1/2/97 - Added memo field support
45  V 1.6a 4/1/98 - Added expression support
46  V 1.6b 4/8/98 - Numeric index keys
47  V 1.7.1 5/25/98 - Expression support enhancements
48 */
49 
50 #ifndef __XB_EXP_H__
51 #define __XB_EXP_H__
52 
53 #ifdef __GNUG__
54 #pragma interface
55 #endif
56 
57 #include <xbase/xbase.h>
58 
59 #ifdef XB_EXPRESSIONS /* compile if expression logic on */
60 
61 #define XB_EXPRESSION xbExpNode
62 
63 #include <xbase/xtypes.h>
64 #include <xbase/xstack.h>
65 
69 #undef ABS
70 #undef MIN
71 #undef MAX
72 
73 class XBDLLEXPORT xbDbf;
74 
76 
79 struct XBDLLEXPORT xbFuncDtl {
80  const char * FuncName; /* function name */
81  xbShort ParmCnt; /* no of parms it needs */
82  char ReturnType; /* return type of function */
83  void (*ExpFuncPtr)(); /* pointer to function routine */
84 };
85 
87 
90 class XBDLLEXPORT xbExpNode {
91 public:
92  char * NodeText; /* expression text */
93  char Type; /* same as TokenType below */
94  xbShort Len; /* length of expression text */
95  xbShort InTree; /* this node in the tree? 1=yes */
96  xbExpNode * Node; /* pointer to parent */
97  xbExpNode * Sibling1; /* pointer to sibling 1 */
98  xbExpNode * Sibling2; /* pointer to sibling 2 */
99  xbExpNode * Sibling3; /* pointer to sibling 3 */
100 
101  xbShort DataLen; /* length of data in result buffer */
102  xbShort ResultLen; /* length of result buffer */
103 // char * Result; /* result buffer - ptr to result */
104  xbString StringResult;
105  xbDouble DoubResult; /* Numeric Result */
106  xbShort IntResult; /* logical result */
107 
108  xbDbf * dbf; /* pointer to datafile */
109  xbShort FieldNo; /* field no if DBF field */
110  char ExpressionType; /* used in head node C,N,L or D */
111 
112 
113  public:
114  xbExpNode() :
115  NodeText(0),
116  Type(0),
117  Len(0),
118  InTree(0),
119  Node(0),
120  Sibling1(0),
121  Sibling2(0),
122  Sibling3(0),
123  DataLen(0),
124  ResultLen(0),
125  DoubResult(0),
126  IntResult(0),
127  dbf(0),
128  FieldNo(-1),
129  ExpressionType(0)
130  {}
131  ~xbExpNode(){
132  if( Sibling1 ) delete Sibling1;
133  if( Sibling2 ) delete Sibling2;
134  if( Sibling3 ) delete Sibling3;
135  }
136 };
137 
139 
142 /* Expression handler */
143 
144 class XBDLLEXPORT xbExpn : public xbStack, public xbDate {
145 public:
146  xbShort ProcessExpression( xbExpNode *, xbShort );
147  xbExpNode * GetTree( void ) { return Tree; }
148  void SetTreeToNull( void ) { Tree = NULL; }
149  xbExpNode * GetFirstTreeNode( xbExpNode * );
150 
151  xbExpn( void );
152  xbShort GetNextToken( const char *s, xbShort MaxLen);
153 
154  /* expression methods */
155  xbDouble ABS( xbDouble );
156  xbLong ASC( const char * );
157  xbLong AT( const char *, const char * );
158  char * CDOW( const char * );
159  char * CHR( xbLong );
160  char * CMONTH( const char * );
161  char * DATE();
162  xbLong DAY( const char * );
163  xbLong DESCEND( const char * );
164  xbLong DOW( const char * );
165  char * DTOC( const char * );
166  char * DTOS( const char * );
167  xbDouble EXP( xbDouble );
168  xbLong INT( xbDouble );
169  xbLong ISALPHA( const char * );
170  xbLong ISLOWER( const char * );
171  xbLong ISUPPER( const char * );
172  char * LEFT( const char *, xbShort );
173  xbLong LEN( const char * );
174  xbDouble LOG( xbDouble );
175  char * LOWER( const char * );
176  char * LTRIM( const char * );
177  xbDouble MAX( xbDouble, xbDouble );
178  xbLong MONTH( const char * ); /* MONTH() */
179  xbDouble MIN( xbDouble, xbDouble );
180  char * RECNO( xbULong );
181  xbLong RECNO( xbDbf * );
182  char * REPLICATE( const char *, xbShort );
183  char * RIGHT( const char *, xbShort );
184  char * RTRIM( const char * );
185  char * SPACE( xbShort );
186  xbDouble SQRT( xbDouble );
187  char * STR( const char * );
188  char * STR( const char *, xbShort );
189  char * STR( const char *, xbShort, xbShort );
190  char * STR( xbDouble );
191  char * STR( xbDouble, xbShort );
192  char * STR(xbDouble, xbUShort length, xbShort numDecimals );
193  char * STRZERO( const char * );
194  char * STRZERO( const char *, xbShort );
195  char * STRZERO( const char *, xbShort, xbShort );
196  char * STRZERO( xbDouble );
197  char * STRZERO( xbDouble, xbShort );
198  char * STRZERO( xbDouble, xbShort, xbShort );
199  char * SUBSTR( const char *, xbShort, xbShort );
200  char * TRIM( const char * );
201  char * UPPER( const char * );
202  xbLong VAL( const char * );
203  xbLong YEAR( const char * );
205 
208  void SetDefaultDateFormat(const xbString f){ DefaultDateFormat = f; }
209 
210  xbString GetDefaultDateFormat() const { return DefaultDateFormat; }
211  xbShort ProcessExpression( const char *exp, xbDbf * d );
212  xbShort ParseExpression( const char *exp, xbDbf * d );
213  XB_EXPRESSION * GetExpressionHandle();
214  char GetExpressionResultType(XB_EXPRESSION * );
215  char * GetCharResult();
216  xbString & GetStringResult();
217  xbDouble GetDoubleResult();
218  xbLong GetIntResult();
219  xbShort ProcessExpression( xbExpNode * );
220  xbShort BuildExpressionTree( const char * Expression, xbShort MaxTokenLen,
221  xbDbf *d );
222 
223 #ifdef XBASE_DEBUG
224  void DumpExpressionTree( xbExpNode * );
225  void DumpExpNode( xbExpNode * );
226 #endif
227 
228 protected:
229  xbFuncDtl *XbaseFuncList; /* pointer to list of Xbase functions */
230 // xbExpNode *NextFreeExpNode; /* pointer to chain of free nodes */
231  xbExpNode *Tree;
232  xbShort LogicalType; /* set to 1 for logical type nodes */
233 
234  char TokenType; /* E - Expression, not in simplest form */
235  /* C - Constant */
236  /* N - Numeric Constant */
237  /* O - Operator */
238  /* F - Function */
239  /* D - Database Field */
240  /* s - character string result */
241  /* l - logical or short int result */
242  /* d - double result */
243 
244  char PreviousType; /* used to see if "-" follows operator */
245  char * Op1; /* pointer to operand 1 */
246  char * Op2; /* pointer to operand 2 */
247  xbDouble Opd1; /* double result 1 */
248  xbDouble Opd2; /* double result 2 */
249  xbShort OpLen1; /* length of memory allocated to operand 1 */
250  xbShort OpLen2; /* length of memory allocated to operand 2 */
251  xbShort OpDataLen1; /* length of data in op1 */
252  xbShort OpDataLen2; /* length of data in op2 */
253 
254  char OpType1; /* type of operand 1 */
255  char OpType2; /* type of operand 2 */
256  xbShort TokenLen; /* length of token */
257 
258  static xbString DefaultDateFormat; /*default date format for DTOC func*/
259 
260  enum { WorkBufMaxLen = 200 };
261  char WorkBuf[WorkBufMaxLen+1];
262 
263  xbShort IsWhiteSpace( char );
264  char IsSeparator( char );
265  xbExpNode * LoadExpNode( const char * ENodeText, const char EType,
266  const xbShort ELen, const xbShort BufLen );
267  xbShort OperatorWeight( const char *Oper, xbShort len );
268  xbShort ReduceComplexExpression( const char * NextToken, xbShort Len,
269  xbExpNode * cn, xbDbf *d );
270  xbShort GetFunctionTokenLen( const char *s );
271  xbShort ReduceFunction( const char *NextToken, xbExpNode *cn, xbDbf *d );
272  xbExpNode * GetNextTreeNode( xbExpNode * );
273  xbShort ProcessOperator( xbShort );
274  xbShort ProcessFunction( char * );
275  xbShort ValidOperation( char *, char, char );
276  char GetOperandType( xbExpNode * );
277  xbShort AlphaOperation( char * );
278  xbShort NumericOperation( char * );
279  xbExpNode * GetExpNode( xbShort );
280  xbShort GetFuncInfo( const char *Function, xbShort Option );
281  xbDouble GetDoub( xbExpNode * );
282  xbLong GetInt( xbExpNode * );
283 };
284 #endif // XB_EXPRESSIONS
285 #endif // __XB_EXP_H__
double xbDouble
xbDouble type
Definition: xtypes.h:76
xbDate class
Definition: xdate.h:78
#define XBDLLEXPORT
Definition: xbase.h:101
xbString class
Definition: xbstring.h:69
unsigned long int xbULong
xbULong type
Definition: xtypes.h:57
#define xbLong
Definition: xtypes.h:67
short int xbShort
xbShort type
Definition: xtypes.h:65
xbStack class
Definition: xstack.h:75
xbMH struct
Definition: dbf.h:201
unsigned short int xbUShort
xbUShort type
Definition: xtypes.h:61