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

Revision 8789, 5.5 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 <vjConfig.h>
37 #include <sys/types.h>
38
39 #include <Kernel/vjDebug.h>
40 #include <Config/vjChunkDescDB.h>
41 #include <Config/vjParseUtil.h>
42 #include <Config/vjConfigTokens.h>
43
44
45 vjChunkDescDB::vjChunkDescDB (): descs() {
46     ;
47 }
48
49
50
51 vjChunkDescDB::~vjChunkDescDB() {
52     removeAll();
53 }
54
55
56 vjChunkDesc* vjChunkDescDB::getChunkDesc (const std::string& _token) {
57     for (unsigned int i = 0; i < descs.size(); i++)
58         if (!vjstrcasecmp (descs[i]->token, _token))
59             return descs[i];
60     return NULL;
61 }
62
63
64
65 bool vjChunkDescDB::insert (vjChunkDesc *d) {
66     for (unsigned int i = 0; i < descs.size(); i++)
67         if (!vjstrcasecmp (descs[i]->token, d->token)) {
68             if (*descs[i] != *d) {
69                 vjDEBUG (vjDBG_ALL,vjDBG_CRITICAL_LVL) <<  clrOutNORM(clrRED, "ERROR:") << " redefinition of vjChunkDesc ("
70                                      << d->name.c_str() << ") not allowed:\n"
71                                      << "  Original Desc: \n" << *descs[i]
72                                      << "\n  New Desc: \n" << *d
73                                      << "\n (multiple definitions must be identical)\n"
74                                      << vjDEBUG_FLUSH;
75                 vjASSERT (false);
76                 return false;
77             }
78             //delete d;  // should be safe to delete, not 100% sure (ipt hack)
79             return true;
80         }
81     descs.push_back(d);
82     return true;
83 }
84
85
86
87 void vjChunkDescDB::insert (vjChunkDescDB* db) {
88     std::vector<vjChunkDesc*>::iterator begin = db->descs.begin();
89     while (begin != db->descs.end()) {
90         insert (new vjChunkDesc(**begin));
91         begin++;
92     }
93 }
94
95
96
97 bool vjChunkDescDB::remove (const std::string& tok) {
98
99     std::vector<vjChunkDesc*>::iterator cur_desc = descs.begin();
100     while (cur_desc != descs.end()) {
101         if (!vjstrcasecmp ((*cur_desc)->token, tok)) {
102             /// delete(*begin);     XXX:
103             cur_desc = descs.erase(cur_desc);
104             return true;
105         }
106         else
107             cur_desc++;
108     }
109     return false;
110 }
111
112
113
114 void vjChunkDescDB::removeAll () {
115     std::vector<vjChunkDesc*>::iterator i = descs.begin();
116     while (i != descs.end()) {
117         // delete (*i);    XXX:
118         i++;
119     }
120     descs.clear();
121 }
122
123
124
125 int vjChunkDescDB::size () {
126     return descs.size();
127 }
128
129
130
131 std::ostream& operator << (std::ostream& out, vjChunkDescDB& self) {
132     for (unsigned int i = 0; i < self.descs.size(); i++)
133         out << "Chunk " << *(self.descs[i]) << std::endl;
134     out << "End" << std::endl;
135     return out;
136 }
137
138
139
140 std::istream& operator >> (std::istream& in, vjChunkDescDB& self) {
141     const int buflen = 512;
142     char str[buflen];
143     vjChunkDesc *ch;
144
145     for (;;) {
146         if (readString (in, str, buflen) == 0)
147             break; /* eof */
148         else if (!strcasecmp (str, chunk_TOKEN)) {
149             ch = new vjChunkDesc();
150             in >> *ch;
151             self.insert(ch);
152         }
153         else if (!strcasecmp (str, end_TOKEN))
154             break;
155         else {
156             vjDEBUG(vjDBG_ERROR,1) << "Unexpected symbol parsing vjChunkDescDB: '"
157                        << str <<"'"<< std::endl << vjDEBUG_FLUSH;
158         }
159     }
160     vjDEBUG(vjDBG_CONFIG,3) << "vjChunkDescDB::>> : Finished - " << self.descs.size()
161                << " descriptions read." << std::endl << vjDEBUG_FLUSH;
162     return in;
163 }
164
165
166
167 bool vjChunkDescDB::load (const std::string& filename, const std::string& parentfile) {
168     std::string fname = demangleFileName (filename, parentfile);
169     std::ifstream in(fname.c_str());
170
171     if (!in) {
172         vjDEBUG(vjDBG_ERROR, vjDBG_CRITICAL_LVL)
173             << clrOutNORM(clrYELLOW, "WARNING:") << " vjChunkDescDB::load(): Unable to open file\n"
174             << vjDEBUG_FLUSH;
175         vjDEBUG_NEXT(vjDBG_ERROR, vjDBG_CRITICAL_LVL)
176             << "'" << fname.c_str() << "'" << clrRESET << std::endl
177             << vjDEBUG_FLUSH;
178         return false;
179     }
180     in >> *this;
181     return true;
182 }
183
184
185
186 bool vjChunkDescDB::save (const char *fname) {
187     std::ofstream out(fname);
188     if (!out) {
189         vjDEBUG(vjDBG_ERROR,0) << "vjChunkDescDB::save(): Unable to open file '"
190                    << fname << "'" << std::endl << vjDEBUG_FLUSH;
191         return false;
192     }
193     out << *this;
194     return true;
195 }
Note: See TracBrowser for help on using the browser.