error.h
#ifndef ERROR_H
/******************************************************************************
* *
* M O D U L E D E F I N E *
* *
******************************************************************************/
#define ERROR_H
/******************************************************************************
* *
* C O M P I L E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* U S E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#ifdef __cplusplus
extern "C" { /* Assume C declarations for C++ */
#endif /* __cplusplus */
/******************************************************************************
* *
* G L O B A L D E F I N E S *
* *
******************************************************************************/
/******************************************************************************
* *
* S T R U C T U R E D E F I N I T I O N S *
* *
******************************************************************************/
#define NO_ERROR 0
#define STACKER_OVERFLOW 1 /* success have success */
#define FREE_STACKER_ERROR 2
/*--------------------------------------------------
IF - THEN - ELSEIF - ELSE -ENDIF
--------------------------------------------------*/
#define IF_NOT_THEN 3
#define IF_NOT_ENDIF 4
#define ELSEIF_NOT_IF 5
#define ELSE_NOT_IF 6
#define ENDIF_NOT_IF 7
#define ERROR_THEN 8
#define ERROR_ENDIF 9
/*--------------------------------------------------
FOR - EXITFOR - NEXT
--------------------------------------------------*/
#define ERROR_FOR 10
#define ERROR_FOR_COUNTER 11
#define FOR_NOT_NEXT 12
#define NEXT_NOT_FOR 13
#define EXITFOR_NOT_FOR 14
#define NEXT_VAR_ERROR 15
#define FOR_USE_TIME_VAR 16
/*--------------------------------------------------
WHILE_WEND
--------------------------------------------------*/
#define WHILE_NOT_WEND 17
#define END_NOT_WHILE 18
#define ERROR_WEND 19
/*--------------------------------------------------
DO - EXITDO LOOP
--------------------------------------------------*/
#define ERROR_DO 20
#define DO_NOT_LOOP 21
#define LOOP_NOT_DO 22
#define ERROR_DO_LOOP 23
#define ERROR_LOOP 24
/*--------------------------------------------------
FILE OPEN CLOSE READ WRITE
--------------------------------------------------*/
#define ERROR_OPEN_MODE 25
#define ERROR_OPEN_FOR 26 /* END have used */
#define ERROR_FILE_NUMBER 27
#define ERROR_OPEN_AS 28
#define READ_FILE_ERROR 29
#define ERROR_KILL 30
#define ERROR_FILE_MARK 31 /* need # */
#define ERROR_FILE_NAME 32
#define ERROR_FEOF 33
#define ERROR_SEEK 34
#define ERROR_WRITE 35
#define ERROR_READ 36
#define ERROR_OPEN 37
#define ERROR_CLOSE 38
#define OUT_OF_MEMORY 39
/*--------------------------------------------------
GOTO GOSUB LABEL
--------------------------------------------------*/
#define GOTO_NOT_LABEL 40
#define GOTO_STACK_OVERFLOW 41
#define ERROR_GOTO 42
#define HAVE_LABEL 43
#define GOSUB_NOT_END_RETURN 44
#define ERROR_GOSUB 45
#define ERROR_LABEL 46
#define GOSUB_NOT_LABEL 47
#define RETURN_NOT_GOSUB 48
/*--------------------------------------------------
PRINT OUT USING
--------------------------------------------------*/
#define ERROR_INPUT 49
#define ERROR_USING 50
#define ERROR_PRINT 51
#define USING_STACK_OVERFLOW 52
#define ERROR_OUT 53
#define ERROR_PORT 54
#define ERROR_FOUT 55
#define ERROR_FOUT_SIZE 56
/*--------------------------------------------------
DIM LCD
--------------------------------------------------*/
#define DIM_ERROR 57
#define ERROR_DIM 58
#define HAVE_DIM 59
#define ERROR_CURSOR 60
#define ERROR_LOCATE 61
#define ERROR_CLS 62
/*--------------------------------------------------
Global Variable
--------------------------------------------------*/
#define ERROR_SOUND 63
#define ERROR_YEAR 64
#define SET_GlOBAL_ERROR 65
#define ERROR_MONTH 66
#define ERROR_DATE 67
#define ERROR_HOUR 68
#define ERROR_MINUTE 69
#define ERROR_SECOND 70
/*--------------------------------------------------
Variable
--------------------------------------------------*/
#define NOT_VAR 71
#define SET_VAR_ERROR 72
#define Add_VAR_ERROR 73
#define VARIABLE_ERROR 74
#define SYNTAX_ERROR 75
/*--------------------------------------------------
Expression
--------------------------------------------------*/
#define DIVISION_BY_ZERO 76
#define ILLEGAL_FUNCTION_CALL 77
#define TYPE_MISMATCH 78
#define OUT_OF_STRING_SPACE 79
#define VARIABLE_NOT_DEFINED 80
#define ARRAY_NOT_DEFINED 81
#define UNKNOW_OPERATOR 82
#define EXPRESSION_TOO_COMPLEX 83
#define ADD_FILE_ERROR 84
#define UNMATCH_BRACE 85
/******************************************************************************
* *
* G L O B A L V A R I A B L E S - N O I N I T I A L I Z E R S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* G L O B A L V A R I A B L E S - I N I T I A L I Z E R S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* F U N C T I O N P R O T O T Y P E S *
* *
******************************************************************************/
/* 1 is success , 25 is END */
/* error 0xf1 */ /* the program must end */
/* overflow 0xf0 */
/* success 0x01 */
/* faint 0xf0 */ /* find not or .... */
/* true 0x01 */
/* false 0x00 */
/* Perror 0xfe */ /* the program error */
/* no_use 0xff */
/* no_data 0x07 */
/* THEN_exp 0xf2 */ /* after THEN have exp */
#ifdef __cplusplus
} /* End of extern "C" { */
#endif /* __cplusplus */
#endif
expdoub.c
/******************************************************************************
* *
* M O D U L E D E F I N E *
* *
******************************************************************************/
#define EXPDOUB_C
/******************************************************************************
* *
* C O M P I L E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/******************************************************************************
* *
* U S E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#include "Common.h"
#include "XKey.h"
#include "XADC.h"
#include "XTimer.h"
#include "XPrtEng.h"
#include "XFileSys.h"
#include "..\ParserUtil.h"
#include "TsplFunc.h"
#include "VarMgr.h"
#include "exprpn.h"
#include "exputil.h"
#include "expfmt.h"
#include "expr.h"
#include "error.h"
/******************************************************************************
* *
* L O C A L D E F I N E S *
* *
******************************************************************************/
#if defined(TSPL)
/******************************************************************************
* *
* L O C A L T Y P E D E F S *
* *
******************************************************************************/
/******************************************************************************
* *
* L O C A L F U N C T I O N P R O T O T Y P E S *
* *
******************************************************************************/
DOUBLE expDoubleDispatch(DOUBLE, DOUBLE, BYTE);
DOUBLE expDoubFuncDispatch(BYTE*, BYTE**);
DOUBLE expGetSensor(CHAR *, INT );
DOUBLE DoubleAddition(DOUBLE, DOUBLE);
DOUBLE DoubleSubtraction(DOUBLE, DOUBLE);
DOUBLE DoubleMultiplication(DOUBLE, DOUBLE);
DOUBLE DoubleDivision(DOUBLE, DOUBLE);
DOUBLE DoubleGreater(DOUBLE, DOUBLE);
DOUBLE DoubleSmaller(DOUBLE, DOUBLE);
DOUBLE DoubleEqual(DOUBLE, DOUBLE);
DOUBLE expDoubleMain(VOID);
/******************************************************************************
* *
* L O C A L I N I T I A L I Z E D D A T A D E F I N I T I O N S *
* *
******************************************************************************/
/******************************************************************************
* *
* L O C A L U N I T I A L I Z E D D A T A D E F I N I T I O N S *
* *
******************************************************************************/
DOUBLE NextOperand(VOID)
{
BYTE *psTemp;
BYTE szToken[TOKEN_LEN]; /*Token extracted from statement*/
DOUBLE dOperand;
switch (CheckOperandType(*(pszStatement[szStatePtr]))) /*Check the type of this token*/
{
case NUMERAL: /*If it is a numeral*/
Delimitate(szToken, pszStatement[szStatePtr], (BYTE *)"+-\x2a/<>=(),;\r\n\"", 1);
/*Take next token delimitated by the given characters*/
dOperand = expStringToDouble(szToken); /*Translate to a double*/
break;
case FUNCTION: /*If it is a function*/
Delimitate(szToken, pszStatement[szStatePtr], (BYTE *)"+-\x2a/<>=(),;\r\n\"", 1);
/*Take next token delimitated by the given characters*/
dOperand = expDoubFuncDispatch(szToken, pszStatement[szStatePtr]); /*Call function handler*/
break;
case VARIABLE: /*If it is a variable*/
Delimitate(szToken, pszStatement[szStatePtr], (BYTE *)"+-\x2a/<>=(),;\r\n\"", 1);
/*Take next token delimitated by the given characters*/
if (CheckVariableType(szToken) == DOUBLE_TYPE) /*If it is a double*/
{
if ((dOperand = CheckGlobalVariable(szToken)) != 0) /*Check variable ID*/
{
dOperand = GetGlobalVariable(dOperand, _NULL); /*Get a variable value*/
}
else if ((dOperand = CheckDoubleVariable(szToken)) != 0)/*Check variable ID*/
{
dOperand = GetDoubleVariable(dOperand); /*Get a variable value*/
}
else
{
wErrorword = VARIABLE_NOT_DEFINED;
}
}
else if (CheckVariableType(szToken) == FLOAT_TYPE) /*If it is a float*/
{
dOperand = CheckFloatVariable(szToken); /*Check variable ID*/
if (dOperand) /*If variable ID is available*/
{
dOperand = GetFloatVariable(dOperand); /*Get a variable value*/
}
else
{
wErrorword = VARIABLE_NOT_DEFINED;
}
}
else if (CheckVariableType(szToken) == INTEGER_TYPE) /*If it is a integer*/
{
dOperand = CheckIntegerVariable(szToken); /*Check variable ID*/
if (dOperand) /*If variable ID is available*/
{
dOperand = GetIntegerVariable(dOperand); /*Get a variable value*/
}
else
{
wErrorword = VARIABLE_NOT_DEFINED;
}
}
else
{
wErrorword = SYNTAX_ERROR;
}
break;
case PARENTHESIS: /*If it is a round bracket */
Delimitate(szToken, pszStatement[szStatePtr], (BYTE *)"(", 1);
/*Take next token delimitated by the given characters*/
PushOperator('\0'); /*Make a tag in RPN buffer*/
dOperand = expDoubleMain(); /*Launch another session of expression*/
break;
case INTEGER_DIM:
GetDimToken(szToken, pszStatement[szStatePtr]);
psTemp = szToken;
dOperand = CheckIntegerDimension(&psTemp); /*Check variable ID*/
if (dOperand) /*If variable ID is available*/
{
dOperand = GetIntegerDimension(dOperand); /*Get a variable value*/
}
else
{
wErrorword = VARIABLE_NOT_DEFINED;
}
break;
case FLOAT_DIM:
GetDimToken(szToken, pszStatement[szStatePtr]);
psTemp = szToken;
dOperand = CheckFloatDimension(&psTemp); /*Check variable ID*/
if (dOperand) /*If variable ID is available*/
{
dOperand = GetFloatDimension(dOperand); /*Get a variable value*/
}
else
{
wErrorword = ARRAY_NOT_DEFINED;
}
break;
case DOUBLE_DIM:
GetDimToken(szToken, pszStatement[szStatePtr]);
psTemp = szToken;
dOperand = CheckDoubleDimension(&psTemp); /*Check variable ID*/
if (dOperand) /*If variable ID is available*/
{
dOperand = GetDoubleDimension(dOperand); /*Get a variable value*/
}
else
{
wErrorword = ARRAY_NOT_DEFINED;
}
break;
default:
wErrorword = VARIABLE_NOT_DEFINED;
}
return dOperand;
}
/******************************************************************************
*
* Function:
* expDoubleMain
*
* Description:
* This function double expression main rountine.
*
* Input:
* Pointer to a pointer to null-terminated string
*
* Output:
* Double
*
******************************************************************************/
DOUBLE expDoubleMain(VOID)
{
BYTE szToken[TOKEN_LEN]; /*Token extracted from statement*/
DOUBLE dOperand1; /*First operand*/
DOUBLE dOperand2; /*Second operand*/
DOUBLE dOperandBackup; /*Temporary storage for operand*/
BYTE bOperator; /*Operator*/
BYTE bOperatorBackup; /*Temporary storage for operator*/
CHAR cSign; /*Flag to indicate negative or positive*/
cSign = 1; /*Default vlaue is positive*/
Delimitate(szToken, pszStatement[szStatePtr], (BYTE *)"+-\x2a/<>=(),;\r\n\"", 0);
/*Take next token delimitated by the given characters*/
if ((*szToken == '-') || (*szToken == '+')) /*If there's a sign character*/
{
if (*szToken == '-') /*If it is a negative sign character*/
{
cSign = -1; /*Set negative flag*/
}
Delimitate(szToken, pszStatement[szStatePtr], (BYTE *)"+-", 1);
/*Take next token delimitated by the given characters*/
}
dOperand1 = NextOperand();
dOperand1 *= cSign; /*Make it negative*/
if (PushOperand(dOperand1) == FAIL) /*Push the result into RPN buffer*/
{
wErrorword = EXPRESSION_TOO_COMPLEX;
}
while (isspace(**(pszStatement[szStatePtr]))) /* remove the space */
(*(pszStatement[szStatePtr]))++;
while ((**(pszStatement[szStatePtr]) != '\0') &&
(**(pszStatement[szStatePtr]) != ')') &&
(**(pszStatement[szStatePtr]) != ',') &&
(wErrorword == NO_ERROR)) /*Do until the end of statement*/
{
Delimitate(szToken, pszStatement[szStatePtr], (BYTE *)"+-\x2a/<>=", 1);
/*Take next token delimitated by the given characters*/
bOperator = bOperatorBackup = *szToken; /*Backup the operator*/
Delimitate(szToken, pszStatement[szStatePtr], (BYTE *)"+-\x2a/<>=(),;\r\n\"", 0);
/*Take next token delimitated by the given characters*/
if (((bOperator == '<') || (bOperator == '=') || (bOperator == '>')) &&
((*szToken == '<') || (*szToken == '=') || (*szToken == '>')))
{
Delimitate(szToken, pszStatement[szStatePtr], (BYTE *)"+-\x2a/<>=", 1);
/*Take next token delimitated by the given characters*/
bOperator = bOperatorBackup = bOperator + *szToken; /*Backup the operator*/
Delimitate(szToken, pszStatement[szStatePtr], (BYTE *)"+-\x2a/<>=(),;\r\n\"", 0);
/*Take next token delimitated by the given characters*/
}
dOperandBackup = dOperand2 = NextOperand(); /*Backup current operand*/
if (CheckPrior(bOperator)<=CheckPrior(PeekOperator()))
/*If current operator is less prior or equal to the last operator*/
{
if (CheckPrior(PeekOperator()) == 0xff) /*If operator buffer is empty*/
{
PushOperator(bOperatorBackup); /*Push operator*/
if (PushOperand(dOperandBackup) == FAIL) /*Push the result into RPN buffer*/
{
wErrorword = EXPRESSION_TOO_COMPLEX;
}
}
else
{
dOperand2 = PopOperand(); /*Pop second operand*/
dOperand1 = PopOperand(); /*Pop first operand*/
bOperator = PopOperator(); /*Pop operator*/
dOperand2 = expDoubleDispatch(dOperand1, dOperand2, bOperator);
/*Implement double operation*/
if (PushOperand(dOperand2) == FAIL) /*Push the result into RPN buffer*/
{
wErrorword = EXPRESSION_TOO_COMPLEX;
}
PushOperator(bOperatorBackup); /*Push backup operator*/
if (PushOperand(dOperandBackup) == FAIL) /*Push the result into RPN buffer*/
{
wErrorword = EXPRESSION_TOO_COMPLEX;
}
}
}
else if (CheckPrior(bOperator) > CheckPrior(PeekOperator()))
/*If current operator is prior to the last operator*/
{
dOperand1 = PopOperand(); /*Pop first operand*/
dOperand2 = expDoubleDispatch(dOperand1, dOperand2, bOperator);
/*Implement double operation*/
if (PushOperand(dOperand2) == FAIL) /*Push the result into RPN buffer*/
{
wErrorword = EXPRESSION_TOO_COMPLEX;
}
}
while (isspace(**(pszStatement[szStatePtr]))) /*Skip white space*/
{
*(pszStatement[szStatePtr]) += 1; /*Increace pointer to string*/
}
}
*(pszStatement[szStatePtr]) += 1; /*The end of expression (';',')'etc.*/
dOperand2 = PopOperand(); /*Pop second operand*/
while (1)
{
bOperator = PopOperator(); /*Pop operator*/
if (bOperator == 0) /*If reach the end of this session*/
{
break;
}
dOperand1 = PopOperand(); /*Pop first operand*/
dOperand2 = expDoubleDispatch(dOperand1, dOperand2, bOperator);
/*Implement double operation*/
}
return dOperand2; /*Return the result of this session*/
}
/******************************************************************************
*
* Function:
* extDoubleDispatch
*
* Description:
* This function dispatch double operations.
*
* Input:
* 1)Operand 1, 2)operand 2, 3)operator
*
* Output:
* Double
*
******************************************************************************/
DOUBLE expDoubleDispatch(DOUBLE dOperand1, DOUBLE dOperand2, BYTE bOperator)
{
switch (bOperator)
{
case '+':
return DoubleAddition(dOperand1, dOperand2);
/*Addition operation*/
case '-':
return DoubleSubtraction(dOperand1, dOperand2);
/*Subtraction operation*/
case '*':
return DoubleMultiplication(dOperand1, dOperand2);
/*Multiplication operation*/
case '/':
return DoubleDivision(dOperand1, dOperand2);
/*Division operation*/
case '>':
return DoubleGreater(dOperand1, dOperand2);
/*Greater operation*/
case '<':
return DoubleSmaller(dOperand1, dOperand2);
/*Smaller operation*/
case '=':
return DoubleEqual(dOperand1, dOperand2);
/*Equal operation*/
case '<'+'>':
return DoubleEqual(dOperand1, dOperand2) ? 0 : 1;
/*Inequal operation*/
case '<'+'=':
return DoubleGreater(dOperand1, dOperand2) ? 0 : 1;
/*Smaller or equal operation*/
case '>'+'=':
return DoubleSmaller(dOperand1, dOperand2) ? 0 : 1;
/*Greater or equal operation*/
default:
wErrorword = UNKNOW_OPERATOR;
}
return 0;
}
/******************************************************************************
*
* Function:
* extDoubFuncDispatch
*
* Description:
* This function dispatch double functions.
*
* Input:
* Pointer to pointer to statement
* *szToken = String Of Type
* ** pszState =
* Output:
* Double
*
******************************************************************************/
DOUBLE expDoubFuncDispatch(BYTE* szToken, BYTE** pszState)
{
BYTE *pTemp1, *pTemp2;
DOUBLE retval = 0;
_FileList List;
_TextAttr Text;
_BarCodeAttr Barcode;
_BarcodeTable *BarcodeTab;
INT temp1, temp2;
DATE date;
pTemp1 = malloc(HUGE_LEN);
pTemp2 = malloc(HUGE_LEN);
if (pTemp1 == 0 || pTemp2 == 0)
{
wErrorword = OUT_OF_STRING_SPACE;
if (pTemp2) free(pTemp2);
if (pTemp1) free(pTemp1);
return retval;
}
switch (CheckFunctionType(szToken))
{
case FUNC_ABS: /*If ABS()*/
Delimitate(NULL, pszState, (BYTE *)"(", 1);
/*Take next token delimitated by the given characters*/
PushOperator('\0'); /*Make a tag in RPN buffer*/
retval = fabs(expDoubleMain()); /*Return absolute value*/
break;
case FUNC_ASC: /*If ASC()*/
Delimitate(NULL, pszState, (BYTE *)"(", 1);
/*Take next token delimitated by the given characters*/
expStringMain(pTemp1); /*Get a character from string expression*/
retval = (DOUBLE)pTemp1[0]; /*Return ASCII code*/
break;
case FUNC_BARCODEPIXEL: /* BARCODEPIXEL(CONT$, SYM$, NARROW, WIDE) */
Delimitate(NULL, pszState, (BYTE *)"(", 1);
/*Take next token delimitated by the given characters*/
expStringMain(pTemp1); /*Launch a string expression*/
expStringMain(pTemp2); /*Launch a string expression*/
PushOperator('\0'); /*Make a tag in RPN buffer*/
temp1 = (INT)expDoubleMain(); /*Launch a double expression*/
temp2 = (INT)expDoubleMain(); /*Launch a double expression*/
if (BarcodeTab = CheckBarcodeTable((CHAR *)pTemp2, sBarcodeTab))
{
Barcode.psImageBuffer = &sImageBuffer;
Barcode.sCoord.iX = Barcode.sCoord.iY = 0;
Barcode.FontHoriMulti = 1;
Barcode.FontVertMulti = 1;
Barcode.AboveCode = FALSE;
Barcode.iNarrow = temp1;
Barcode.iWide = temp2;
Barcode.iHeight = 8;
Barcode.iHuman = 0;
Barcode.iRotation = 0;
Barcode.iCheck = BarcodeTab->Check;
Barcode.pFont = (BYTE *)BarcodeTab->FontName;
Barcode.pExp = (BYTE *)pTemp1;
Barcode.eLanguage = LANG_TSPL;
Barcode.ePutWay = PUT_OR;
BarcodeTab->PreviewBarcode(&Barcode);
if (strlen((CHAR *)Barcode.pExp))
retval = (DOUBLE)Barcode.TotalWidth;
}
break;
case FUNC_DATEADD: /* DATEADD(INTERVAL$, NUMBER, DATE) */
Delimitate(NULL, pszState, (BYTE *)"(", 1);
/* Required. String expression representing the time interval you want to add. */
expStringMain(pTemp1);
/* Required. Numeric expression that is the number of intervals you want to add. */
PushOperator('\0');
temp1 = (INT)expDoubleMain();
/* Required. Date value that you want to evaluate. */
if (CheckExpressionType(*pszState) == NUMERAL) /*Numeric expression*/
{
PushOperator('\0'); /*Make a tag in RPN buffer*/
retval = expDoubleMain();
date = expDoubleToDate(retval); /*Convert the double to date/time*/
}
else /*String expression*/
{
expStringMain(pTemp2); /*Launch another session of expression*/
date = expStringToDate(pTemp2); /*Convert the string to date/time*/
}
if (expDateAdd((CHAR *)pTemp1, temp1, &date))
retval = expDateToDouble(date);
else
wErrorword = SYNTAX_ERROR;
break;
case FUNC_EOF: /*If EOF()*/
Delimitate(NULL, pszState, (BYTE *)"(", 1);
PushOperator('\0'); /*Make a tag in RPN buffer*/
temp1 = (INT)expDoubleMain(); /*Launch a double expression*/
if (temp1 == 0)
retval = (Feof(UseHandle_0) == 0 ? 0 : 1);
if (temp1 == 1)
retval = (Feof(UseHandle_1) == 0 ? 0 : 1);
break;
case FUNC_FPOS:
Delimitate(NULL, pszState, (BYTE *)"(", 1);
PushOperator('\0'); /*Make a tag in RPN buffer*/
temp1 = (INT)expDoubleMain(); /*Launch a double expression*/
retval = Ftell(sBasicData.UseHandle);
break;
case FUNC_FRE: /*If FRE()*/
Delimitate(NULL, pszState, (BYTE *)"(", 1);
temp1 = (INT)DRAM_DEVICE;
if (CheckExpressionType(*pszState) == STRING_TYPE) /*Check the type*/
{
expStringMain(pTemp1);
if (strcmp((CHAR *)pTemp1, "F") == 0 || strcmp((CHAR *)pTemp1, "FLASH") == 0)
temp1 = (INT)FLASH_DEVICE;
if (strcmp((CHAR *)pTemp1, "E") == 0 || strcmp((CHAR *)pTemp1, "CARD") == 0)
temp1 = (INT)CARD_DEVICE;
}
retval = FreeSpace((_eFileDevice)temp1);
break;
case FUNC_GETKEY: /*IF GETKEY()*/
Delimitate(NULL, pszState, (BYTE *)")", 1);
*pszState += 1;
temp1 = (INT)ConGetKey();
retval = (DOUBLE)temp1;
break;
case FUNC_GETSENSOR: /* GETSENSOR(TYPE$[, INTEN]) */
Delimitate(NULL, pszState, (BYTE *)"(", 1);
/* Required. String expression containing the name of the sensor. */
expStringMain(pTemp1);
/* Optional. Numeric expression that sets the sensor intension.
If omitted, the sensor intension is printer configuration. */
temp1 = 0;
if (*(*pszState - 1) == ',')
{
PushOperator('\0'); /*Make a tag in RPN buffer*/
temp1 = (INT)expDoubleMain(); /*Launch a double expression*/
}
retval = expGetSensor((CHAR *)pTemp1, temp1);
break;
case FUNC_INSTR: /* INSTR([START, ]STR1$, STR2$) */
Delimitate(NULL, pszState, (BYTE *)"(", 1);
/* Start Optional. Numeric expression that sets the starting position for each search.
If omitted, search begins at the first character position. The start index is 1-based. */
temp1 = 1;
if (CheckExpressionType(*pszState) == NUMERAL) /*Check the type*/
{
PushOperator('\0'); /*Make a tag in RPN buffer*/
temp1 = (INT)expDoubleMain(); /*Launch a double expression*/
if (temp1 < 1)
wErrorword = SYNTAX_ERROR;
}
/* String1 Required. String expression being searched. */
expStringMain(pTemp1); /*Launch a string expression*/
/* String2 Required. String expression sought. */
expStringMain(pTemp2); /*Launch a string expression*/
if (temp1 <= strlen((CHAR *)pTemp1))
{
if (temp1 = (INT)strstr((CHAR *)pTemp1 + temp1 - 1, (CHAR *)pTemp2))
temp1 -= (INT)pTemp1 - 1;
retval = (DOUBLE)temp1;
}
break;
case FUNC_INT: /*If INT()*/
Delimitate(NULL, pszState, (BYTE *)"(", 1);
/*Take next token delimitated by the given characters*/
PushOperator('\0'); /*Make a tag in RPN buffer*/
retval = (LONG)expDoubleMain(); /*Round down the double*/
break;
case FUNC_LEN: /*If LEN()*/
Delimitate(NULL, pszState, (BYTE *)"(", 1);
/*Take next token delimitated by the given characters*/
expStringMain(pTemp1); /*Launch a string expression*/
retval = (DOUBLE)strlen((CHAR *)pTemp1); /*Return string length*/
break;
case FUNC_LOB: /*If LOB()*/
Delimitate(NULL, pszState, (BYTE *)")", 1);
*pszState += 1;
SetDirectIgnoreLF(FALSE);
retval = (DOUBLE)GetBufferLength();
break;
case FUNC_LOC: /*If LOC()*/
Delimitate(NULL, pszState, (BYTE *)"(", 1);
PushOperator('\0'); /*Make a tag in RPN buffer*/
temp1 = (INT)expDoubleMain(); /*Launch a double expression*/
if (temp1 == 0)
retval = Ftell(UseHandle_0);
if (temp1 == 1)
retval = Ftell(UseHandle_1);
break;
case FUNC_LOF: /*If LOF()*/
Delimitate(NULL, pszState, (BYTE *)"(", 1);
expStringMain(pTemp1);
OpenList(&List, CARD_DEVICE);
while (NextFile(&List, (CHAR *)pTemp1))
retval = (List.FileSize);
OpenList(&List, FLASH_DEVICE);
while (NextFile(&List, (CHAR *)pTemp1))
retval = (List.FileSize);
OpenList(&List, DRAM_DEVICE);
while (NextFile(&List, (CHAR *)pTemp1))
retval = (List.FileSize);
break;
case FUNC_NOW: /* NOW()*/
Delimitate(NULL, pszState, (BYTE *)")", 1);
*pszState += 1;
retval = GetGlobalVariable(GLO_NOW, _NULL);
break;
case FUNC_POS: /*If POS()*/
// Delimitate(NULL, pszState, (BYTE *)")", 1);
// *pszState += 1;
// retval = ConGetCursorPosition();
break;
case FUNC_RND:
// Delimitate(NULL, pszState, (BYTE *)")", 1);
// *pszState += 1;
// retval = (DOUBLE)GetTimer()/(DOUBLE)RAND_MAX;
break;
case FUNC_STRCOMP: /* STRCOMP(STR1$, STR2$[, COMP]) */
Delimitate(NULL, pszState, (BYTE *)"(", 1);
/* Required. Any valid string expression. */
expStringMain(pTemp1);
/* Required. Any valid string expression. */
expStringMain(pTemp2);
/* Optional. Numeric value indicating the kind of comparison to use when evaluating strings.
If omitted, a binary comparison is performed. */
temp1 = 0;
if (*(*pszState - 1) == ',')
{
PushOperator('\0');
temp1 = (INT)expDoubleMain();
}
for (retval = 0, temp2 = 0; retval == 0 && temp2 <= strlen((CHAR *)pTemp1); temp2++)
{
if ((temp1 == 0 && *(pTemp1 + temp2) != *(pTemp2 + temp2)) ||
/* Textual comparison is case-insensitive */
toupper(*(pTemp1 + temp2)) != toupper(*(pTemp2 + temp2)))
{
/* STR1 is less than STR2 return -1 */
if (*(pTemp1 + temp2) < *(pTemp2 + temp2))
retval = -1;
/* STR1 is greater than STR2 return 1 */
if (*(pTemp1 + temp2) > *(pTemp2 + temp2))
retval = 1;
}
}
break;
case FUNC_TEXTPIXEL: /* TEXTPIXEL(CONT$, FONT$, SIZE) */
Delimitate(NULL, pszState, (BYTE *)"(", 1);
/*Take next token delimitated by the given characters*/
expStringMain(pTemp1); /*Launch a string expression*/
expStringMain(pTemp2); /*Launch a string expression*/
PushOperator('\0'); /*Make a tag in RPN buffer*/
temp1 = (INT)expDoubleMain(); /*Launch a double expression*/
Text.psImageBuffer = &sImageBuffer;
Text.sCoord.iX = Text.sCoord.iY = 0;
Text.iDirection = Text.iRotation = 0;
Text.iHoriMulti = Text.iVertMulti = temp1;
Text.ExpLength = strlen((CHAR *)pTemp1);
Text.pExp = pTemp1;
Text.pFont = pTemp2;
Text.eLanguage = LANG_TSPL;
Text.ePutWay = PUT_OR;
PreviewText(&Text);
retval = (DOUBLE)Text.iRealWidth;
break;
case FUNC_VAL: /*If VAL()*/
Delimitate(NULL, pszState, (BYTE *)"(", 1);
/*Take next token delimitated by the given characters*/
expStringMain(pTemp1); /*String expression*/
retval = expStringToDouble(pTemp1); /*Convert the string to double*/
break;
default:
wErrorword = ILLEGAL_FUNCTION_CALL;
break;
}
free(pTemp2);
free(pTemp1);
return retval;
}
/******************************************************************************
*
* Function:
* expGetSensor
*
* Description:
*
* Input:
*
* Output:
*
******************************************************************************/
DOUBLE expGetSensor(CHAR *name, INT intension)
{
_PrintCfg *cfg = GrabPrintConfig();
INT value = 0;
#if defined(AT91SAM9260)
INT setup = 35000 * 10; // setup time is 10 ms
#elif defined(N3290)
INT setup = 7100 * 10;
#elif defined(SH7040)
INT setup = 7100 * 10; // setup time is 10 ms
#endif
// return value of the gap sensor
if (strcmp(name, "GAP") == 0)
{
WaitWorkJobEnd();
intension = intension ? intension : cfg->GapInten;
EnableSensor(GAP_SENSOR, intension, cfg->RibbonFlag, 0);
DelayTime(setup);
#if defined(GAP_SENSOR_AD)
StartADConvert(GAP_SENSOR_AD);
value = ReadAD(GAP_SENSOR_AD);
#else
value = GetSensorSignal(GAP_SENSOR);
#endif
DisableSensor(GAP_SENSOR);
}
// return value of the black mark sensor
if (strcmp(name, "BLINE") == 0)
{
WaitWorkJobEnd();
intension = intension ? intension : cfg->BlineInten;
EnableSensor(BLINE_SENSOR, intension, cfg->RibbonFlag, 0);
DelayTime(setup);
#if defined(BLINE_SENSOR_AD)
StartADConvert(BLINE_SENSOR_AD);
value = ReadAD(BLINE_SENSOR_AD);
debug_printf("7");
#else
value = GetSensorSignal(BLINE_SENSOR);
#endif
DisableSensor(BLINE_SENSOR);
}
// return value of the ribbon sensor
if (strcmp(name, "RIBBON") == 0)
{
WaitWorkJobEnd();
intension = intension ? intension : cfg->RibbonInten;
EnableSensor(RIBBON_SENSOR, intension, cfg->RibbonFlag, 0);
DelayTime(setup);
#if defined(RIBBON_SENSOR_AD)
StartADConvert(RIBBON_SENSOR_AD);
value = ReadAD(RIBBON_SENSOR_AD);
#else
value = GetSensorSignal(RIBBON_SENSOR);
#endif
DisableSensor(RIBBON_SENSOR);
}
// return status of the peel-off sensor (1 = paper is on top of peel sensor)
if (strcmp(name, "PEEL") == 0)
{
value = IsPeelsLabelExists() ? 1 : 0;
}
// return print head up (1 = head in up position)
if (strcmp(name, "HEAD UP") == 0)
{
#if defined(CARRIAGE_OPEN_MODEL)
value = IsCarriageOpen() ? 1 : 0;
#endif
}
// return print head temperature
if (strcmp(name, "HEAD TEMP") == 0)
{
#if defined(TPH_TEMP_AD)
StartADConvert(TPH_TEMP_AD);
value = ReadAD(TPH_TEMP_AD);
value = CalculateTPHTemp((WORD)value);
#endif
}
// return print head volt AD
if (strcmp(name, "HEAD VOLT") == 0)
{
WaitWorkJobEnd();
SetTPHPower(TRUE);
DelayTime(setup);
#if defined(TPH_VOLT_AD)
StartADConvert(TPH_VOLT_AD);
value = ReadAD(TPH_VOLT_AD);
#endif
SetTPHPower(FALSE);
}
return (DOUBLE)value;
}
/******************************************************************************
*
* Function:
* DoubleAddition
*
* Description:
* This function handle double addition.
*
* Input:
* 1)Operand 1, 2)operand 2, 3)operator
*
* Output:
* Double
*
******************************************************************************/
DOUBLE DoubleAddition(DOUBLE dOperand1, DOUBLE dOperand2)
{
return dOperand1 + dOperand2;
}
/******************************************************************************
*
* Function:
* DoubleSubtraction
*
* Description:
* This function handle double subtraction.
*
* Input:
* 1)Operand 1, 2)operand 2, 3)operator
*
* Output:
* Double
*
******************************************************************************/
DOUBLE DoubleSubtraction(DOUBLE dOperand1, DOUBLE dOperand2)
{
return dOperand1 - dOperand2;
}
/******************************************************************************
*
* Function:
* DoubleMultiplication
*
* Description:
* This function handle double multiplication.
*
* Input:
* 1)Operand 1, 2)operand 2, 3)operator
*
* Output:
* Double
*
******************************************************************************/
DOUBLE DoubleMultiplication(DOUBLE dOperand1, DOUBLE dOperand2)
{
return dOperand1 * dOperand2;
}
/******************************************************************************
*
* Function:
* DoubleDivision
*
* Description:
* This function handle double division.
*
* Input:
* 1)Operand 1, 2)operand 2, 3)operator
*
* Output:
* Double
*
******************************************************************************/
DOUBLE DoubleDivision(DOUBLE dOperand1, DOUBLE dOperand2)
{
if (dOperand2 == 0)
{
wErrorword = DIVISION_BY_ZERO;
return 0;
}
return dOperand1 / dOperand2;
}
/******************************************************************************
*
* Function:
* DoubleGreater
*
* Description:
* This function handle double comparison.
*
* Input:
* 1)Operand 1, 2)operand 2, 3)operator
*
* Output:
* Double
*
******************************************************************************/
DOUBLE DoubleGreater(DOUBLE dOperand1, DOUBLE dOperand2)
{
if (dOperand1 > dOperand2)
{
return 1;
}
else
{
return 0;
}
}
/******************************************************************************
*
* Function:
* DoubleSmaller
*
* Description:
* This function handle double comparison.
*
* Input:
* 1)Operand 1, 2)operand 2, 3)operator
*
* Output:
* Double
*
******************************************************************************/
DOUBLE DoubleSmaller(DOUBLE dOperand1, DOUBLE dOperand2)
{
if (dOperand1 < dOperand2)
{
return 1;
}
else
{
return 0;
}
}
/******************************************************************************
*
* Function:
* DoubleEqual
*
* Description:
* This function handle double comparison.
*
* Input:
* 1)Operand 1, 2)operand 2, 3)operator
*
* Output:
* Double
*
******************************************************************************/
DOUBLE DoubleEqual(DOUBLE dOperand1, DOUBLE dOperand2)
{
if (dOperand1 == dOperand2)
{
return 1;
}
else
{
return 0;
}
}
#endif
expfmt.c ///
/******************************************************************************
* *
* M O D U L E D E F I N E *
* *
******************************************************************************/
#define EXPFMT_C
/******************************************************************************
* *
* C O M P I L E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#include <ctype.h>
#include <stdio.h>
#include <string.h>
/******************************************************************************
* *
* U S E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#include "Common.h"
#include "XCore.h"
#include "exputil.h"
#include "expfmt.h"
/******************************************************************************
* *
* L O C A L D E F I N E S *
* *
******************************************************************************/
#if defined(TSPL)
/******************************************************************************
* *
* L O C A L T Y P E D E F S *
* *
******************************************************************************/
/******************************************************************************
* *
* L O C A L F U N C T I O N P R O T O T Y P E S *
* *
******************************************************************************/
/******************************************************************************
* *
* L O C A L I N I T I A L I Z E D D A T A D E F I N I T I O N S *
* *
******************************************************************************/
/******************************************************************************
* *
* L O C A L U N I T I A L I Z E D D A T A D E F I N I T I O N S *
* *
******************************************************************************/
STATIC INT FormatCompare(CONST CHAR *Format, CONST CHAR *Symbol, INT Size)
{
while (Size--)
{
if (toupper(*Format++) != toupper(*Symbol++))
return 0;
}
return 1;
}
STATIC VOID expFormatNumber(CHAR *Target, DOUBLE Numeric, CHAR *Format)
{
BOOL String;
/** User-defined numeric formats */
String = FALSE;
while (*Format)
{
/* Display the string inside the double quotation marks */
if (*Format == '"')
{
String = !String;
Format += 1;
}
else if (String)
{
*Target++ = *Format++;
}
/* Display the next character in the format string */
else if (*Format == '\\')
{
Format += 1;
if (*Format)
*Target++ = *Format++;
}
/* Sections separator */
else if (*Format == ';')
{
break;
}
else
{
*Target++ = *Format++;
}
}
*Target = '\0';
}
STATIC VOID expFormatDateTime(CHAR *Target, DATE Date, CHAR *Format, BOOL Use12)
{
STATIC CONST CHAR *MonthName[] = { "January","February","March","April","May","June",
"July","August","September","October","November","December" };
STATIC CONST CHAR *WeekName[] = { "Sunday","Monday","Tuesday",
"Wednesday","Thursday","Friday","Saturday" };
LONG Hour12 = (Date.Hour % 12) ? (Date.Hour % 12) : 12;
CHAR Prev, Last = 0;
BOOL String;
if (Date.DateUse == FALSE && Date.TimeUse == FALSE)
return;
/** User-defined date/time formats */
String = FALSE;
while (*Format)
{
/* Display the string inside the double quotation marks */
if (*Format == '"')
{
String = !String;
Format += 1;
continue;
}
else if (String)
{
*Target++ = *Format++;
continue;
}
/* Display the next character in the format string */
else if (*Format == '\\')
{
Format += 1;
if (*Format)
*Target++ = *Format++;
continue;
}
Prev = Last;
Last = tolower(*Format);
/* Sections separator */
if (*Format == ';')
{
break;
}
/* AMPM can be either uppercase or lowercase, but the case of the string
displayed matches the string as defined by your system settings */
else if (FormatCompare(Format, "AMPM", 4))
{
Target += sprintf(Target, "%s", (Date.Hour < 12) ? "AM" : "PM");
Format += 4;
}
/* Display a lowercase A with any hour before noon;
display a lowercase P with any hour between noon and 11:59 P.M. */
else if (FormatCompare(Format, "a/p", 3) && islower(*Format))
{
Target += sprintf(Target, "%c", (Date.Hour < 12) ? 'a' : 'p');
Format += 3;
}
/* Display an uppercase A with any hour before noon;
display an uppercase P with any hour between noon and 11:59 P.M. */
else if (FormatCompare(Format, "A/P", 3) && isupper(*Format))
{
Target += sprintf(Target, "%c", (Date.Hour < 12) ? 'A' : 'P');
Format += 3;
}
/* Display a lowercase AM with any hour before noon;
display a lowercase PM with any hour between noon and 11:59 P.M. */
else if (FormatCompare(Format, "am/pm", 5) && islower(*Format))
{
Target += sprintf(Target, "%s", (Date.Hour < 12) ? "am" : "pm");
Format += 5;
}
/* Display an uppercase AM with any hour before noon;
display an uppercase PM with any hour between noon and 11:59 P.M. */
else if (FormatCompare(Format, "AM/PM", 5) && isupper(*Format))
{
Target += sprintf(Target, "%s", (Date.Hour < 12) ? "AM" : "PM");
Format += 5;
}
/* Display a time as a complete time (including hour, minute, and second) */
else if (FormatCompare(Format, "ttttt", 5))
{
/* The default time format is h:mm:ss AM/PM */
Target += sprintf(Target, "%u:%02u:%02u %s",
Hour12, Date.Minute, Date.Second, (Date.Hour < 12) ? "AM" : "PM");
Format += 5;
}
/* Display the second as a number with leading zeros (00 - 59) */
else if (FormatCompare(Format, "ss", 2))
{
Target += sprintf(Target, "%02u", Date.Second);
Format += 2;
}
/* Display the second as a number without leading zeros (0 - 59) */
else if (FormatCompare(Format, "s", 1))
{
Target += sprintf(Target, "%u", Date.Second);
Format += 1;
}
/* Display the minute as a number with leading zeros (00 - 59) */
else if (FormatCompare(Format, "nn", 2))
{
Target += sprintf(Target, "%02u", Date.Minute);
Format += 2;
}
/* Display the minute as a number without leading zeros (0 - 59) */
else if (FormatCompare(Format, "n", 1))
{
Target += sprintf(Target, "%u", Date.Minute);
Format += 1;
}
/* Display the hour as a number with leading zeros (00 - 23) */
else if (FormatCompare(Format, "hh", 2))
{
Target += sprintf(Target, "%02u", Use12 ? Hour12 : Date.Hour);
Format += 2;
}
/* Display the hour as a number without leading zeros (0 - 23) */
else if (FormatCompare(Format, "h", 1))
{
Target += sprintf(Target, "%u", Use12 ? Hour12 : Date.Hour);
Format += 1;
}
/* Display the year as a 4-digit number (100 - 9999) */
else if (FormatCompare(Format, "yyyy", 4))
{
Target += sprintf(Target, "%04u", Date.Year);
Format += 4;
}
/* Display the year as a 2-digit number (00 - 99) */
else if (FormatCompare(Format, "yy", 2))
{
Target += sprintf(Target, "%02u", Date.Year % 100);
Format += 2;
}
/* Display the day of the year as a number (1 - 366) */
else if (FormatCompare(Format, "y", 1))
{
Target += sprintf(Target, "%u", Date.DayYear);
Format += 1;
}
/* Display the quarter of the year as a number (1 - 4) */
else if (FormatCompare(Format, "q", 1))
{
Target += sprintf(Target, "%u", (Date.Month + 2) / 3);
Format += 1;
}
/* Display the month as a full month name (January - December) */
else if (FormatCompare(Format, "mmmm", 4))
{
Target += sprintf(Target, "%s", MonthName[Date.Month - 1]);
Format += 4;
}
/* Display the month as an abbreviation (Jan - Dec) */
else if (FormatCompare(Format, "mmm", 3))
{
Target += sprintf(Target, "%c%c%c",
*(MonthName[Date.Month - 1] + 0),
*(MonthName[Date.Month - 1] + 1),
*(MonthName[Date.Month - 1] + 2));
Format += 3;
}
/* Display the month as a number with a leading zero (01 - 12) */
/* If mm immediately follows h or hh, the minute rather than the month is displayed */
else if (FormatCompare(Format, "mm", 2))
{
Target += sprintf(Target, "%02u", Prev == 'h' ? Date.Minute : Date.Month);
Format += 2;
}
/* Display the month as a number without a leading zero (1 - 12) */
/* If m immediately follows h or hh, the minute rather than the month is displayed */
else if (FormatCompare(Format, "m", 1))
{
Target += sprintf(Target, "%u", Prev == 'h' ? Date.Minute : Date.Month);
Format += 1;
}
/* Display the week of the year as a number (1 - 53) */
else if (FormatCompare(Format, "ww", 2))
{
Target += sprintf(Target, "%u", (Date.DayYear + 13 - Date.DayWeek) / 7);
Format += 2;
}
/* Display the day of the week as a number (1 for Sunday through 7 for Saturday) */
else if (FormatCompare(Format, "w", 1))
{
Target += sprintf(Target, "%u", Date.DayWeek);
Format += 1;
}
/* Display the date as a complete date (including day, month, and year),
formatted according to the long date setting recognized by your system */
else if (FormatCompare(Format, "dddddd", 6))
{
/* The default long date format is dddd, mmmm dd, yyyy */
Target += sprintf(Target, "%s, %s %02u %04u",
WeekName[Date.DayWeek - 1], MonthName[Date.Month - 1], Date.DayMonth, Date.Year);
Format += 6;
}
/* Display a date serial number as a complete date (including day, month, and year),
formatted according to your system's short date format setting */
else if (FormatCompare(Format, "ddddd", 5))
{
/* The default short date format is m/d/yyyy */
Target += sprintf(Target, "%u/%u/%04u", Date.Month, Date.DayMonth, Date.Year);
Format += 5;
}
/* Display the day as a full name (Sunday - Saturday) */
else if (FormatCompare(Format, "dddd", 4))
{
Target += sprintf(Target, "%s", WeekName[Date.DayWeek - 1]);
Format += 4;
}
/* Display the day as an abbreviation (Sun - Sat) */
else if (FormatCompare(Format, "ddd", 3))
{
Target += sprintf(Target, "%c%c%c",
*(WeekName[Date.DayWeek - 1] + 0),
*(WeekName[Date.DayWeek - 1] + 1),
*(WeekName[Date.DayWeek - 1] + 2));
Format += 3;
}
/* Display the day as a number with a leading zero (01 - 31) */
else if (FormatCompare(Format, "dd", 2))
{
Target += sprintf(Target, "%02u", Date.DayMonth);
Format += 2;
}
/* Display the day as a number without a leading zero (1 - 31) */
else if (FormatCompare(Format, "d", 1))
{
Target += sprintf(Target, "%u", Date.DayMonth);
Format += 1;
}
/* Display the date as ddddd and display the time as ttttt, in that order */
else if (FormatCompare(Format, "c", 1))
{
Target += expDateToString((BYTE *)Target, Date);
Format += 1;
}
else
{
*Target++ = *Format++;
Last = Prev;
}
}
*Target = '\0';
}
/******************************************************************************
*
* Function:
* expFormat
*
* Description:
* Returns a string formatted according to instructions contained in a format
* String expression.
*
* Input:
* Target - Pointer to target string and String expression
* Date - An expression representing the date/time
* Numeric - Floating-point expression
* Format - A valid named or user-defined format String expression
*
******************************************************************************/
VOID expFormat(CHAR *Target, DATE Date, DOUBLE Numeric, CHAR *Format)
{
CHAR *cp, *Section;
BOOL DateTime, Use12, String;
INT Separated;
if (strlen(Format) == 0)
return;
/** Predefined numeric formats */
/* Displays the number as entered, with no rounding and no commas */
if (FormatCompare(Format, "General Number", 15))
return;
/* Displays the number with a dollar sign, comma (if appropriate), and two digits
to the right of the decimal point. Shows negative numbers inside parentheses */
if (FormatCompare(Format, "Currency", 9))
strcpy(Format, "$#,##0.00;($#,##0.00)");
/* Displays the number with at least one digit to the left of the decimal
separator and two digits to the right. Does not show comma */
if (FormatCompare(Format, "Fixed", 6))
strcpy(Format, "0.00");
/* Displays the number with at least one digit to the left of the decimal
separator and two digits to the right and commas (if appropriate) */
if (FormatCompare(Format, "Standard", 9))
strcpy(Format, "#,##0.00");
/* Multiplies the value by 100 and displays the result with two digits
to the right of the decimal point and a percent sign at the end */
if (FormatCompare(Format, "Percent", 8))
strcpy(Format, "0.00%");
/* Uses standard scientific notation */
if (FormatCompare(Format, "Scientific", 11))
strcpy(Format, "0.00E+00");
/* Any nonzero numeric value is Yes. Zero is No */
if (FormatCompare(Format, "Yes/No", 7))
strcpy(Format, "\"Yes\";;\"No\"");
/* Any nonzero numeric value is True. Zero is False */
if (FormatCompare(Format, "True/False", 11))
strcpy(Format, "\"True\";;\"False\"");
/* Any nonzero numeric value is On. Zero is Off */
if (FormatCompare(Format, "On/Off", 7))
strcpy(Format, "\"On\";;\"Off\"");
/** Predefined date/time formats */
/* Shows date and time */
if (FormatCompare(Format, "General Date", 13))
strcpy(Format, "c");
/* Uses the Long Date format */
if (FormatCompare(Format, "Long Date", 10))
strcpy(Format, "dddddd");
/* Uses the dd-mmm-yy format */
if (FormatCompare(Format, "Medium Date", 12))
strcpy(Format, "dd-mmm-yy");
/* Uses the Short Date format */
if (FormatCompare(Format, "Short Date", 11))
strcpy(Format, "ddddd");
/* Shows the hour, minute, second, and "AM" or "PM" using the h:mm:ss format */
if (FormatCompare(Format, "Long Time", 10))
strcpy(Format, "ttttt");
/* Shows the hour, minute, and "AM" or "PM" using the "hh:mm AM/PM" format */
if (FormatCompare(Format, "Medium Time", 12))
strcpy(Format, "hh:mm AM/PM");
/* Shows the hour and minute using the hh:mm format */
if (FormatCompare(Format, "Short Time", 11))
strcpy(Format, "hh:mm");
/** Preparsing */
Separated = 0;
DateTime = FALSE;
Use12 = FALSE;
String = FALSE;
for (cp = Section = Format; *cp; cp++)
{
/* Display the string inside the double quotation marks */
if (*cp == '"')
{
String = !String;
}
else if (String == FALSE)
{
/* Use date and time formats */
if (FormatCompare(cp, ":", 1) ||
FormatCompare(cp, "/", 1) ||
FormatCompare(cp, "c", 1) ||
FormatCompare(cp, "d", 1) ||
FormatCompare(cp, "w", 1) ||
FormatCompare(cp, "m", 1) ||
FormatCompare(cp, "q", 1) ||
FormatCompare(cp, "y", 1) ||
FormatCompare(cp, "h", 1) ||
FormatCompare(cp, "n", 1) ||
FormatCompare(cp, "s", 1) ||
FormatCompare(cp, "ttttt", 5))
{
DateTime = TRUE;
}
/* Use the 12-hour clock */
if (FormatCompare(cp, "AMPM", 4) ||
FormatCompare(cp, "A/P", 3) ||
FormatCompare(cp, "AM/PM", 5))
{
DateTime = TRUE;
Use12 = TRUE;
}
/* Format can have from one to four sections separated by semicolons */
if (*cp == ';')
{
if (DateTime)
break;
if (++Separated >= 4)
break;
if ((Separated == 1 && Numeric < 0) || /* the second to negative values */
(Separated == 2 && Numeric == 0)) /* the third to zeros */
{
if (*(cp + 1) && *(cp + 1) != ';')
Section = cp + 1;
}
}
/* Display the next character in the format string */
else if (*cp == '\\')
cp++;
}
}
if (DateTime)
expFormatDateTime(Target, Date, Format, Use12);
else
expFormatNumber(Target, Numeric, Section);
}
/******************************************************************************
*
* Function:
* expDateAdd
*
* Description:
* The function returns a date to which a specified time interval has been added.
*
* Input:
* Interval - String expression representing the time interval you want to add
* Number - Numeric expression that is the number of intervals you want to add
* Date - Date value that you want to evaluate
*
* Output:
* If success return TRUE else FALSE
*
******************************************************************************/
BOOL expDateAdd(CHAR *Interval, INT Number, DATE *Date)
{
LONG SecAdd = 0;
LONG DayAdd = 0;
DOUBLE Value;
if (Date->DateUse == FALSE && Date->TimeUse == FALSE)
return FALSE;
if (FormatCompare(Interval, "yyyy", 5)) /* Year */
Date->Year += Number;
else if (FormatCompare(Interval, "q", 2)) /* Quarter */
Date->Month += Number * 3;
else if (FormatCompare(Interval, "m", 2)) /* Month */
Date->Month += Number;
else if (FormatCompare(Interval, "y", 2)) /* Day of year */
DayAdd += Number;
else if (FormatCompare(Interval, "d", 2)) /* Day of month */
DayAdd += Number;
else if (FormatCompare(Interval, "w", 2)) /* Day of week */
DayAdd += Number;
else if (FormatCompare(Interval, "ww", 3)) /* Week of year */
DayAdd += Number * 7;
else if (FormatCompare(Interval, "h", 2)) /* Hour */
SecAdd += Number * 3600;
else if (FormatCompare(Interval, "n", 2)) /* Minute */
SecAdd += Number * 60;
else if (FormatCompare(Interval, "s", 2)) /* Second */
SecAdd += Number;
else
return FALSE;
Value = expDateToDouble(*Date);
Value += (DOUBLE)DayAdd + (DOUBLE)SecAdd / SEC_PER_DAY;
*Date = expDoubleToDate(Value);
if (Date->DateUse == FALSE && Date->TimeUse == FALSE)
return FALSE;
return TRUE;
}
#endif
expfmt.h ///
#ifndef EXPFMT_H
/******************************************************************************
* *
* M O D U L E D E F I N E *
* *
******************************************************************************/
#define EXPFMT_H
/******************************************************************************
* *
* C O M P I L E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* U S E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* G L O B A L D E F I N E S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* S T R U C T U R E D E F I N I T I O N S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* G L O B A L V A R I A B L E S - N O I N I T I A L I Z E R S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* G L O B A L V A R I A B L E S - I N I T I A L I Z E R S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* F U N C T I O N P R O T O T Y P E S *
* *
******************************************************************************/
VOID expFormat(CHAR *, DATE, DOUBLE, CHAR *);
BOOL expDateAdd(CHAR *, INT, DATE *);
#endif
expmain.c
/******************************************************************************
* *
* M O D U L E D E F I N E *
* *
******************************************************************************/
#define EXPMAIN_C
/******************************************************************************
* *
* C O M P I L E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#include <string.h>
#include <stdio.h>
#include <ctype.h>
/******************************************************************************
* *
* U S E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#include "Common.h"
#include "..\ParserUtil.h"
#include "expr.h"
#include "exputil.h"
#include "exprpn.h"
#include "error.h"
/******************************************************************************
* *
* L O C A L D E F I N E S *
* *
******************************************************************************/
#if defined(TSPL)
/******************************************************************************
* *
* L O C A L T Y P E D E F S *
* *
******************************************************************************/
/******************************************************************************
* *
* L O C A L F U N C T I O N P R O T O T Y P E S *
* *
******************************************************************************/
VOID SeeOperator(VOID);
/******************************************************************************
* *
* L O C A L I N I T I A L I Z E D D A T A D E F I N I T I O N S *
* *
******************************************************************************/
/******************************************************************************
* *
* L O C A L U N I T I A L I Z E D D A T A D E F I N I T I O N S *
* *
******************************************************************************/
VOID InitialExp(VOID)
{
bExpKind = 0;
dRetVal = 0;
szRetStr[0] = 0;
szStatePtr = 0;
wErrorword = 0;
}
/******************************************************************************
*
* Function:
* expMain
*
* Description:
* This function expression main rountine.
*
* Input:
* Pointer to a pointer to null-terminated string
*
* Output:
* Null
*
******************************************************************************/
VOID expMain(BYTE **pszText)
{
szStatePtr += 1; //字符变量
pszStatement[szStatePtr] = pszText; // **Stack max 50
bCounterUse = FALSE;
wErrorword = 0;
switch (CheckExpressionType(*pszText)) /*Check the type of this token*/
{
case NUMERAL: /*If it is a numeral*/
PushOperator('\0'); /*Make a tag in RPN buffer*/
dRetVal = expDoubleMain(); /*Launch a double expression*/
bExpKind = DOUBLE_TYPE; /*This is double expression */
SeeOperator();
expDoubleToString(szRetStr, dRetVal);
break;
case STRING_TYPE: /*If it is a constant string*/
expStringMain(szRetStr); /*Launch a string expression*/
bExpKind = STRING_TYPE; /*This is string expression*/
break;
default:
szStatePtr = 0;
if (wErrorword == 0)
wErrorword = VARIABLE_NOT_DEFINED;
break;
}
szStatePtr -= 1;
}
/******************************************************************************
*
* Function:
* expReMain
*
* Description:
* This function re-enter expression main rountine.
*
* Input:
* Pointer to a pointer to null-terminated string
*
* Output:
* Null
*
******************************************************************************/
VOID expReMain(BYTE **pszText)
{
szStatePtr += 1;
pszStatement[szStatePtr] = pszText;
switch (CheckExpressionType(*pszText)) /*Check the type of this token*/
{
case NUMERAL: /*If it is a numeral*/
PushOperator('\0'); /*Make a tag in RPN buffer*/
dRetVal = expDoubleMain(); /*Launch a double expression*/
bExpKind = DOUBLE_TYPE; /*This is double expression */
SeeOperator();
expDoubleToString(szRetStr, dRetVal);
break;
case STRING_TYPE: /*If it is a constant string*/
expStringMain(szRetStr); /*Launch a string expression*/
bExpKind = STRING_TYPE; /*This is string expression*/
break;
default:
szStatePtr = 0;
if (wErrorword == 0)
wErrorword = SYNTAX_ERROR;
break;
}
szStatePtr -= 1;
}
#endif
expr.h ///
#ifndef EXPR_H
/******************************************************************************
* *
* M O D U L E D E F I N E *
* *
******************************************************************************/
#define EXPR_H
/******************************************************************************
* *
* C O M P I L E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* U S E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#include "..\ParserUtil.h"
#ifdef __cplusplus
extern "C" { /* Assume C declarations for C++ */
#endif /* __cplusplus */
/******************************************************************************
* *
* G L O B A L D E F I N E S *
* *
******************************************************************************/
#define FUNC_ABS 1
#define FUNC_ASC 2
#define FUNC_BARCODEPIXEL 3
#define FUNC_DATEADD 4
#define FUNC_EOF 5
#define FUNC_FPOS 6
#define FUNC_FRE 7
#define FUNC_GETKEY 8
#define FUNC_GETSENSOR 9
#define FUNC_INSTR 10
#define FUNC_INT 11
#define FUNC_LEN 12
#define FUNC_LOB 13
#define FUNC_LOC 14
#define FUNC_LOF 15
#define FUNC_NOW 16
#define FUNC_POS 17
#define FUNC_RND 18
#define FUNC_STRCOMP 19
#define FUNC_TEXTPIXEL 20
#define FUNC_VAL 21
#define FUNC_S_CHR 22
#define FUNC_S_DATEADD 23
#define FUNC_S_FORMAT 24
#define FUNC_S_FREAD 25
#define FUNC_S_GETSETTING 26
#define FUNC_S_INP 27
#define FUNC_S_LEFT 28
#define FUNC_S_LSTRIM 29
#define FUNC_S_LTRIM 30
#define FUNC_S_MID 31
#define FUNC_S_NOW 32
#define FUNC_S_RIGHT 33
#define FUNC_S_RSTRIM 34
#define FUNC_S_RTRIM 35
#define FUNC_S_SPC 36
#define FUNC_S_STR 37
#define FUNC_S_STRIM 38
#define FUNC_S_TRIM 39
#define FUNC_S_XOR 40
/******************************************************************************
* V A R I A B L E I D E N T I F I E R *
******************************************************************************/
#define FLOAT_TYPE 1
#define INTEGER_TYPE 2
#define STRING_TYPE 3
#define DOUBLE_TYPE 4
/******************************************************************************
* V A R I A B L E S U F F I X *
******************************************************************************/
#define INTEGER_ID '%'
#define FLOAT_ID '!'
#define DOUBLE_ID '#'
#define STRING_ID '$'
/******************************************************************************
* O P E R A N D I D E N T I F I E R *
******************************************************************************/
#define NUMERAL 1
#define FUNCTION 2
#define VARIABLE 3
#define PARENTHESIS 4
#define CSTRING 5
#define INTEGER_DIM 6
#define FLOAT_DIM 7
#define STRING_DIM 8
#define DOUBLE_DIM 9
/*----------------------------
common define const
----------------------------*/
#define NO FALSE
#define YES TRUE
#define SET TRUE
#define RESET FALSE
#define PASS TRUE
#define FAILURE FALSE
#define FAIL FALSE
#define ZERO 0
#define BLANK ' '
#define PS_STACK 50
/******************************************************************************
* *
* S T R U C T U R E D E F I N I T I O N S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* G L O B A L V A R I A B L E S - N O I N I T I A L I Z E R S *
* *
******************************************************************************/
#ifdef EXPMAIN_C
#define EXTERN
#else
#define EXTERN extern
#endif
EXTERN BYTE bExpKind; /*Expression kind identifier*/
EXTERN DOUBLE dRetVal; /*Floating point return value*/
EXTERN BYTE szRetStr[HUGE_LEN]; /*String return value*/
EXTERN BYTE szStatePtr;
EXTERN BYTE** pszStatement[PS_STACK];
EXTERN WORD wErrorword;
EXTERN BOOL bCounterUse;
/******************************************************************************
* *
* G L O B A L V A R I A B L E S - I N I T I A L I Z E R S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* F U N C T I O N P R O T O T Y P E S *
* *
******************************************************************************/
// expdoub.c
DOUBLE expDoubleMain(VOID);
// expstr.c
VOID expStringMain(BYTE *);
// expmain.c
VOID InitialExp(VOID);
VOID expMain(BYTE **);
VOID expReMain(BYTE **);
// exputil.c
BYTE CheckVariableType(BYTE *);
#undef EXTERN
#ifdef __cplusplus
} /* End of extern "C" { */
#endif /* __cplusplus */
#endif
exprpn.c
/******************************************************************************
* *
* M O D U L E D E F I N E *
* *
******************************************************************************/
#define EXPRESS_C
/******************************************************************************
* *
* C O M P I L E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#include <stdio.h>
#include <ctype.h>
/******************************************************************************
* *
* U S E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#include "Common.h"
#include "expr.h"
#include "exprpn.h"
/******************************************************************************
* *
* L O C A L D E F I N E S *
* *
******************************************************************************/
#if defined(TSPL)
/******************************************************************************
* *
* L O C A L T Y P E D E F S *
* *
******************************************************************************/
/******************************************************************************
* *
* L O C A L F U N C T I O N P R O T O T Y P E S *
* *
******************************************************************************/
/******************************************************************************
* *
* L O C A L I N I T I A L I Z E D D A T A D E F I N I T I O N S *
* *
******************************************************************************/
CONST BYTE aOperatorTbl[] = "+-\x2a/<>=;";
CONST BYTE aOperatorPrior[] = {3, 3, 4, 4, 2, 2, 2, 1};
/******************************************************************************
* *
* L O C A L U N I T I A L I Z E D D A T A D E F I N I T I O N S *
* *
******************************************************************************/
BYTE aOperator[MAX_OPERATOR_QTY]; /*operator buffer*/
DOUBLE aOperand[MAX_OPERAND_QTY]; /*operand buffer*/
BYTE bOperatorIdx; /*operator buffer index*/
BYTE bOperandIdx; /*operand buffer index*/
/******************************************************************************
*
* Function:
* InitializeRPN
*
* Description:
* This function initialize RPN buffer.
*
* Input:
* Null
*
* Output:
* Null.
*
******************************************************************************/
VOID InitializeRPN(VOID)
{
bOperatorIdx = 0; /*Reset operator buffer index*/
bOperandIdx = 0; /*Reset operand buffer index*/
}
/******************************************************************************
*
* Function:
* PushOperand
*
* Description:
* This function push operand into stack.
*
* Input:
* Pointer to operand
*
* Output:
* If succeed, return SUCCESS; else return FAIL.
*
******************************************************************************/
BYTE PushOperand(DOUBLE dOperand)
{
if (bOperandIdx >= MAX_OPERAND_QTY) /*If reach the end of buffer*/
{ /*return FAIL*/
return FAIL; /*else push the operand into*/
} /*buffer and return SUCCESS*/
else
{
aOperand[bOperandIdx] = dOperand;
bOperandIdx++;
return SUCCESS;
}
}
/******************************************************************************
*
* Function:
* PopOperand
*
* Description:
* This function pop operand from stack.
*
* Input:
* Pointer to operand
*
* Output:
* If succeed, return SUCCESS; else return FAIL.
*
******************************************************************************/
DOUBLE PopOperand(VOID)
{
if (bOperandIdx == 0) /*If reach the start of buffer*/
{ /*return FAIL*/
return FAIL; /*else pop the operand from*/
} /*buffer and return SUCCESS*/
else
{
bOperandIdx--;
return aOperand[bOperandIdx];
}
}
/******************************************************************************
*
* Function:
* PushOperator
*
* Description:
* This function push operator into stack.
*
* Input:
* Operator
*
* Output:
* If succeed, return SUCCESS; else return FAIL.
*
******************************************************************************/
BYTE PushOperator(BYTE bOperator)
{
if (bOperatorIdx >= MAX_OPERATOR_QTY) /*If reach the end of buffer*/
{ /*return FAIL*/
return FAIL; /*else push the operator into*/
} /*buffer and return SUCCESS*/
else
{
aOperator[bOperatorIdx] = bOperator;
bOperatorIdx++;
return SUCCESS;
}
}
/******************************************************************************
*
* Function:
* PopOperator
*
* Description:
* This function pop operator from stack.
*
* Input:
* Null
*
* Output:
* If succeed, return operator; else return NULL.
*
******************************************************************************/
BYTE PopOperator(VOID)
{
if (bOperatorIdx == 0) /*If reach the start of buffer*/
{ /*return FAIL*/
return ZERO; /*else pop the operand from*/
} /*buffer and return SUCCESS*/
else
{
bOperatorIdx--;
return aOperator[bOperatorIdx];
}
}
VOID SeeOperator(VOID)
{
bOperatorIdx--;
bOperatorIdx++;
return;
}
/******************************************************************************
*
* Function:
* PeekOperator
*
* Description:
* This function retrieve operator from RPN buffer without change indicator.
*
* Input:
* Null
*
* Output:
* If succeed, return operator; else return FAIL.
*
******************************************************************************/
BYTE PeekOperator(VOID)
{
if (bOperatorIdx == 0) /*If reach the start of buffer*/
{ /*return FAIL*/
return FAIL; /*else retrieve the operator from*/
} /*buffer and return SUCCESS*/
else
{
return aOperator[bOperatorIdx - 1];
}
}
/******************************************************************************
*
* Function:
* CheckPrior
*
* Description:
* This function check the priority of operator.
*
* Input:
* Operator
*
* Output:
* The priority of operator.
*
******************************************************************************/
BYTE CheckPrior(BYTE bOperator)
{
BYTE bOperatorSeq = 0; /*Initialize table indicator*/
while (bOperatorSeq < 8) /*Make sure within the table*/
{
if (bOperator == aOperatorTbl[bOperatorSeq]) /*If the input operator match*/
{ /*one in the table */
return aOperatorPrior[bOperatorSeq]; /*check priority table*/
} /*and return it*/
else /*else incease indicator*/
{
bOperatorSeq++;
}
}
return 0xff; /*If none is match, return 0xff*/
}
#endif
exprpn.h //
/******************************************************************************
*
* $Header: E:\WorkShop\017\exprpn.h_v 1.0 07 Sep 1998 11:54:32 Clay Lin $
* $Author: Clay Lin $
* $Date: 07 Sep 1998 11:54:32 $
* $Revision: 1.0 $
*
*-----------------------------------------------------------------------------
*
* Module:
* exprpn.h
*
* Language:
* ANSI Compliant
*
* Environment:
* ANSI Compliant
*
* Description:
* This modules lists the defines, macros, and structure definitions.
*
* Functions:
* N/A
*
*-----------------------------------------------------------------------------
*
* Edit History:
*
* Rev 1.0 07 Sep 1998 11:54:32 Clay Lin
* Initial revision.
*
******************************************************************************/
#ifndef EXPRPN_H
/******************************************************************************
* *
* M O D U L E D E F I N E *
* *
******************************************************************************/
#define EXPRPN_H
/******************************************************************************
* *
* C O M P I L E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* U S E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* G L O B A L D E F I N E S *
* *
******************************************************************************/
#define MAX_OPERAND_QTY 10 /*Operand buffer size*/
#define MAX_OPERATOR_QTY 10 /*Operator buffer size*/
/******************************************************************************
* *
* S T R U C T U R E D E F I N I T I O N S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* G L O B A L V A R I A B L E S - N O I N I T I A L I Z E R S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* G L O B A L V A R I A B L E S - I N I T I A L I Z E R S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* F U N C T I O N P R O T O T Y P E S *
* *
******************************************************************************/
VOID InitializeRPN(VOID);
BYTE PushOperand(DOUBLE);
DOUBLE PopOperand(VOID);
BYTE PushOperator(BYTE);
BYTE PopOperator(VOID);
BYTE PeekOperator(VOID);
BYTE CheckPrior(BYTE);
#endif