Tom,

Just info, note that the "garbage_collect" command in mged is particularly relevant to what you started with.  It's implemented as a tcl script, basically calling the keep command: src/tclscripts/mged/garbage_collect.tcl 

Cheers!
Sean


On Oct 31, 2013, at 08:51 AM, [email protected] wrote:

Revision: 58373
http://sourceforge.net/p/brlcad/code/58373
Author: tbrowder2
Date: 2013-10-31 12:51:01 +0000 (Thu, 31 Oct 2013)
Log Message:
-----------
add beginnings of a database admin program

Modified Paths:
--------------
brlcad/trunk/src/util/CMakeLists.txt

Added Paths:
-----------
brlcad/trunk/src/util/admin-db.cpp

Modified: brlcad/trunk/src/util/CMakeLists.txt
===================================================================
--- brlcad/trunk/src/util/CMakeLists.txt 2013-10-31 12:01:27 UTC (rev 58372)
+++ brlcad/trunk/src/util/CMakeLists.txt 2013-10-31 12:51:01 UTC (rev 58373)
@@ -96,6 +96,9 @@
# ignore it for now
CMAKEFILES(test_bu_arg_parse.c)

+# another test for attributes
+BRLCAD_ADDEXEC(admin-db "admin-db.cpp;bu_arg_parse.cpp" libbu NO_INSTALL)
+
# tmp header to stand-in for bu.h (ignore it)
CMAKEFILES(bu_arg_parse.h)
# permanent private header for TCLAP customizations (ignore it)

