Revision: 76850
          http://sourceforge.net/p/brlcad/code/76850
Author:   starseeker
Date:     2020-08-19 15:26:00 +0000 (Wed, 19 Aug 2020)
Log Message:
-----------
Stub in bare bones of a subcommand based analyze command.

Modified Paths:
--------------
    brlcad/branches/analyze_cmd/src/libged/analyze/CMakeLists.txt

Added Paths:
-----------
    brlcad/branches/analyze_cmd/src/libged/analyze/analyze.cpp
    brlcad/branches/analyze_cmd/src/libged/analyze/ged_analyze.h
    brlcad/branches/analyze_cmd/src/libged/analyze/util.cpp

Modified: brlcad/branches/analyze_cmd/src/libged/analyze/CMakeLists.txt
===================================================================
--- brlcad/branches/analyze_cmd/src/libged/analyze/CMakeLists.txt       
2020-08-19 14:54:56 UTC (rev 76849)
+++ brlcad/branches/analyze_cmd/src/libged/analyze/CMakeLists.txt       
2020-08-19 15:26:00 UTC (rev 76850)
@@ -6,9 +6,14 @@
   ${GED_INCLUDE_DIRS}
   )
 
+set(analyze_srcs
+  analyze.cpp
+  util.cpp
+  )
+
 add_definitions(-DGED_PLUGIN)
-ged_plugin_library(ged-analyze SHARED analyze.c)
-target_link_libraries(ged-analyze libged libbu)
+ged_plugin_library(ged-analyze SHARED ${analyze_srcs})
+target_link_libraries(ged-analyze libged libanalyze libbu)
 set_property(TARGET ged-analyze APPEND PROPERTY COMPILE_DEFINITIONS 
BRLCADBUILD HAVE_CONFIG_H)
 VALIDATE_STYLE(ged-analyze analyze.c)
 PLUGIN_SETUP(ged-analyze ged)
@@ -15,6 +20,8 @@
 
 CMAKEFILES(
   CMakeLists.txt
+  ${analyze_srcs}
+  ged_analyze.h
   analyze.c
   )
 

Added: brlcad/branches/analyze_cmd/src/libged/analyze/analyze.cpp
===================================================================
--- brlcad/branches/analyze_cmd/src/libged/analyze/analyze.cpp                  
        (rev 0)
