Exemplo n.º 1
0
DBInt DBMiscTrimQuotes(char *str) {
    DBInt i, quoteNum = 0, strLen = strlen(str);

    DBMiscTrimWhiteSpaces(str);
    if (((str[0] != '\"') || (str[strLen - 1] != '\"')) &&
        ((str[0] != '\'') || (str[strLen - 1] != '\'')))
        return (quoteNum);

    for (i = 0; i < strLen - 2; ++i) str[i] = str[i + 1];
    str[i] = '\0';
    return (DBMiscTrimQuotes(str) + 1);
}
Exemplo n.º 2
0
DBInt DBMathOperand::Expand(DBObjectLIST<DBObject> *variables) {
    char *expression = Var.String, *exprPtr;
    DBInt i, strLen = expression != (char *) NULL ? (int) strlen(expression) : 0, bracketNum = 0;
    DBInt operPos = DBFault, oper, operLen, nonWhite;
    long intVal;
    double floatVal;
    void *function;

    if (expression == (char *) NULL) return (DBFault);

    for (i = 0; i < strLen; ++i) {
        if (expression[i] == '(') {
            bracketNum = 0;
            for (; i < strLen; ++i) {
                if (expression[i] == '(') bracketNum++;
                if (expression[i] == ')') {
                    bracketNum--;
                    if (bracketNum == 0) break;
                }
            }
            if (i == strLen) return (DBFault);
        }
        if (strncmp(expression + i, " ? ", 3) == 0) {
            DBMathExpression *expr;
            DBInt condPos = i + 1, condNum;

            condNum = 0;
            for (; i < strLen; ++i) {
                if (strncmp(expression + i, " ? ", 3) == 0) {
                    condNum++;
                    i += 2;
                }
                if (strncmp(expression + i, " : ", 3) == 0) {
                    condNum--;
                    if (condNum == 0) break;
                }
            }
            if (i == strLen) return (DBFault);

            OprTypeVAR = DBMathOperandCond;
            expression[condPos] = '\0';
            expression[i + 1] = '\0';
            i += 2;
//			CMmsgPrint (CMmsgDebug,"if %s then %s else %s",expression,expression + condPos + 1,expression + i + 1);
            expr = new DBMathExpression(expression + condPos + 1, expression + i + 1);
            if (expr->Expand(variables) == DBFault) return (DBFault);
            Var.ExpPTR = new DBMathExpression(expression, expr);
            return (Var.ExpPTR->Expand(variables));
        }
    }

    nonWhite = false;
    for (i = 0; i < strLen; ++i) {
        if (expression[i] == '(') {
            bracketNum = 0;
            for (; i < strLen; ++i) {
                if (expression[i] == '(') bracketNum++;
                if (expression[i] == ')') {
                    bracketNum--;
                    if (bracketNum == 0) break;
                }
            }
            if (i == strLen) return (DBFault);
        }
        if (((oper = _DBMathOperator(expression + i, &operLen)) != DBFault) && nonWhite) {
            if (operPos == DBFault) operPos = i;
            else { if (_DBMathOperator(expression + operPos) <= oper) operPos = i; }
            for (; operLen > 1; --operLen) ++i;
            nonWhite = false;
        }
        else if (DBMiscTestWhiteChar(expression[i]) != true) nonWhite = true;
    }

    if (operPos != DBFault) {
        OprTypeVAR = DBMathOperandExpr;
        VarTypeVAR = DBFault;

        oper = _DBMathOperator(expression + operPos, &operLen);
        expression[operPos] = '\0';
        for (; operLen > 1; --operLen) ++operPos;
//		CMmsgPrint (CMmsgDebug,"%s and %s", expression, expression + operPos + 1);
        Var.ExpPTR = new DBMathExpression(expression, expression + operPos + 1, oper);
        free(expression);
        return (Var.ExpPTR->Expand(variables));
    }

    if ((function = _DBMathFunction(expression, &operPos)) != (void *) NULL) {
        OprTypeVAR = DBMathOperandExpr;
        VarTypeVAR = DBFault;

        expression[operPos] = '\0';
//		CMmsgPrint (CMmsgDebug,"%s of %s", expression, expression + operPos + 1);
        Var.ExpPTR = new DBMathExpression(expression + operPos + 1, DBVariableFloat, function);
        free(expression);
        return (Var.ExpPTR->Expand(variables));
    }

    intVal = strtol(expression, &exprPtr, 10);
    if (expression + strlen(expression) == exprPtr) {
        OprTypeVAR = DBMathOperandConst;
        VarTypeVAR = DBVariableInt;

        Var.Int = intVal;
//		CMmsgPrint (CMmsgDebug,"Constant integer: %d",(int) intVal);
        free(expression);
        return (DBSuccess);
    }

    floatVal = strtod(expression, &exprPtr);
    if (expression + strlen(expression) == exprPtr) {
        OprTypeVAR = DBMathOperandConst;
        VarTypeVAR = DBVariableFloat;

        Var.Float = floatVal;
//		CMmsgPrint (CMmsgDebug,"Constant float: %f",floatVal);
        free(expression);
        return (DBSuccess);
    }

    if (DBMiscTrimQuotes(expression) > 0) {
        floatVal = strtod(expression, &exprPtr);
        if (expression + strlen(expression) != exprPtr) {
            OprTypeVAR = DBMathOperandConst;
            VarTypeVAR = DBVariableString;
//			CMmsgPrint (CMmsgDebug,"Constant string: %s",expression);
            if ((Var.String = (char *) malloc(strlen(expression) + 1)) == (char *) NULL) {
                CMmsgPrint(CMmsgSysError, "Memory allocation error in: %s %d", __FILE__, __LINE__);
                return (DBFault);
            }
            strcpy(Var.String, expression);
            free(expression);
            return (DBSuccess);
        }
    }

    if (strcmp(expression, "M_PI") == 0) {
        OprTypeVAR = DBMathOperandConst;
        VarTypeVAR = DBVariableFloat;

        Var.Float = M_PI;
//		CMmsgPrint (CMmsgDebug,"Constant float: %f",floatVal);
        free(expression);
        return (DBSuccess);
    }

    if (strcmp(expression, "M_E") == 0) {
        OprTypeVAR = DBMathOperandConst;
        VarTypeVAR = DBVariableFloat;

        Var.Float = M_E;
//		CMmsgPrint (CMmsgDebug,"Constant float: %f",floatVal);
        free(expression);
        return (DBSuccess);
    }

    if (strcmp(expression, "nodata") == 0) {
        OprTypeVAR = DBMathOperandConst;
        VarTypeVAR = DBVariableInt;

        Var.Int = DBDefaultMissingIntVal;
//		CMmsgPrint (CMmsgDebug,"Nodata");
        free(expression);
        return (DBSuccess);
    }

    if (strcmp(expression, "true") == 0) {
        OprTypeVAR = DBMathOperandConst;
        VarTypeVAR = DBVariableInt;

        Var.Int = true;
//		CMmsgPrint (CMmsgDebug,"True");
        free(expression);
        return (DBSuccess);
    }

    if (strcmp(expression, "rowID") == 0) {
        OprTypeVAR = DBMathOperandRowID;
        VarTypeVAR = DBVariableInt;

        Var.Int = 1;
//		CMmsgPrint (CMmsgDebug,"True");
        free(expression);
        return (DBSuccess);
    }

    if (strcmp(expression, "false") == 0) {
        OprTypeVAR = DBMathOperandConst;
        VarTypeVAR = DBVariableInt;

        Var.Int = false;
//		CMmsgPrint (CMmsgDebug,"False");
        free(expression);
        return (DBSuccess);
    }

    OprTypeVAR = DBMathOperandVar;
    VarTypeVAR = DBFault;
    if ((Var.ObjPTR = variables->Item(expression)) == (DBObject *) NULL) {
        Var.ObjPTR = new DBObject(expression);
        variables->Add(Var.ObjPTR);
    }

//	CMmsgPrint (CMmsgDebug,"Variable: %s(%d)",expression,Var.ObjPTR->RowID ());
    free(expression);
    return (DBSuccess);
}