Added: brlcad/trunk/src/util/admin-db.cpp
===================================================================
--- brlcad/trunk/src/util/admin-db.cpp (rev 0)
+++ brlcad/trunk/src/util/admin-db.cpp 2013-10-31 12:51:01 UTC (rev 58373)
@@ -0,0 +1,184 @@
+#include "common.h"
+
+#include <stdlib.h>
+#include <sys/stat.h>
+#include "bio.h"
+
+#include "bu_arg_parse.h" /* includes bu.h */
+
+static const char usage[] = "Example: admin-db [...options] db.g [db2.g]\n";
+
+/* purpose: manage various aspects of a BRL-CAD database
+ *
+ * description: Enables the user to perform various tasks on a BRL-CAD
+ * database (DB) file including reporting statistics and shortening
+ * the DB file by removing unused free space. The original DB file
+ * is opened read-only and is not modified in any way.
+ *
+ * The only option at the moment is to compress the input DB by removing
+ * unused free space which accumulates during the construction of a BRL-CAD model.
+ * The shortened copy of the input DB is written to a new file which may be named
+ * by the user, otherwise the new file is name "<input file name>.compressed".
+ */
+
+
+int
+main(int argc, char** argv)
+{
+ FILE *in = NULL;
+ FILE *out = NULL;
+ struct stat sb;
+ const char DBSUF[] = ".compressed";
+
+ /* vars expected from cmd line parsing */
+ int arg_err = 0;
+ int has_force = 0;
+ int has_help = 0;
+ int has_compress = 0;
+ char db_fname[BU_ARG_PARSE_BUFSZ] = {0};
+ char db2_fname[BU_ARG_PARSE_BUFSZ] = {0};
+
+ /* FIXME: this '-?' arg doesn't work correctly due to some TCLAPisms */
+ static bu_arg_switch_t h_arg;
+ /* define a force option to allow user to shoot himself in the foot */
+ static bu_arg_switch_t f_arg;
+ /* define a compress option */
+ static bu_arg_switch_t c_arg;
+
+ /* input file names */
+ static bu_arg_unlabeled_value_t db_arg;
+ /* the output file name (optional) */
+ static bu_arg_unlabeled_value_t db2_arg;
+
+ /* place the arg pointers in an array (note the array is of
+ * type void* to hold the heterogeneous arg type structs)
+ */
+ static void *args[] = {
+ &h_arg, &f_arg,
+ &db_arg, &db2_arg,
+ NULL
+ };
+
+ BU_ARG_SWITCH_INIT(
+ h_arg,
+ "?",
+ "short-help",
+ "Same as '-h' or '--help'"
+ );
+
+ BU_ARG_SWITCH_INIT(
+ f_arg,
+ "f",
+ "force",
+ "Allow overwriting existing files."
+ );
+
+ BU_ARG_SWITCH_INIT(
+ c_arg,
+ "c",
+ "compress",
+ "Create a copy with no free space."
+ );
+
+ /* input file name */
+ BU_ARG_UNLABELED_VALUE_INIT(
+ db_arg,
+ "",
+ "DB_infile",
+ "DB input file name",
+ BU_ARG_REQUIRED,
+ BU_ARG_STRING,
+ ""
+ );
+
+ /* the output file name (optional) */
+ BU_ARG_UNLABELED_VALUE_INIT(
+ db2_arg,
+ "",
+ "DB_outfile",
+ "DB output file name",
+ BU_ARG_OPTIONAL,
+ BU_ARG_STRING,
+ ""
+ );
+
+ /* parse the args */
+ arg_err = bu_arg_parse(args, argc, argv);
+
+ if (arg_err == BU_ARG_PARSE_ERR) {
+ /* the TCLAP exception handler has fired with its own message
+ * so need no message here
+ */
+ bu_exit(EXIT_SUCCESS, NULL);
+ }
+
+ /* Get the value parsed by each arg. */
+ has_force = bu_arg_get_bool(&f_arg);
+ has_help = bu_arg_get_bool(&h_arg);
+ has_compress = bu_arg_get_bool(&c_arg);
+ bu_arg_get_string(&db_arg, db_fname);
+ bu_arg_get_string(&db2_arg, db2_fname);
+
+ /* take appropriate action... */
+
+ /* note this exit is SUCCESS because it is expected
+ * behavior--important for good auto-man-page handling
+ */
+ if (has_help) {
+ bu_exit(EXIT_SUCCESS, usage);
+ }
+
+ if (has_compress) {
+ if (!db2_fname[0]) {
+ bu_strlcpy(db2_fname, db_fname, BU_ARG_PARSE_BUFSZ);
+ bu_strlcat(db2_fname, DBSUF, BU_ARG_PARSE_BUFSZ);
+ }
+ }
+
+ /* open the db file read-only */
+ /* TCLAP doesn't check for existing files (FIXME: add to TCLAP) */
+ if (!stat(db_fname, &sb)) {
+ bu_exit(EXIT_FAILURE, "non-existent input file\n");
+ }
+ in = fopen(db_fname, "r");
+ if (!in) {
+ perror(db_fname);
+ bu_exit(EXIT_FAILURE, "ERROR: input file open failure\n");
+ }
+ if (fstat(fileno(in), &sb)) {
+ perror(db_fname);
+ fclose(in);
+ bu_exit(EXIT_FAILURE, "ERROR: input file stat failure\n");
+ }
+
+ if (has_compress) {
+ /* TCLAP doesn't check for confusion in file names */
+ if (BU_STR_EQUAL(db_fname, db2_fname)) {
+ bu_exit(EXIT_FAILURE, "overwriting an input file\n");
+ }
+ /* check for existing file */
+ if (!stat(db2_fname, &sb)) {
+ if (has_force) {
+ printf("WARNING: overwriting an existing file...\n");
+ bu_file_delete(db2_fname);
+ }
+ else {
+ bu_exit(EXIT_FAILURE, "overwriting an existing file (use the '-f' option to continue)\n");
+ }
+ }
+ out = fopen(db2_fname, "w");
+ if (!out) {
+ perror(db_fname);
+ bu_exit(EXIT_FAILURE, "ERROR: output file open failure\n");
+ }
+ if (fstat(fileno(out), &sb)) {
+ perror(db2_fname);
+ fclose(out);
+ bu_exit(EXIT_FAILURE, "ERROR: output file stat failure\n");
+ }
+ }
+
+
+ return 0;
+
+}


Property changes on: brlcad/trunk/src/util/admin-db.cpp
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.


------------------------------------------------------------------------------
Android is increasing in popularity, but the open development platform that
developers love is also attractive to malware creators. Download this white
paper to learn more about secure code signing practices that can help keep
Android apps secure.
http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits
------------------------------------------------------------------------------
Android is increasing in popularity, but the open development platform that
developers love is also attractive to malware creators. Download this white
paper to learn more about secure code signing practices that can help keep
Android apps secure.
http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk
_______________________________________________
BRL-CAD Developer mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-devel

Reply via email to