static int f_eval_num_attr(lf_obj_handle_t ohandle, void *fdata) { int i; const void * data; size_t dsize; float val; fnum_attr_holder_t * fholder = (fnum_attr_holder_t *) fdata; fnum_attr_t * fconfig; int err; for (i = 0; i<fholder->num_configs; i++) { fconfig = &fholder->fconfigs[i]; err = lf_ref_attr(ohandle, fconfig->attr_name, &dsize, &data); if (err) { if (fconfig->drop_missing) { return(0); } } else { val = atof((char *)data); if ((val < fconfig->min_value) || (val > fconfig->max_value)) { return(0); } } } return(1); }
static int f_eval_xquery (lf_obj_handle_t ohandle, void *filter_args) { XQQuery *query = ((struct ctx *) filter_args)->query; XQQuery *post_query = ((struct ctx *) filter_args)->post_query; // create context objects AutoDelete<DynamicContext> context(query->createDynamicContext()); AutoDelete<DynamicContext> post_context(post_query->createDynamicContext()); // slurp in the entire object size_t len; const void *data; lf_ref_attr(ohandle, "", &len, &data); // parse the document, set it as context item xercesc::MemBufInputSource input_source((const XMLByte *) data, len, X("diamond")); Node::Ptr doc = context->parseDocument(input_source); context->setContextItem(doc); context->setContextPosition(1); context->setContextSize(1); // execute user query Result result = query->execute(context); // convert into diamond attributes, by executing our "post_query" post_context->setContextItem(result->toSequence(context).first()); post_context->setContextPosition(1); post_context->setContextSize(1); bool settingName = true; char *attributeName = NULL; try { Result post_result = post_query->execute(post_context); Item::Ptr item; while(item = post_result->next(post_context)) { char *str = strdup(UTF8(item->asString(post_context))); if (settingName) { attributeName = strdup(str); } else { //std::cout << "writing attribute '" << attributeName << "':'" << str << "'" << std::endl; lf_write_attr(ohandle, attributeName, strlen(str) + 1, (unsigned char *) str); free(attributeName); } free(str); settingName = !settingName; } } catch(XQException &e) { std::cerr << "XQException: " << UTF8(e.getError()) << std::endl; return 0; } return 1; }
int f_eval_string (lf_obj_handle_t ohandle, void *filter_args) { context_t *ctx = (context_t *) filter_args; const char *target_str = ctx->target_str; int result = 0; // slurp in the object size_t len; const void *data; lf_ref_attr(ohandle, "", &len, &data); char *source = malloc(len + 1); if (source == NULL) { return 0; } memcpy(source, data, len); source[len] = '\0'; char *strstr_result = strstr(source, target_str); if (strstr_result != NULL) { // found, get index int index = strstr_result - source; // put into attribute char *index_str; asprintf(&index_str, "%d", index); lf_write_attr(ohandle, "string-index", strlen(index_str) + 1, (unsigned char *) index_str); free(index_str); result = 1; } free(source); return result; }