exp.h

Go to the documentation of this file.
00001 /*  $Id: exp.h,v 1.6 2000/11/10 19:04:17 dbryson Exp $
00002 
00003     Xbase project source code 
00004 
00005     This file contains a header file for the EXP object, which is
00006     used for expression processing.
00007 
00008     Copyright (C) 1997  Startech, Gary A. Kunkel   
00009 
00010     This library is free software; you can redistribute it and/or
00011     modify it under the terms of the GNU Lesser General Public
00012     License as published by the Free Software Foundation; either
00013     version 2.1 of the License, or (at your option) any later version.
00014 
00015     This library is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018     Lesser General Public License for more details.
00019 
00020     You should have received a copy of the GNU Lesser General Public
00021     License along with this library; if not, write to the Free Software
00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 
00024     Contact:
00025 
00026       Mail:
00027 
00028         Technology Associates, Inc.
00029         XBase Project
00030         1455 Deming Way #11
00031         Sparks, NV 89434
00032         USA
00033 
00034       Email:
00035 
00036         xbase@techass.com
00037 
00038       See our website at:
00039 
00040         xdb.sourceforge.net
00041 
00042 
00043     V 1.0   10/10/97   - Initial release of software
00044     V 1.5   1/2/97     - Added memo field support
00045     V 1.6a  4/1/98     - Added expression support
00046     V 1.6b  4/8/98     - Numeric index keys
00047     V 1.7.1 5/25/98    - Expression support enhancements
00048 */
00049 
00050 #ifndef __XB_EXP_H__
00051 #define __XB_EXP_H__
00052 
00053 #ifdef __GNUG__
00054 #pragma interface
00055 #endif
00056 
00057 #include <xbase/xbase.h>
00058 
00059 #ifdef XB_EXPRESSIONS             /* compile if expression logic on */
00060 
00061 #define XB_EXPRESSION xbExpNode
00062 
00063 #include <xbase/xtypes.h>
00064 #include <xbase/xstack.h>
00065 
00069 #undef ABS
00070 #undef MIN
00071 #undef MAX
00072 
00073 class XBDLLEXPORT xbDbf;
00074 
00076 
00079 struct XBDLLEXPORT xbFuncDtl {
00080    const char * FuncName;     /* function name               */
00081    xbShort ParmCnt;                  /* no of parms it needs        */
00082    char    ReturnType;               /* return type of function     */
00083    void    (*ExpFuncPtr)();          /* pointer to function routine */
00084 };
00085 
00087 
00090 class XBDLLEXPORT xbExpNode {
00091 public:
00092    char * NodeText;           /* expression text                 */
00093    char Type;                 /* same as TokenType below         */
00094    xbShort Len;                 /* length of expression text       */
00095    xbShort InTree;              /* this node in the tree? 1=yes    */
00096    xbExpNode * Node;            /* pointer to parent               */
00097    xbExpNode * Sibling1;        /* pointer to sibling 1            */
00098    xbExpNode * Sibling2;        /* pointer to sibling 2            */
00099    xbExpNode * Sibling3;        /* pointer to sibling 3            */
00100 
00101    xbShort  DataLen;            /* length of data in result buffer */
00102    xbShort  ResultLen;          /* length of result buffer         */
00103 //   char * Result;             /* result buffer - ptr to result   */
00104    xbString StringResult;
00105    xbDouble DoubResult;         /* Numeric Result                  */
00106    xbShort  IntResult;          /* logical result                  */
00107 
00108    xbDbf *  dbf;                /* pointer to datafile             */
00109    xbShort  FieldNo;            /* field no if DBF field           */
00110    char   ExpressionType;     /* used in head node C,N,L or D    */
00111 
00112 
00113   public:
00114    xbExpNode() :
00115     NodeText(0),
00116     Type(0),
00117     Len(0),
00118     InTree(0),
00119     Node(0),
00120     Sibling1(0),
00121     Sibling2(0),
00122     Sibling3(0),
00123     DataLen(0),
00124     ResultLen(0),
00125     DoubResult(0),
00126     IntResult(0),
00127     dbf(0),
00128     FieldNo(-1),
00129     ExpressionType(0)
00130     {}
00131    ~xbExpNode(){
00132      if( Sibling1 ) delete Sibling1;
00133      if( Sibling2 ) delete Sibling2;
00134      if( Sibling3 ) delete Sibling3;
00135    }
00136 };
00137 
00139 
00142 /* Expression handler */
00143 
00144 class XBDLLEXPORT xbExpn : public xbStack, public xbDate {
00145 public:
00146    xbShort ProcessExpression( xbExpNode *, xbShort );
00147    xbExpNode * GetTree( void ) { return Tree; }
00148    void SetTreeToNull( void ) { Tree = NULL; }
00149    xbExpNode * GetFirstTreeNode( xbExpNode * );
00150 
00151    xbExpn( void );
00152    xbShort  GetNextToken( const char *s, xbShort MaxLen);
00153 
00154    /* expression methods */
00155    xbDouble ABS( xbDouble );
00156    xbLong   ASC( const char * );
00157    xbLong   AT( const char *, const char * );
00158    char *   CDOW( const char * );
00159    char *   CHR( xbLong );
00160    char *   CMONTH( const char * );
00161    char *   DATE();
00162    xbLong   DAY( const char * );
00163    xbLong   DESCEND( const char * );
00164    xbLong   DOW( const char * );
00165    char *   DTOC( const char * );
00166    char *   DTOS( const char * );
00167    xbDouble EXP( xbDouble );   
00168    xbLong   INT( xbDouble );
00169    xbLong   ISALPHA( const char * );
00170    xbLong   ISLOWER( const char * );
00171    xbLong   ISUPPER( const char * );
00172    char *   LEFT( const char *, xbShort );
00173    xbLong   LEN( const char * );
00174    xbDouble LOG( xbDouble );
00175    char *   LOWER( const char * );
00176    char *   LTRIM( const char * );
00177    xbDouble MAX( xbDouble, xbDouble );
00178    xbLong   MONTH( const char * );         /* MONTH() */
00179    xbDouble MIN( xbDouble, xbDouble );
00180    char *   RECNO( xbULong );
00181    xbLong   RECNO( xbDbf * );
00182    char *   REPLICATE( const char *, xbShort );
00183    char *   RIGHT( const char *, xbShort );
00184    char *   RTRIM( const char * );
00185    char *   SPACE( xbShort );   
00186    xbDouble SQRT( xbDouble );
00187    char *   STR( const char * );
00188    char *   STR( const char *, xbShort );
00189    char *   STR( const char *, xbShort, xbShort );
00190    char *   STR( xbDouble );
00191    char *   STR( xbDouble, xbShort );
00192    char *   STR(xbDouble, xbUShort length, xbShort numDecimals );
00193    char *   STRZERO( const char * );
00194    char *   STRZERO( const char *, xbShort );
00195    char *   STRZERO( const char *, xbShort, xbShort );
00196    char *   STRZERO( xbDouble );
00197    char *   STRZERO( xbDouble, xbShort );
00198    char *   STRZERO( xbDouble, xbShort, xbShort );
00199    char *   SUBSTR( const char *, xbShort, xbShort );
00200    char *   TRIM( const char * );
00201    char *   UPPER( const char * );
00202    xbLong   VAL( const char * );
00203    xbLong   YEAR( const char * );  
00205 
00208    void     SetDefaultDateFormat(const xbString f){ DefaultDateFormat = f; }
00209 
00210    xbString GetDefaultDateFormat() const { return DefaultDateFormat; }
00211    xbShort  ProcessExpression( const char *exp, xbDbf * d );
00212    xbShort  ParseExpression( const char *exp, xbDbf * d );
00213    XB_EXPRESSION * GetExpressionHandle();
00214    char     GetExpressionResultType(XB_EXPRESSION * );
00215    char *   GetCharResult();
00216    xbString & GetStringResult();
00217    xbDouble GetDoubleResult();
00218    xbLong   GetIntResult();
00219    xbShort  ProcessExpression( xbExpNode * );
00220    xbShort  BuildExpressionTree( const char * Expression, xbShort MaxTokenLen,
00221             xbDbf *d );
00222 
00223 #ifdef XBASE_DEBUG
00224    void DumpExpressionTree( xbExpNode * );
00225    void DumpExpNode( xbExpNode * );
00226 #endif
00227 
00228 protected:
00229    xbFuncDtl *XbaseFuncList;    /* pointer to list of Xbase functions    */
00230 //   xbExpNode *NextFreeExpNode;  /* pointer to chain of free nodes        */
00231    xbExpNode *Tree;
00232    xbShort LogicalType;         /* set to 1 for logical type nodes       */
00233 
00234    char TokenType;            /* E - Expression, not in simplest form  */
00235                               /* C - Constant                          */
00236                               /* N - Numeric Constant                  */
00237                               /* O - Operator                          */
00238                               /* F - Function                          */
00239                               /* D - Database Field                    */
00240                               /* s - character string result           */
00241                               /* l - logical or short int result       */
00242                               /* d - double result                     */
00243 
00244    char  PreviousType;         /* used to see if "-" follows operator     */
00245    char  *  Op1;               /* pointer to operand 1                    */
00246    char  *  Op2;               /* pointer to operand 2                    */
00247    xbDouble Opd1;              /* double result 1                         */
00248    xbDouble Opd2;              /* double result 2                         */
00249    xbShort OpLen1;             /* length of memory allocated to operand 1 */
00250    xbShort OpLen2;             /* length of memory allocated to operand 2 */
00251    xbShort OpDataLen1;         /* length of data in op1                   */
00252    xbShort OpDataLen2;         /* length of data in op2                   */
00253 
00254    char    OpType1;            /* type of operand 1                       */
00255    char    OpType2;            /* type of operand 2                       */
00256    xbShort TokenLen;           /* length of token                         */
00257 
00258    static xbString DefaultDateFormat;  /*default date format for DTOC func*/
00259 
00260    enum { WorkBufMaxLen = 200 };
00261    char  WorkBuf[WorkBufMaxLen+1];
00262 
00263    xbShort  IsWhiteSpace( char );
00264    char     IsSeparator( char );
00265    xbExpNode * LoadExpNode( const char * ENodeText, const char EType,
00266             const xbShort ELen, const xbShort BufLen );
00267    xbShort  OperatorWeight( const char *Oper, xbShort len );
00268    xbShort  ReduceComplexExpression( const char * NextToken, xbShort Len,
00269             xbExpNode * cn, xbDbf *d );
00270    xbShort  GetFunctionTokenLen( const char *s );
00271    xbShort  ReduceFunction( const char *NextToken, xbExpNode *cn, xbDbf *d );
00272    xbExpNode * GetNextTreeNode( xbExpNode * );
00273    xbShort  ProcessOperator( xbShort );
00274    xbShort  ProcessFunction( char * );
00275    xbShort  ValidOperation( char *, char, char );
00276    char     GetOperandType( xbExpNode * );
00277    xbShort  AlphaOperation( char * );
00278    xbShort  NumericOperation( char * );
00279    xbExpNode * GetExpNode( xbShort );
00280    xbShort  GetFuncInfo( const char *Function, xbShort Option );
00281    xbDouble GetDoub( xbExpNode * );
00282    xbLong   GetInt( xbExpNode * );
00283 };
00284 #endif               // XB_EXPRESSIONS
00285 #endif               // __XB_EXP_H__

Generated on Fri Aug 17 03:28:03 2007 for Xbase Class Library by  doxygen 1.5.2