+++ brlcad/branches/analyze_cmd/src/libged/analyze/analyze.cpp  2020-08-19 
15:26:00 UTC (rev 76850)
@@ -0,0 +1,249 @@
+/*                      A N A L Y Z E . C P P
+ * BRL-CAD
+ *
+ * Copyright (c) 2020 United States Government as represented by
+ * the U.S. Army Research Laboratory.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; see the file named COPYING for more
+ * information.
+ */
+/** @file analyze.cpp
+ *
+ * The analyze command
+ *
+ */
+
+#include "common.h"
+
+extern "C" {
+#include <stdlib.h>
+#include <ctype.h>
+#include <string.h>
+}
+
+#include <limits>
+
+extern "C" {
+#include "bu/cmd.h"
+#include "bu/opt.h"
+#include "../ged_private.h"
+#include "./ged_analyze.h"
+}
+
+#define HELPFLAG "--print-help"
+#define PURPOSEFLAG "--print-purpose"
+
+struct _ged_analyze_info {
+    struct ged *gedp = NULL;
+    const struct bu_cmdtab *cmds = NULL;
+    struct bu_opt_desc *gopts = NULL;
+    int verbosity = 0;
+};
+
+
+static int
+_analyze_cmd_msgs(void *cs, int argc, const char **argv, const char *us, const 
char *ps)
+{
+    struct _ged_analyze_info *gc = (struct _ged_analyze_info *)cs;
+    if (argc == 2 && BU_STR_EQUAL(argv[1], HELPFLAG)) {
+       bu_vls_printf(gc->gedp->ged_result_str, "%s\n%s\n", us, ps);
+       return 1;
+    }
+    if (argc == 2 && BU_STR_EQUAL(argv[1], PURPOSEFLAG)) {
+       bu_vls_printf(gc->gedp->ged_result_str, "%s\n", ps);
+       return 1;
+    }
+    return 0;
+}
+
+extern "C" int
+_analyze_cmd_summarize(void *bs, int argc, const char **argv)
+{
+    const char *usage_string = "analyze [options] summarize obj1 <obj2 ...>";
+    const char *purpose_string = "Summary of analytical information about 
listed objects";
+    if (_analyze_cmd_msgs(bs, argc, argv, usage_string, purpose_string)) {
+       return GED_OK;
+    }
+
+    struct _ged_analyze_info *gc = (struct _ged_analyze_info *)bs;
+
+    argc--; argv++;
+    if (argc != 2) {
+       bu_vls_printf(gc->gedp->ged_result_str, "%s\n", usage_string);
+       return GED_ERROR;
+    }
+
+    return GED_OK;
+}
+
+extern "C" int
+_analyze_cmd_help(void *bs, int argc, const char **argv)
+{
+    struct _ged_analyze_info *gc = (struct _ged_analyze_info *)bs;
+    if (!argc || !argv || BU_STR_EQUAL(argv[0], "help")) {
+       bu_vls_printf(gc->gedp->ged_result_str, "analyze [options] subcommand 
[args]\n");
+       if (gc->gopts) {
+           char *option_help = bu_opt_describe(gc->gopts, NULL);
+           if (option_help) {
+               bu_vls_printf(gc->gedp->ged_result_str, "Options:\n%s\n", 
option_help);
+               bu_free(option_help, "help str");
+           }
+       }
+       bu_vls_printf(gc->gedp->ged_result_str, "Available subcommands:\n");
+       const struct bu_cmdtab *ctp = NULL;
+       int ret;
+       const char *helpflag[2];
+       helpflag[1] = PURPOSEFLAG;
+       size_t maxcmdlen = 0;
+       for (ctp = gc->cmds; ctp->ct_name != (char *)NULL; ctp++) {
+           maxcmdlen = (maxcmdlen > strlen(ctp->ct_name)) ? maxcmdlen : 
strlen(ctp->ct_name);
+       }
+       for (ctp = gc->cmds; ctp->ct_name != (char *)NULL; ctp++) {
+           bu_vls_printf(gc->gedp->ged_result_str, "  %s%*s", ctp->ct_name, 
(int)(maxcmdlen - strlen(ctp->ct_name)) +   2, " ");
+           if (!BU_STR_EQUAL(ctp->ct_name, "help")) {
+               helpflag[0] = ctp->ct_name;
+               bu_cmd(gc->cmds, 2, helpflag, 0, (void *)gc, &ret);
+           } else {
+               bu_vls_printf(gc->gedp->ged_result_str, "print help and 
exit\n");
+           }
+       }
+    } else {
+       int ret;
+       const char **helpargv = (const char **)bu_calloc(argc+1, sizeof(char 
*), "help argv");
+       helpargv[0] = argv[0];
+       helpargv[1] = HELPFLAG;
+       for (int i = 1; i < argc; i++) {
+           helpargv[i+1] = argv[i];
+       }
+       bu_cmd(gc->cmds, argc+1, helpargv, 0, (void *)gc, &ret);
+       bu_free(helpargv, "help argv");
+       return ret;
+    }
+
+    return GED_OK;
+}
+
+
+const struct bu_cmdtab _analyze_cmds[] = {
+      { "summarize",            _analyze_cmd_summarize},
+      { (char *)NULL,      NULL}
+  };
+
+
+extern "C" int
+ged_analyze_core(struct ged *gedp, int argc, const char *argv[])
+{
+    int help = 0;
+    struct _ged_analyze_info gc;
+    gc.gedp = gedp;
+    gc.cmds = _analyze_cmds;
+    gc.verbosity = 0;
+
+    // Sanity
+    if (UNLIKELY(!gedp || !argc || !argv)) {
+       return GED_ERROR;
+    }
+
+    // Clear results
+    bu_vls_trunc(gedp->ged_result_str, 0);
+
+    // We know we're the brep command - start processing args
+    argc--; argv++;
+
+    // See if we have any high level options set
+    struct bu_opt_desc d[3];
+    BU_OPT(d[0], "h", "help",    "",      NULL,                 &help,         
"Print help");
+    BU_OPT(d[1], "v", "verbose", "",      NULL,                 &gc.verbosity, 
"Verbose output");
+    BU_OPT_NULL(d[2]);
+
+    gc.gopts = d;
+
+    if (!argc) {
+       _analyze_cmd_help(&gc, 0, NULL);
+       return GED_OK;
+    }
+
+    // High level options are only defined prior to the subcommand
+    int cmd_pos = -1;
+    for (int i = 0; i < argc; i++) {
+       if (bu_cmd_valid(_analyze_cmds, argv[i]) == BRLCAD_OK) {
+           cmd_pos = i;
+           break;
+       }
+    }
+
+    int acnt = (cmd_pos >= 0) ? cmd_pos : argc;
+
+    bu_opt_parse(NULL, acnt, argv, d);
+
+    if (help) {
+       if (cmd_pos >= 0) {
+           argc = argc - cmd_pos;
+           argv = &argv[cmd_pos];
+           _analyze_cmd_help(&gc, argc, argv);
+       } else {
+           _analyze_cmd_help(&gc, 0, NULL);
+       }
+       return GED_OK;
+    }
+
+    // Must have a subcommand
+    if (cmd_pos == -1) {
+       bu_vls_printf(gedp->ged_result_str, ": no valid subcommand 
specified\n");
+       _analyze_cmd_help(&gc, 0, NULL);
+       return GED_ERROR;
+    }
+
+
+    // Jump the processing past any options specified
+    argc = argc - cmd_pos;
+    argv = &argv[cmd_pos];
+
+    int ret;
+    if (bu_cmd(_analyze_cmds, argc, argv, 0, (void *)&gc, &ret) == BRLCAD_OK) {
+       return ret;
+    } else {
+       bu_vls_printf(gedp->ged_result_str, "subcommand %s not defined", 
argv[0]);
+    }
+
+    return GED_ERROR;
+}
+
+// Local Variables:
+
+#ifdef GED_PLUGIN
+#include "../include/plugin.h"
+extern "C" {
+    struct ged_cmd_impl analyze_cmd_impl = { "analyze", ged_analyze_core, 
GED_CMD_DEFAULT };
+    const struct ged_cmd analyze_cmd = { &analyze_cmd_impl };
+
+    const struct ged_cmd *analyze_cmds[] = { &analyze_cmd, NULL };
+
+    static const struct ged_plugin pinfo = { GED_API,  analyze_cmds, 1 };
+
+    COMPILER_DLLEXPORT const struct ged_plugin *ged_plugin_info()
+    {
+       return &pinfo;
+    }
+}
+#endif
+
+// Local Variables:
+// tab-width: 8
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: t
+// c-file-style: "stroustrup"
+// End:
+// ex: shiftwidth=4 tabstop=8
+


Property changes on: brlcad/branches/analyze_cmd/src/libged/analyze/analyze.cpp
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: brlcad/branches/analyze_cmd/src/libged/analyze/ged_analyze.h
===================================================================
--- brlcad/branches/analyze_cmd/src/libged/analyze/ged_analyze.h                
                (rev 0)
+++ brlcad/branches/analyze_cmd/src/libged/analyze/ged_analyze.h        
2020-08-19 15:26:00 UTC (rev 76850)
@@ -0,0 +1,57 @@
+/*                    G E D _ A N A L Y Z E . H
+ * BRL-CAD
+ *
+ * Copyright (c) 2020 United States Government as represented by
+ * the U.S. Army Research Laboratory.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; see the file named COPYING for more
+ * information.
+ */
+/** @file ged_cg.h
+ *
+ * Brief description
+ *
+ */
+
+#ifndef LIBGED_ANALYZE_GED_PRIVATE_H
+#define LIBGED_ANALYZE_GED_PRIVATE_H
+
+#include "common.h"
+
+#include "./ged/defines.h"
+
+__BEGIN_DECLS
+
+GED_EXPORT extern void
+print_volume_table(
+       struct ged *gedp,
+               const fastf_t tot_vol,
+               const fastf_t tot_area,
+               const fastf_t tot_gallons
+       );
+
+
+__END_DECLS
+
+#endif /* LIBGED_ANALYZE_GED_PRIVATE_H */
+
+/** @} */
+/*
+ * Local Variables:
+ * mode: C
+ * tab-width: 8
+ * indent-tabs-mode: t
+ * c-file-style: "stroustrup"
+ * End:
+ * ex: shiftwidth=4 tabstop=8
+ */


Property changes on: 
brlcad/branches/analyze_cmd/src/libged/analyze/ged_analyze.h
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: brlcad/branches/analyze_cmd/src/libged/analyze/util.cpp
===================================================================
--- brlcad/branches/analyze_cmd/src/libged/analyze/util.cpp                     
        (rev 0)
+++ brlcad/branches/analyze_cmd/src/libged/analyze/util.cpp     2020-08-19 
15:26:00 UTC (rev 76850)
@@ -0,0 +1,148 @@
+#include "common.h"
+
+extern "C" {
+#include <stdlib.h>
+#include <ctype.h>
+#include <string.h>
+}
+
+#include <limits>
+
+extern "C" {
+#include "bu/malloc.h"
+#include "bu/vls.h"
+#include "../ged_private.h"
+#include "./ged_analyze.h"
+}
+
+#define FBUFSIZ 100
+#define NFIELDS 9
+
+typedef struct row_field
+{
+    int nchars;
+    char buf[FBUFSIZ];
+} field_t;
+
+typedef struct table_row
+{
+    int nfields;
+    field_t fields[NFIELDS];
+} row_t;
+
+typedef struct table
+{
+    int nrows;
+    row_t *rows;
+} table_t;
+
+void get_dashes(field_t *f, const int ndashes)
+{
+    int i;
+    f->buf[0] = '\0';
+    for (i = 0; i < ndashes; ++i) {
+       bu_strlcat(f->buf, "-", FBUFSIZ);
+    }
+    f->nchars = ndashes;
+}
+
+void
+print_volume_table(
+       struct ged *gedp,
+       const fastf_t tot_vol,
+       const fastf_t tot_area,
+       const fastf_t tot_gallons
+       )
+{
+
+/* table format
+
+   +------------------------------------+
+   | Volume       = 7999999999.99999905 |
+   | Surface Area =   24000000.00000000 |
+   | Gallons      =       2113.37641887 |
+   +------------------------------------+
+
+*/
+    /* track actual table column widths */
+    /* this table has 1 column (plus a name column) */
+    int maxwidth[2] = {0, 0};
+    field_t dashes;
+    const char* fnames[3] = {"Volume", "Surface Area", "Gallons"};
+    int indent = 4; /* number spaces to indent the table */
+    int table_width_chars;
+    table_t table;
+    int i, nd, field;
+
+    table.nrows = 3;
+    table.rows = (row_t *)bu_calloc(3, sizeof(row_t), "print_volume_table: 
rows");
+    for (i = 0; i < table.nrows; ++i) {
+       fastf_t val = 0.0;
+
+       /* field 0 */
+       field = 0;
+       table.rows[i].fields[0].nchars = 
snprintf(table.rows[i].fields[field].buf, FBUFSIZ, "%s",
+                                                 fnames[i]);
+       if (maxwidth[field] < table.rows[i].fields[field].nchars)
+           maxwidth[field] = table.rows[i].fields[field].nchars;
+
+       if (i == 0) {
+           val = tot_vol;
+       } else if (i == 1) {
+           val = tot_area;
+       } else if (i == 2) {
+           val = tot_gallons;
+       }
+
+       /* field 1 */
+       field = 1;
+       if (val < 0) {
+           table.rows[i].fields[1].nchars = 
snprintf(table.rows[i].fields[field].buf, FBUFSIZ, "COULD NOT DETERMINE");
+       } else {
+           table.rows[i].fields[1].nchars = 
snprintf(table.rows[i].fields[field].buf, FBUFSIZ, "%10.8f", val);
+       }
+       if (maxwidth[field] < table.rows[i].fields[field].nchars)
+           maxwidth[field] = table.rows[i].fields[field].nchars;
+    }
+
+    /* get total table width */
+    table_width_chars  = maxwidth[0] + maxwidth[1];
+    table_width_chars += 2 + 2; /* 2 chars at each end of a row */
+    table_width_chars += 3; /* ' = ' between the two fields of a row */
+
+    /* newline following previous table */
+    bu_vls_printf(gedp->ged_result_str, "\n");
+
+    /* header row 1 */
+    nd = table_width_chars - 4;
+    get_dashes(&dashes, nd);
+    bu_vls_printf(gedp->ged_result_str, "%-*.*s+-%-*.*s-+\n",
+                 indent, indent, " ",
+                 nd, nd, dashes.buf);
+
+    /* the three data rows */
+    for (i = 0; i < table.nrows; ++i) {
+       bu_vls_printf(gedp->ged_result_str, "%-*.*s| %-*.*s = %*.*s |\n",
+                     indent, indent, " ",
+                     maxwidth[0], maxwidth[0], table.rows[i].fields[0].buf,
+                     maxwidth[1], maxwidth[1], table.rows[i].fields[1].buf);
+    }
+
+    /* closing table row */
+    bu_vls_printf(gedp->ged_result_str, "%-*.*s+-%-*.*s-+\n",
+                 indent, indent, " ",
+                 nd, nd, dashes.buf);
+    bu_free((char *)table.rows, "print_volume_table: rows");
+}
+
+
+
+// Local Variables:
+// tab-width: 8
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: t
+// c-file-style: "stroustrup"
+// End:
+// ex: shiftwidth=4 tabstop=8
+


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



_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to