1 /*---------------------------------------------------------------------------* 2 * ExpressionEvaluator.c * 3 * * 4 * Copyright 2007, 2008 Nuance Communciations, Inc. * 5 * * 6 * Licensed under the Apache License, Version 2.0 (the 'License'); * 7 * you may not use this file except in compliance with the License. * 8 * * 9 * You may obtain a copy of the License at * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, software * 13 * distributed under the License is distributed on an 'AS IS' BASIS, * 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 15 * See the License for the specific language governing permissions and * 16 * limitations under the License. * 17 * * 18 *---------------------------------------------------------------------------*/ 19 20 #include "SR_ExpressionEvaluator.h" 21 #include "LCHAR.h" 22 #include "plog.h" 23 24 25 26 //static const char* MTAG = __FILE__; 27 28 29 ESR_ReturnCode EE_Init(ExpressionEvaluator** self) 30 { 31 return ESR_SUCCESS; 32 } 33 34 ESR_ReturnCode EE_Free(ExpressionEvaluator* self) 35 { 36 return ESR_SUCCESS; 37 } 38 39 ESR_ReturnCode EE_concat(LCHAR* name, LCHAR** operands, size_t opCount, void* data, LCHAR* resultBuf, size_t* resultLen) 40 { 41 size_t i, opLen; 42 ESR_ReturnCode rc; 43 44 if (operands == NULL || resultBuf == NULL || resultLen == NULL) 45 { 46 PLogError(L("ESR_INVALID_ARGUMENT")); 47 return ESR_INVALID_ARGUMENT; 48 } 49 *resultLen = 0; 50 for (i = 0; i < opCount; ++i) 51 { 52 opLen = LSTRLEN(operands[i]); 53 MEMCHK(rc, (*resultLen + opLen), MAX_STRING_LEN); 54 LSTRCAT(resultBuf, operands[i]); 55 *resultLen += opLen; 56 } 57 return ESR_SUCCESS; 58 CLEANUP: 59 return rc; 60 } 61 62 ESR_ReturnCode EE_conditional(LCHAR* name, LCHAR** operands, size_t opCount, void* data, LCHAR* resultBuf, size_t* resultLen) 63 { 64 if (operands == NULL || resultBuf == NULL || resultLen == NULL) 65 { 66 PLogError(L("ESR_INVALID_ARGUMENT")); 67 return ESR_INVALID_ARGUMENT; 68 } 69 70 if (!LSTRCMP(operands[0], UNDEFINED_SYMBOL) || !operands[0] || 71 !LSTRCMP(operands[0], FALSE_SYMBOL)) 72 { 73 if (strlen(operands[2]) >= *resultLen) 74 { 75 PLogError("EE_conditional overflow error %d<%d\n", *resultLen, strlen(operands[2])); 76 *resultLen = strlen(operands[2]); 77 return ESR_BUFFER_OVERFLOW; 78 } 79 LSTRCPY(resultBuf, operands[2]); 80 } 81 else 82 { 83 if (strlen(operands[1]) >= *resultLen) 84 { 85 PLogError("EE_conditional overflow error %d<%d\n", *resultLen, strlen(operands[1])); 86 *resultLen = strlen(operands[1]); 87 return ESR_BUFFER_OVERFLOW; 88 } 89 LSTRCPY(resultBuf, operands[1]); 90 } 91 *resultLen = LSTRLEN(resultBuf); 92 return ESR_SUCCESS; 93 } 94 95 96 ESR_ReturnCode EE_add(LCHAR* name, LCHAR** operands, size_t opCount, void* data, LCHAR* resultBuf, size_t* resultLen) 97 { 98 size_t i, sum; 99 100 if (operands == NULL || resultBuf == NULL || resultLen == NULL) 101 { 102 PLogError(L("ESR_INVALID_ARGUMENT")); 103 return ESR_INVALID_ARGUMENT; 104 } 105 sum = 0; 106 for (i = 0; i < opCount; ++i) 107 sum += atoi(operands[i]); 108 109 return litostr(sum, resultBuf, resultLen, BASE_10); 110 } 111 112 ESR_ReturnCode EE_subtract(LCHAR* name, LCHAR** operands, size_t opCount, void* data, LCHAR* resultBuf, size_t* resultLen) 113 { 114 size_t i; 115 int diff; 116 117 if (operands == NULL || resultBuf == NULL || resultLen == NULL) 118 { 119 PLogError(L("ESR_INVALID_ARGUMENT")); 120 return ESR_INVALID_ARGUMENT; 121 } 122 diff = atoi(operands[0]); 123 for (i = 1; i < opCount; ++i) 124 diff -= atoi(operands[i]); 125 126 return litostr(diff, resultBuf, resultLen, BASE_10); 127 } 128