/* * Open a handle to the named brand. * * Returns a handle to the named brand, which is used for all subsequent brand * interaction, or NULL if unable to open or initialize the brand. */ brand_handle_t brand_open(const char *name) { struct brand_handle *bhp; char path[MAXPATHLEN]; xmlNodePtr node; xmlChar *property; struct stat statbuf; /* * Make sure brand name isn't too long */ if (strlen(name) >= MAXNAMELEN) return (NULL); /* * Check that the brand exists */ (void) snprintf(path, sizeof (path), "%s/%s", BRAND_DIR, name); if (stat(path, &statbuf) != 0) return (NULL); /* * Allocate brand handle */ if ((bhp = malloc(sizeof (struct brand_handle))) == NULL) return (NULL); bzero(bhp, sizeof (struct brand_handle)); (void) strcpy(bhp->bh_name, name); /* * Open the configuration file */ (void) snprintf(path, sizeof (path), "%s/%s/%s", BRAND_DIR, name, BRAND_CONFIG); if ((bhp->bh_config = open_xml_file(path)) == NULL) { brand_close((brand_handle_t)bhp); return (NULL); } /* * Verify that the name of the brand matches the directory in which it * is installed. */ if ((node = xmlDocGetRootElement(bhp->bh_config)) == NULL) { brand_close((brand_handle_t)bhp); return (NULL); } if (xmlStrcmp(node->name, DTD_ELEM_BRAND) != 0) { brand_close((brand_handle_t)bhp); return (NULL); } if ((property = xmlGetProp(node, DTD_ATTR_NAME)) == NULL) { brand_close((brand_handle_t)bhp); return (NULL); } if (strcmp((char *)property, name) != 0) { xmlFree(property); brand_close((brand_handle_t)bhp); return (NULL); } xmlFree(property); /* * Open handle to platform configuration file. */ (void) snprintf(path, sizeof (path), "%s/%s/%s", BRAND_DIR, name, BRAND_PLATFORM); if ((bhp->bh_platform = open_xml_file(path)) == NULL) { brand_close((brand_handle_t)bhp); return (NULL); } return ((brand_handle_t)bhp); }
int main (int argc, char *argv[]) { FILE *kb; struct GModule *module; struct { struct Option *file; struct Option *log; } parm; struct { struct Flag *all; } flag; /* setup some basic GIS stuff */ G_gisinit (argv[0]); module = G_define_module (); module->description = "Displays structured contents of a Dempster-Shafer knowledge base"; /* do not pause after a warning message was displayed */ G_sleep_on_error (0); /* Parameters */ parm.file = G_define_option (); parm.file->key = "file"; parm.file->type = TYPE_STRING; parm.file->required = YES; parm.file->description = "Name of the knowledge base file to display"; parm.log = G_define_option (); parm.log->key = "output"; parm.log->type = TYPE_STRING; parm.log->required = NO; parm.log->description = "File to write contents to (default: display on screen)"; /* Flags */ flag.all = G_define_flag (); flag.all->key='a'; flag.all->description = "Show all hypotheses (including type AUTO)"; /* parse command line */ if (G_parser (argc, argv)) { exit (-1); } /* check if we have read/write access to knowledge base */ errno = 0; kb = fopen (parm.file->answer, "r"); if ( kb == NULL ) { G_fatal_error ("Cannot open knowledge base file for reading.\nReason: %s.", strerror (errno)); } else { fclose(kb); } open_xml_file ( parm.file->answer ); if ( parm.log->answer != NULL ) { errno = 0; lp = fopen (parm.log->answer,"w+"); if ( lp == NULL ) { G_fatal_error ("Cannot create output file for writing.\nReason: %s.", strerror (errno)); } } else { /* send output to terminal */ lp = stdout; } /* now output information */ print_header ( parm.file->answer ); print_hypotheses ( flag.all->answer ); print_const_evidence (); print_rast_evidence (); print_vect_evidence (); print_groups (); exit (EXIT_SUCCESS); }