Debian Codegraph @

apps/tools/ivinfo/ivinfo.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990,91 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.3 $
44 |
45 | Description:
46 | ivinfo - prints out information about an Inventor data file,
47 | including:
48 |
49 | - whether data is ASCII or binary
50 | - number of root nodes
51 | - number of nodes under each root
52 | - contents of any SoInfo nodes found in the data
53 |
54 | Author(s) : Paul S. Strauss
55 |
56 ______________ S I L I C O N G R A P H I C S I N C . ____________
57 _______________________________________________________________________
58 */
59
60 #include <cstdlib>
61 #include <unistd.h>
62
63 #include <Inventor/SoDB.h>
64 #include <Inventor/SoInteraction.h>
65 #include <Inventor/SoInput.h>
66 #include <Inventor/actions/SoSearchAction.h>
67 #include <Inventor/nodes/SoGroup.h>
68 #include <Inventor/nodes/SoInfo.h>
69
70 static const char *progName;
71
72 //
73 // Counts and returns the number of nodes under the given root node.
74 // This is recursive.
75 //
76
77 static int
78 countNodes(const SoNode *root)
79 {
80 int numNodes = 1;
81
82 if (root->isOfType(SoGroup::getClassTypeId())) {
83
84 const SoGroup *group = (const SoGroup *) root;
85 int i;
86
87 for (i = 0; i < group->getNumChildren(); i++)
88 numNodes += countNodes(group->getChild(i));
89 }
90
91 return numNodes;
92 }
93
94 //
95 // Searches for all SoInfo nodes in the input graph, printing out the
96 // string field for any that are found.
97 //
98
99 static void
100 printInfo(SoNode *root)
101 {
102 SoSearchAction sa;
103 const SoInfo *info;
104 int i;
105
106 // Set up action to search for SoInfo nodes
107 sa.setType(SoInfo::getClassTypeId());
108 sa.setInterest(SoSearchAction::ALL);
109 sa.setSearchingAll(TRUE);
110
111 sa.apply(root);
112
113 for (i = 0; i < sa.getPaths().getLength(); i++) {
114 info = (const SoInfo *) sa.getPaths()[i]->getTail();
115 printf("%s\n", info->string.getValue().getString());
116 }
117 }
118
119 static void
120 printUsage(const char *progname)
121 {
122 fprintf(stderr, "Usage: %s [-h] [file]\n", progname);
123 fprintf(stderr,
124 "-h : Print this message (help)\n"
125 "If no filename is given, standard input will be read\n");
126 }
127
128 static void
129 parseArgs(int argc, char **argv, SoInput &in)
130 {
131 int err = 0; // Flag: error in options?
132 int c;
133
134 while ((c = getopt(argc, argv, "h")) != -1) {
135 switch(c) {
136 case 'h': // Help
137 default:
138 err = 1;
139 break;
140 }
141 }
142 if (optind+1 == argc) { // One optional filename
143 if (! in.openFile(argv[optind])) {
144 fprintf(stderr, "Could not open file %s\n", argv[optind]);
145 err = 1;
146 }
147 }
148 else if (optind == argc) { // No filename
149 if (isatty(fileno(stdin))) {
150 fprintf(stderr, "Trying to read from standard input, "
151 "but standard input is a tty!\n");
152 err = 1;
153 }
154 }
155 else err = 1;
156 if (err) {
157 printUsage(argv[0]);
158 exit(1);
159 }
160 }
161
162 int
163 main(int argc, char *argv[])
164 {
165 SoInput in;
166 SoNode *root;
167 SbBool gotAny = FALSE;
168
169 progName = argv[0];
170
171 SoInteraction::init();
172
173 parseArgs(argc, argv, in);
174
175 while (TRUE) {
176
177 if (! SoDB::read(&in, root)) {
178 fprintf(stderr, "%s: bad data in input\n", progName);
179 return 1;
180 }
181
182 if (root == NULL) {
183 if (! gotAny)
184 printf("%s: no data in input\n", progName);
185 break;
186 }
187
188 root->ref();
189
190 if (! gotAny) {
191 printf("%s: data format is %s\n", progName,
192 in.isBinary() ? "binary" : "ASCII");
193 gotAny = TRUE;
194 }
195
196 else
197 printf("-----------------------------------------------------\n");
198
199 printf("%s: number of nodes under root is %d\n", progName,
200 countNodes(root));
201
202 printInfo(root);
203
204 root->unref();
205 }
206
207 return(0);
208 }