int keychain_recode(int argc, char * const *argv) { char *keychainName1 = NULL, *keychainName2 = NULL; int ch, result = 0; while ((ch = getopt(argc, argv, "h")) != -1) { switch (ch) { case '?': default: return 2; /* @@@ Return 2 triggers usage message. */ } } argc -= optind; argv += optind; if (argc == 2) { keychainName1 = argv[0]; if (*keychainName1 == '\0') { result = 2; goto loser; } keychainName2 = argv[1]; if (*keychainName2 == '\0') { result = 2; goto loser; } } else return 2; result = do_recode(keychainName1, keychainName2); loser: return result; }
/** * copies a subset of data from one DB into another one. The subset is * given by field names */ static int cmd_recode(const char* output_path, const char* input, const char** field_names, int names_length) { assert(names_length > 0); tdb* const db = tdb_init(); assert(db); int err = tdb_open(db, input); if(err) { REPORT_ERROR("Failed to open TDB. error=%i\n", err); return 1; } tdb_field* field_ids; err = resolve_fieldids(&field_ids, db, field_names, names_length); if(err < 0) { goto free_tdb; } tdb_cons* const cons = tdb_cons_init(); assert(cons); err = tdb_cons_open(cons, output_path, field_names, (unsigned)names_length); if(err) { REPORT_ERROR("Failed to create TDB cons. error=%i\n", err); goto free_ids; } TIMED("recode", err, do_recode(cons, db, field_ids, names_length)); if(err) goto close_cons; err = tdb_cons_finalize(cons); if(err) { REPORT_ERROR("Failed to finalize output DB. error=%i\n", err); } close_cons: tdb_cons_close(cons); free_ids: free(field_ids); free_tdb: tdb_close(db); return err; }
int main(int argc, char *argv[]) { char *title; FILE *srcfp; struct GModule *module; struct { struct Option *input, *output, *title, *rules; struct Flag *a, *d; } parm; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster")); G_add_keyword(_("recode categories")); G_add_keyword(_("reclassification")); module->description = _("Recodes categorical raster maps."); parm.input = G_define_standard_option(G_OPT_R_INPUT); parm.input->description = _("Name of raster map to be recoded"); parm.output = G_define_standard_option(G_OPT_R_OUTPUT); parm.rules = G_define_standard_option(G_OPT_F_INPUT); parm.rules->key = "rules"; parm.rules->label = _("File containing recode rules"); parm.rules->description = _("'-' for standard input"); parm.title = G_define_option(); parm.title->key = "title"; parm.title->required = NO; parm.title->type = TYPE_STRING; parm.title->description = _("Title for output raster map"); parm.a = G_define_flag(); parm.a->key = 'a'; parm.a->description = _("Align the current region to the input raster map"); parm.d = G_define_flag(); parm.d->key = 'd'; parm.d->description = _("Force output to 'double' raster map type (DCELL)"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); name = parm.input->answer; result = parm.output->answer; title = parm.title->answer; align_wind = parm.a->answer; make_dcell = parm.d->answer; srcfp = stdin; if (strcmp(parm.rules->answer, "-") != 0) { srcfp = fopen(parm.rules->answer, "r"); if (!srcfp) G_fatal_error(_("Unable to open file <%s>"), parm.rules->answer); } if (!read_rules(srcfp)) { if (isatty(fileno(srcfp))) G_fatal_error(_("No rules specified. Raster map <%s> not created."), result); else G_fatal_error(_("No rules specified")); } no_mask = 0; do_recode(); if(title) Rast_put_cell_title(result, title); exit(EXIT_SUCCESS); }