예제 #1
0
파일: rules.c 프로젝트: sethdford/rules
static PyObject *pyRetractFacts(PyObject *self, PyObject *args) {
    void *handle;
    char *facts;
    if (!PyArg_ParseTuple(args, "ls", &handle, &facts)) {
        PyErr_SetString(RulesError, "pyAssertFacts Invalid argument");
        return NULL;
    }

    unsigned int *results = NULL;
    unsigned int resultsLength;
    unsigned int result = retractFacts(handle, facts, &resultsLength, &results);
    if (result == RULES_OK) {   
        if (results) {
            free(results);
        }
        return Py_BuildValue("i", resultsLength);
    } else {
        if (result == ERR_OUT_OF_MEMORY) {
            PyErr_NoMemory();
        } else {
            if (results) {
                free(results);
            } 
            char *message;
            if (asprintf(&message, "Could not retract facts, error code: %d", result)) {
                PyErr_NoMemory();
            } else {
                PyErr_SetString(RulesError, message);
                free(message);
            }
        }
        return NULL;
    }
}
예제 #2
0
파일: rules.c 프로젝트: sureddy/rules
static VALUE rbRetractFacts(VALUE self, VALUE handle, VALUE facts) {
    Check_Type(handle, T_FIXNUM);
    Check_Type(facts, T_STRING);

    unsigned int *results = NULL;
    unsigned int resultsLength = 0;
    unsigned int result = retractFacts((void *)FIX2LONG(handle), RSTRING_PTR(facts), &resultsLength, &results);
    if (result == RULES_OK) {
        if (results) {
            free(results);
        }
        return INT2FIX(resultsLength);   
    } else {
        if (result == ERR_OUT_OF_MEMORY) {
            rb_raise(rb_eNoMemError, "Out of memory");
        } else { 
            if (results) {
                free(results);
            }
            rb_raise(rb_eException, "Could not retract facts, error code: %d", result);
        }
    }

    return Qnil;
}