static int start(void *d) { plugin_data_t *data = d; cp_extension_t **exts; exts = cp_get_extensions_info(data->ctx, "symuser.strings", NULL, NULL); if (exts != NULL && exts[0] != NULL) { const char *symname; symname = cp_lookup_cfg_value(exts[0]->configuration, "@string-symbol"); if (symname != NULL) { data->str = cp_resolve_symbol(data->ctx, exts[0]->plugin->identifier, symname, NULL); if (data->str == NULL) { cp_log(data->ctx, CP_LOG_ERROR, "Could not resolve symbol specified by extension."); } } else { cp_log(data->ctx, CP_LOG_ERROR, "No string-symbol attribute present in extension."); } } else { cp_log(data->ctx, CP_LOG_ERROR, "No extensions available."); } if (exts != NULL) { cp_release_info(data->ctx, exts); } if (data->str == NULL) { return CP_ERR_RUNTIME; } return cp_define_symbol(data->ctx, "used_string", (void *) data->str); }
/** * Returns whether the specified file is of the type matching the specified * file-type element. */ static int is_of_type(const char *path, const cp_cfg_element_t *type) { int i; int iot = 0; /* Go through all extensions specified for the type */ for (i = 0; !iot && i < type->num_children; i++) { cp_cfg_element_t *ee = type->children + i; const char *ext; iot = (strcmp(ee->name, "file-extension") == 0 && (ext = cp_lookup_cfg_value(ee, "@ext")) != NULL && strlen(path) >= strlen(ext) && strcmp(path + (strlen(path) - strlen(ext)), ext) == 0); } return iot; }
/** * Classifies a file based on file extension. This classifier uses extensions * installed at the file type extension point. Therefore we need pointer to * the plug-in context to access the extensions. A plug-in instance initializes * the classifier structure with the plug-in context pointer and registers a * virtual symbol pointing to the classifier. */ static int classify(void *d, const char *path) { cp_context_t *ctx = d; cp_extension_t **exts; const char *type = NULL; int i; // Go through all extensions registered at the extension point exts = cp_get_extensions_info(ctx, "org.c-pluff.examples.cpfile.extension.file-types", NULL, NULL); if (exts == NULL) { cp_log(ctx, CP_LOG_ERROR, "Could not resolve file type extensions."); return 0; } for (i = 0; type == NULL && exts[i] != NULL; i++) { int j; // Go through all file types provided by the extension for (j = 0; type == NULL && j < exts[i]->configuration->num_children; j++) { cp_cfg_element_t *elem = exts[i]->configuration->children + j; const char *desc = NULL; if (strcmp(elem->name, "file-type") == 0 && (desc = cp_lookup_cfg_value(elem, "@description")) != NULL && (is_of_type(path, elem))) { type = desc; } } } // Release extension information cp_release_info(ctx, exts); // Print file type if recognized, otherwise try other classifiers if (type != NULL) { fputs(type, stdout); putchar('\n'); return 1; } else { return 0; } }
void extcfgutils(void) { cp_context_t *ctx; cp_plugin_info_t *plugin; cp_extension_t *ext; cp_cfg_element_t *ce, *cebase; const char *str; int errors; cp_status_t status; int i; ctx = init_context(CP_LOG_ERROR, &errors); check((plugin = cp_load_plugin_descriptor(ctx, plugindir("maximal"), &status)) != NULL && status == CP_OK); for (i = 0, ext = NULL; ext == NULL && i < plugin->num_extensions; i++) { cp_extension_t *e = plugin->extensions + i; if (e->identifier != NULL && !strcmp(e->local_id, "ext1")) { ext = e; } } check(ext != NULL); // Look up using forward path check((ce = cp_lookup_cfg_element(ext->configuration, "structure/parameter")) != NULL && ce->value != NULL && strcmp(ce->value, "parameter") == 0); check((ce = cebase = cp_lookup_cfg_element(ext->configuration, "structure/deeper/struct/is")) != NULL && ce->value != NULL && strcmp(ce->value, "here") == 0); check((str = cp_lookup_cfg_value(ext->configuration, "structure/parameter")) != NULL && strcmp(str, "parameter") == 0); check((str = cp_lookup_cfg_value(ext->configuration, "@name")) != NULL && strcmp(str, "Extension 1") == 0); // Look up using reverse path check((ce = cp_lookup_cfg_element(cebase, "../../../parameter/../deeper")) != NULL && strcmp(ce->name, "deeper") == 0); check((str = cp_lookup_cfg_value(cebase, "../../../../@name")) != NULL && strcmp(str, "Extension 1") == 0); // Look up nonexisting components check(cp_lookup_cfg_element(ext->configuration, "non/existing") == NULL); check(cp_lookup_cfg_element(ext->configuration, "structure/../..") == NULL); check(cp_lookup_cfg_value(ext->configuration, "non/existing") == NULL); check(cp_lookup_cfg_value(ext->configuration, "structure/../..") == NULL); check(cp_lookup_cfg_value(ext->configuration, "structure@nonexisting") == NULL); cp_release_info(ctx, plugin); cp_destroy_context(ctx); check(errors == 0); }