/* Test against basename UNIX tool */ void automatic_test(const char *input) { char buf_input[1000]; char *ans = NULL; char *res = (char *)bu_calloc(strlen(buf_input), sizeof(char), "automatic_test res"); #ifdef HAVE_BASENAME if (input) bu_strlcpy(buf_input, input, strlen(input)+1); /* build UNIX 'basename' command */ if (!input) ans = basename(NULL); else ans = basename(buf_input); if (!input) bu_basename(res, NULL); else bu_basename(res, buf_input); if (BU_STR_EQUAL(res, ans)) printf("%24s -> %24s [PASSED]\n", input, res); else bu_exit(EXIT_FAILURE, "%24s -> %24s (should be: %s) [FAIL]\n", input, res, ans); bu_free(res, NULL); #else printf("BASENAME not available on this platform\n"); #endif /* FIXME: this does not functionally halt */ }
/** @if no * P A R S E _ A R G S * @endif * @brief Parse command line flags. * * This routine handles parsing of all command line options. * * @param ac count of arguments * @param av array of pointers to null-terminated strings * @return index into av of first argument past options (new ac value) */ int parse_args(int ac, char *av[]) { int c; char *strrchr(); char *tmp_basename = NULL; /* Turn off bu_getopt's error messages */ bu_opterr = 0; /* get all the option flags from the command line */ while ((c=bu_getopt(ac, av, options)) != -1) { switch (c) { case 'd': debug = strtol(bu_optarg, NULL, 16); break; case '?': case 'h': default: tmp_basename = bu_basename(av[0]); usage(tmp_basename, "Bad or help flag specified\n"); bu_free(tmp_basename, "tmp_basename free"); break; } } return bu_optind; }
/** * M A I N * * Call parse_args to handle command line arguments first, then * process input. */ int main(int ac, char *av[]) { /** @struct rt_i * This structure contains some global state information for librt */ struct rt_i *rtip; struct db_tree_state init_state; /* state table for the hierarchy walker */ char idbuf[1024] = {0}; /* Database title */ int arg_count; char *tmp_basename; /** @struct user_data * This is an example structure. * It contains anything you want to have available in the region/leaf processing routines */ struct user_data { int stuff; } user_data; arg_count = parse_args(ac, av); if ((ac - arg_count) < 1) { tmp_basename = bu_basename(av[0]); usage(tmp_basename, "bad argument count"); bu_free(tmp_basename, "tmp_basename free"); } /* * Build an index of what's in the database. * rt_dirbuild() returns an "instance" pointer which describes * the database. It also gives you back the * title string in the header (ID) record. */ rtip = rt_dirbuild(av[arg_count], idbuf, sizeof(idbuf)); if (rtip == RTI_NULL) { bu_exit(2, "%s: rt_dirbuild failure\n", av[0]); } arg_count++; init_state = rt_initial_tree_state; db_walk_tree(rtip->rti_dbip, /* database instance */ ac-arg_count, /* number of trees to get from the database */ (const char **)&av[arg_count], 1, /* number of cpus to use */ &init_state, region_start, region_end, leaf_func, (genptr_t)&user_data); /* at this point you can do things with the geometry you have obtained */ return 0; }
/** b u _ g e t p r o g n a m e * * get the name of the running application if they ran * bu_setprogname() first or if we know what it's supposed to be * anyways. */ const char * bu_getprogname(void) { const char *name = NULL; if (bu_progname[0] != '\0') { return bu_basename(bu_progname); } #ifdef HAVE_GETPROGNAME name = getprogname(); /* not malloc'd memory */ #endif if (!name) { name = bu_argv0(); } snprintf(bu_progname, MAXPATHLEN, "%s", name?name:"unknown"); return bu_basename(bu_progname); }
int ged_bot_flip(struct ged *gedp, int argc, const char *argv[]) { int i; struct directory *dp; struct rt_db_internal intern; struct rt_bot_internal *bot; static const char *usage = "bot [bot2 bot3 ...]"; GED_CHECK_DATABASE_OPEN(gedp, GED_ERROR); GED_CHECK_READ_ONLY(gedp, GED_ERROR); GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR); /* initialize result */ bu_vls_trunc(gedp->ged_result_str, 0); /* must be wanting help */ if (argc == 1) { bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage); return GED_HELP; } for (i = 1; i < argc; ++i) { /* Skip past any path elements */ char *obj = (char *)bu_calloc(strlen(argv[i]), sizeof(char), "ged_bot_flip obj"); bu_basename(obj, argv[i]); if (BU_STR_EQUAL(obj, ".")) { /* malformed path, lookup using exactly what was provided */ bu_free(obj, "free bu_basename"); obj = bu_strdup(argv[i]); } if ((dp = db_lookup(gedp->ged_wdbp->dbip, obj, LOOKUP_QUIET)) == RT_DIR_NULL) { bu_vls_printf(gedp->ged_result_str, "%s: db_lookup(%s) error\n", argv[0], obj); } else { GED_DB_GET_INTERNAL(gedp, &intern, dp, bn_mat_identity, &rt_uniresource, GED_ERROR); if (intern.idb_major_type != DB5_MAJORTYPE_BRLCAD || intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_BOT) { bu_vls_printf(gedp->ged_result_str, "%s: %s is not a BOT solid!\n", argv[0], obj); } else { bot = (struct rt_bot_internal *)intern.idb_ptr; rt_bot_flip(bot); GED_DB_PUT_INTERNAL(gedp, dp, &intern, gedp->ged_wdbp->wdb_resp, GED_ERROR); } } bu_free(obj, "free obj"); } return GED_OK; }