msEvalExpression() * * Evaluates a mapserver expression for a given set of attribute values and * returns the result of the expression (MS_TRUE or MS_FALSE) * May also return MS_FALSE in case of parsing errors or invalid expressions * (check the error stack if you care) * */
| 407 | * |
| 408 | */ |
| 409 | int msEvalExpression(layerObj *layer, shapeObj *shape, expressionObj *expression, int itemindex) |
| 410 | { |
| 411 | if(!expression->string) return MS_TRUE; /* empty expressions are ALWAYS true */ |
| 412 | |
| 413 | switch(expression->type) { |
| 414 | case(MS_STRING): |
| 415 | if(itemindex == -1) { |
| 416 | msSetError(MS_MISCERR, "Cannot evaluate expression, no item index defined.", "msEvalExpression()"); |
| 417 | return MS_FALSE; |
| 418 | } |
| 419 | if(itemindex >= layer->numitems) { |
| 420 | msSetError(MS_MISCERR, "Invalid item index.", "msEvalExpression()"); |
| 421 | return MS_FALSE; |
| 422 | } |
| 423 | if(expression->flags & MS_EXP_INSENSITIVE) { |
| 424 | if(strcasecmp(expression->string, shape->values[itemindex]) == 0) return MS_TRUE; /* got a match */ |
| 425 | } else { |
| 426 | if(strcmp(expression->string, shape->values[itemindex]) == 0) return MS_TRUE; /* got a match */ |
| 427 | } |
| 428 | break; |
| 429 | case(MS_EXPRESSION): |
| 430 | { |
| 431 | int status; |
| 432 | parseObj p; |
| 433 | |
| 434 | p.shape = shape; |
| 435 | p.expr = expression; |
| 436 | p.expr->curtoken = p.expr->tokens; /* reset */ |
| 437 | p.type = MS_PARSE_TYPE_BOOLEAN; |
| 438 | |
| 439 | status = yyparse(&p); |
| 440 | |
| 441 | if (status != 0) { |
| 442 | msSetError(MS_PARSEERR, "Failed to parse expression: %s", "msEvalExpression", expression->string); |
| 443 | return MS_FALSE; |
| 444 | } |
| 445 | |
| 446 | return p.result.intval; |
| 447 | break; |
| 448 | } |
| 449 | case(MS_REGEX): |
| 450 | if(itemindex == -1) { |
| 451 | msSetError(MS_MISCERR, "Cannot evaluate expression, no item index defined.", "msEvalExpression()"); |
| 452 | return MS_FALSE; |
| 453 | } |
| 454 | if(itemindex >= layer->numitems) { |
| 455 | msSetError(MS_MISCERR, "Invalid item index.", "msEvalExpression()"); |
| 456 | return MS_FALSE; |
| 457 | } |
| 458 | |
| 459 | if(!expression->compiled) { |
| 460 | if(expression->flags & MS_EXP_INSENSITIVE) { |
| 461 | if(ms_regcomp(&(expression->regex), expression->string, MS_REG_EXTENDED|MS_REG_NOSUB|MS_REG_ICASE) != 0) { /* compile the expression */ |
| 462 | msSetError(MS_REGEXERR, "Invalid regular expression.", "msEvalExpression()"); |
| 463 | return MS_FALSE; |
| 464 | } |
| 465 | } else { |
| 466 | if(ms_regcomp(&(expression->regex), expression->string, MS_REG_EXTENDED|MS_REG_NOSUB) != 0) { /* compile the expression */ |
no test coverage detected