int treatmnt_readExpression(char* tok[], int ntoks) // // Input: tok[] = array of string tokens // ntoks = number of tokens // Output: returns an error code // Purpose: reads a treatment expression from a tokenized line of input. // { char s[MAXLINE+1]; char* expr; int i, j, k, p; MathExpr* equation; // ptr. to a math. expression //(5.0.010 - LR) // --- retrieve node & pollutant if ( ntoks < 3 ) return error_setInpError(ERR_ITEMS, ""); j = project_findObject(NODE, tok[0]); if ( j < 0 ) return error_setInpError(ERR_NAME, tok[0]); p = project_findObject(POLLUT, tok[1]); if ( p < 0 ) return error_setInpError(ERR_NAME, tok[1]); // --- concatenate remaining tokens into a single string strcpy(s, tok[2]); for ( i=3; i<ntoks; i++) { strcat(s, " "); strcat(s, tok[i]); } // --- check treatment type if ( UCHAR(s[0]) == 'R' ) k = 0; else if ( UCHAR(s[0]) == 'C' ) k = 1; else return error_setInpError(ERR_KEYWORD, tok[2]); // --- start treatment expression after equals sign expr = strchr(s, '='); if ( expr == NULL ) return error_setInpError(ERR_KEYWORD, ""); else expr++; // --- create treatment objects at node j if they don't already exist if ( Node[j].treatment == NULL ) { if ( !createTreatment(j) ) return error_setInpError(ERR_MEMORY, ""); } // --- create a parsed expression tree from the string expr // (getVariableIndex is the function that converts a treatment // variable's name into an index number) equation = mathexpr_create(expr, getVariableIndex); if ( equation == NULL ) return error_setInpError(ERR_TREATMENT_EXPR, ""); // --- save the treatment parameters in the node's treatment object Node[j].treatment[p].treatType = k; Node[j].treatment[p].equation = equation; return 0; }
int gwater_readFlowExpression(Project* project, char* tok[], int ntoks) // // Input: tok[] = array of string tokens // ntoks = number of tokens // Output: returns error code // Purpose: reads mathematical expression for lateral or deep groundwater // flow for a subcatchment from a line of input data. // // Format is: subcatch LATERAL/DEEP <expr> // where subcatch is the ID of the subcatchment, LATERAL is for lateral // project->GW flow, DEEP is for deep project->GW flow and <expr> is any well-formed math // expression. // { int i, j, k; char exprStr[MAXLINE+1]; MathExpr* expr; // --- return if too few tokens if ( ntoks < 3 ) return error_setInpError(ERR_ITEMS, ""); // --- check that subcatchment exists j = project_findObject(project, SUBCATCH, tok[0]); if ( j < 0 ) return error_setInpError(ERR_NAME, tok[0]); // --- check if expression is for lateral or deep project->GW flow k = 1; if ( match(tok[1], "LAT") ) k = 1; else if ( match(tok[1], "DEEP") ) k = 2; else return error_setInpError(ERR_KEYWORD, tok[1]); // --- concatenate remaining tokens into a single string strcpy(exprStr, tok[2]); for ( i = 3; i < ntoks; i++) { strcat(exprStr, " "); strcat(exprStr, tok[i]); } // --- delete any previous flow eqn. if ( k == 1 ) mathexpr_delete(project->Subcatch[j].gwLatFlowExpr); else mathexpr_delete(project->Subcatch[j].gwDeepFlowExpr); // --- create a parsed expression tree from the string expr // (getVariableIndex is the function that converts a project->GW // variable's name into an index number) expr = mathexpr_create(project,exprStr, getVariableIndex); if ( expr == NULL ) return error_setInpError(ERR_TREATMENT_EXPR, ""); // --- save expression tree with the subcatchment if ( k == 1 ) project->Subcatch[j].gwLatFlowExpr = expr; else project->Subcatch[j].gwDeepFlowExpr = expr; return 0; }