root/juggler/branches/1.0/Config/vjPropertyDesc.cpp

Revision 8789, 7.7 kB (checked in by patrickh, 7 years ago)

Copyright update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998, 1999, 2000, 2001, 2002
4  *   by Iowa State University
5  *
6  * Original Authors:
7  *   Allen Bierbaum, Christopher Just,
8  *   Patrick Hartling, Kevin Meinert,
9  *   Carolina Cruz-Neira, Albert Baker
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *
26  * -----------------------------------------------------------------
27  * File:          $RCSfile$
28  * Date modified: $Date$
29  * Version:       $Revision$
30  * -----------------------------------------------------------------
31  *
32  *************** <auto-copyright.pl END do not edit this line> ***************/
33
34
35
36 #include <Config/vjParseUtil.h>
37 #include <Config/vjChunkDesc.h>
38 #include <Kernel/vjDebug.h>
39 #include <Config/vjConfigTokens.h>
40
41
42 vjPropertyDesc::vjPropertyDesc () : valuelabels(), enumv() {
43     validation = 1;
44     name = "";
45     token = "";
46     num = 0;
47     type = T_INVALID;
48     help = "";
49 }
50
51
52 vjPropertyDesc::vjPropertyDesc (const vjPropertyDesc& d): valuelabels(), enumv() {
53     validation = 1;
54     *this = d;
55 }
56
57
58
59 vjPropertyDesc::vjPropertyDesc (const std::string& n, int i, VarType t,
60             const std::string& h): valuelabels(), enumv() {
61     validation = 1;
62     name = n;
63     token = n;
64     help = h;
65     num = i;
66     type = t;
67 }
68
69
70
71 vjPropertyDesc::~vjPropertyDesc () {
72     /* XXX
73     unsigned int i;
74     for (i = 0; i < enumv.size(); i++)
75         delete enumv[i];
76     for (i = 0; i < valuelabels.size(); i++)
77         delete valuelabels[i];
78     */
79     validation = 0;
80 }
81
82
83
84 #ifdef VJ_DEBUG
85 void vjPropertyDesc::assertValid () const {
86     assert (validation == 1 && "Trying to use deleted vjPropertyDesc");
87 }
88 #endif
89
90
91
92 std::string vjPropertyDesc::getValueLabel (unsigned int i) {
93     assertValid();
94
95     if (i < valuelabels.size())
96         return valuelabels[i]->getName();
97     else
98         return (std::string)"";
99 }
100
101
102
103 vjEnumEntry* vjPropertyDesc::getEnumEntry (const std::string& s) {
104     assertValid();
105
106     for (unsigned int i = 0; i < enumv.size(); i++) {
107         if (!vjstrcasecmp (enumv[i]->getName(), s))
108             return enumv[i];
109     }
110     return NULL;
111 }
112
113
114 vjEnumEntry* vjPropertyDesc::getEnumEntryAtIndex (unsigned int index) {
115     assertValid();
116
117     if (enumv.size() > index)
118         return enumv[index];
119     else
120         return NULL;
121 }
122
123
124 vjEnumEntry* vjPropertyDesc::getEnumEntryWithValue (vjVarValue& val) {
125     assertValid();
126
127     for (unsigned int i = 0; i < enumv.size(); i++) {
128         if (enumv[i]->getValue() == val)
129             return enumv[i];
130     }
131     return NULL;
132 }
133
134
135 std::ostream& operator << (std::ostream& out, vjPropertyDesc& self) {
136     self.assertValid();
137
138     out << self.token.c_str() << " " << typeString(self.type) << " "
139         << self.num << " \"" << self.name.c_str() << "\"";
140
141     /* print valuelabels if we have 'em */
142     if (self.valuelabels.size() > 0) {
143         vjEnumEntry *e;
144         out << " vj_valuelabels { ";
145         for (unsigned int i = 0; i < self.valuelabels.size(); i++) {
146             e = self.valuelabels[i];
147             out << '"' << e->getName().c_str() << "\" ";
148         }
149         out << "}";
150     }
151
152     /* print enumeration only if we have values. */
153     if (self.enumv.size() > 0) {
154         out << " vj_enumeration { ";
155         for (unsigned int i = 0; i < self.enumv.size(); i++)
156             out << *(self.enumv[i]) << ' ';
157         out << "}";
158     }
159
160     /* print help string - always quoted. */
161     out << " \"" << self.help.c_str() << '"';
162     return out;
163 }
164
165
166
167 std::istream& operator >> (std::istream& in, vjPropertyDesc& self) {
168     self.assertValid();
169
170
171     const int size = 512;
172     char str[size];
173
174     const char equal_TOKEN[] = "=";
175
176     /* format of line is: name type size { enums/chunktypes } token. */
177
178     readString (in, str, size);
179     //cout << "read propertydesc token " << str << endl;
180     self.token = str;
181     if (!strcasecmp (str, end_TOKEN))
182         return in;
183
184     self.type = readType(in);
185     in >> self.num;
186     readString (in,str,size);
187
188     self.name = str;
189
190     readString (in, str, size);
191
192     /* parsing value labels, if there are any */
193     if (!strcasecmp (str, vj_valuelabels_TOKEN)) {
194         //cout << "reading valuelabels" << endl;
195         readString (in,str,size);
196         if (strcasecmp (str, lbrace_TOKEN))
197             vjDEBUG(vjDBG_ERROR,1) << clrOutNORM(clrRED, "ERROR:") << " expected '{'" << std::endl
198                                    << vjDEBUG_FLUSH;
199
200         vjEnumEntry *e;
201         readString (in, str, size);
202         while (strcasecmp (str, rbrace_TOKEN) && !in.eof()) {
203             e = new vjEnumEntry (str, T_STRING);
204             self.valuelabels.push_back (e);
205             readString (in, str, size);
206         }
207         readString (in, str, size);
208     }
209
210     /* parsing enumerations, if there are any */
211     if (!strcasecmp (str, vj_enumeration_TOKEN))
212         readString (in, str, size);
213     if (!strcasecmp (str, lbrace_TOKEN)) {
214         //cout << "parsing enumerations" << endl;
215         int i = 0;
216         readString (in, str, size);
217         while (strcmp (str, rbrace_TOKEN) && !in.eof()) {
218             vjVarValue *v;
219             // this is slightly kludgey.  We make a varvalue to store the enumeration
220             // value... except for T_CHUNK and T_EMBEDDEDCHUNK where we store a chunk
221             // name type...
222             if ((self.type == T_CHUNK) || (self.type == T_EMBEDDEDCHUNK))
223                 v = new vjVarValue (T_STRING);
224             else
225                 v = new vjVarValue (self.type);
226
227             char* c = strstr (str, equal_TOKEN);
228             if (c) {
229                 *c = '\0';
230                 *v = (c+1);
231             }
232             else {
233                 if (self.type == T_STRING || self.type == T_CHUNK ||
234                     self.type == T_EMBEDDEDCHUNK)
235                     *v = str;
236                 else
237                     *v = i++;
238             }
239             self.enumv.push_back (new vjEnumEntry (str, *v));
240             // delete v; XXX
241             readString (in, str, size);
242         }
243         readString (in, str, size);
244     }
245     self.help = str;
246
247     return in;
248 }
249
250
251 vjPropertyDesc& vjPropertyDesc::operator= (const vjPropertyDesc& pd) {
252     assertValid();
253
254     unsigned int i;
255     if (&pd == this)
256         return *this;
257     name = pd.name;
258     token = pd.token;
259     help = pd.help;
260     type = pd.type;
261     num = pd.num;
262
263     /*
264     for (i = 0; i < valuelabels.size(); i++)
265         delete valuelabels[i];
266     for (i = 0; i < enumv.size(); i++)
267         delete enumv[i];
268         */
269     valuelabels.clear();
270     enumv.clear();
271     for (i = 0; i < pd.valuelabels.size(); i++)
272         valuelabels.push_back (new vjEnumEntry(*(pd.valuelabels[i])));
273     for (i = 0; i < pd.enumv.size(); i++)
274         enumv.push_back (new vjEnumEntry(*(pd.enumv[i])));
275     return *this;
276 }
277
278
279 //: Equality Operator
280 // BUG (IPTHACK) - doesn't check equality of enumerations and valuelabels
281 bool vjPropertyDesc::operator== (const vjPropertyDesc& pd) {
282     assertValid();
283
284     if (vjstrcasecmp (name, pd.name))
285         return false;
286     if (vjstrcasecmp (token, pd.token))
287         return false;
288     if (type != pd.type)
289         return false;
290     if (num != pd.num)
291         return false;
292     return true;
293 }
294
295
296
Note: See TracBrowser for help on using the browser.