BRL-CAD
prcolor.c
Go to the documentation of this file.
1 /* P R C O L O R . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file libged/prcolor.c
21  *
22  * The prcolor command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 
32 #include "db.h"
33 #include "mater.h"
34 
35 #include "./ged_private.h"
36 
37 
38 static void
39 pr_vls_col_item(struct bu_vls *str,
40  char *cp,
41  int *ccp, /* column count pointer */
42  int *clp) /* column length pointer */
43 {
44  /* Output newline if last column printed. */
45  if (*ccp >= _GED_COLUMNS || (*clp+_GED_V4_MAXNAME-1) >= _GED_TERMINAL_WIDTH) {
46  /* line now full */
47  bu_vls_putc(str, '\n');
48  *ccp = 0;
49  } else if (*ccp != 0) {
50  /* Space over before starting new column */
51  do {
52  bu_vls_putc(str, ' ');
53  ++*clp;
54  } while ((*clp % _GED_V4_MAXNAME) != 0);
55  }
56  /* Output string and save length for next tab. */
57  *clp = 0;
58  while (*cp != '\0') {
59  bu_vls_putc(str, *cp);
60  ++cp;
61  ++*clp;
62  }
63  ++*ccp;
64 }
65 
66 
67 static void
68 pr_vls_col_eol(struct bu_vls *str,
69  int *ccp,
70  int *clp)
71 {
72  if (*ccp != 0) /* partial line */
73  bu_vls_putc(str, '\n');
74  *ccp = 0;
75  *clp = 0;
76 }
77 
78 
79 static void
80 pr_mater(struct ged *gedp,
81  const struct mater *mp,
82  int *ccp,
83  int *clp)
84 {
85  char buf[128];
86 
87  (void)sprintf(buf, "%5d..%d", mp->mt_low, mp->mt_high);
88  pr_vls_col_item(gedp->ged_result_str, buf, ccp, clp);
89  (void)sprintf(buf, "%3d, %3d, %3d", mp->mt_r, mp->mt_g, mp->mt_b);
90  pr_vls_col_item(gedp->ged_result_str, buf, ccp, clp);
91  pr_vls_col_eol(gedp->ged_result_str, ccp, clp);
92 }
93 
94 
95 int
96 ged_prcolor(struct ged *gedp, int argc, const char *argv[])
97 {
98  const struct mater *mp;
99  int col_count = 0;
100  int col_len = 0;
101 
104  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
105 
106  /* initialize result */
107  bu_vls_trunc(gedp->ged_result_str, 0);
108 
109  if (argc != 1) {
110  bu_vls_printf(gedp->ged_result_str, "Usage: %s", argv[0]);
111  return GED_ERROR;
112  }
113 
114  if (rt_material_head() == MATER_NULL) {
115  bu_vls_printf(gedp->ged_result_str, "none");
116  return GED_OK;
117  }
118 
119  for (mp = rt_material_head(); mp != MATER_NULL; mp = mp->mt_forw)
120  pr_mater(gedp, mp, &col_count, &col_len);
121 
122  return GED_OK;
123 }
124 
125 
126 /*
127  * Local Variables:
128  * tab-width: 8
129  * mode: C
130  * indent-tabs-mode: t
131  * c-file-style: "stroustrup"
132  * End:
133  * ex: shiftwidth=4 tabstop=8
134  */
#define GED_OK
Definition: ged.h:55
Definition: ged.h:338
void bu_vls_trunc(struct bu_vls *vp, int len)
Definition: vls.c:198
#define GED_CHECK_ARGC_GT_0(_gedp, _argc, _flags)
Definition: ged.h:202
Header file for the BRL-CAD common definitions.
#define GED_ERROR
Definition: ged.h:61
#define _GED_V4_MAXNAME
Definition: ged_private.h:48
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define _GED_COLUMNS
Definition: ged_private.h:50
struct bu_vls * ged_result_str
Definition: ged.h:357
struct mater * rt_material_head(void)
Definition: mater.c:243
int ged_prcolor(struct ged *gedp, int argc, const char *argv[])
Definition: prcolor.c:96
#define _GED_TERMINAL_WIDTH
Definition: ged_private.h:49
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
Definition: vls.h:56
void bu_vls_putc(struct bu_vls *vp, int c)
Definition: vls.c:666