root/juggler/tags/1.0.7/Config/vjParseUtil.cpp

Revision 11100, 9.5 kB (checked in by patrickh, 6 years ago)

From the submitter:

"assume if (!isAbsolutePathName(fname)) and (lastslash) are true.

Then we return fname = s + n where n is the original string, with the
environment variables unresolved, and s in the directory of the parent
file.

"The change is to make fname = s + fname. fname contains the expand

version of n at the start of the statement, and the absolute file name
after the statement executes."

Submitted by: Fuel Tech Inc.

  • 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 #include <vjConfig.h>
36 #include <ctype.h>
37 #include <Config/vjParseUtil.h>
38 #include <Kernel/vjDebug.h>
39 #include <Config/vjConfigTokens.h>
40
41 /* a utility function that probably belongs elsewhere */
42 bool readString (std::istream &in, char *buffer, int size, bool *quoted) {
43     /* reads a string from in.  a string is either " delimited
44      * or contains no whitespace.
45      *
46      * returns true if a string is correctly read.  This could be a 0-length
47      * quoted string...
48      *
49      * Quoted is set true if the string read was in quotes.
50      */
51
52     size = size-5; // this is just cheezy reassurance
53     int i;
54     bool retval = false;
55     char c, vj;
56     buffer[0] = '\0';
57
58     if (quoted)
59         *quoted = false;
60
61     // read whitespace, comments
62     for (;;) {
63         if (!in.get(c))
64             break;
65         if (isspace(c))
66             continue;
67         if (c == '#') {            // shell type comment
68             while (c != '\n')
69                 in.get(c);
70             continue;
71         }
72         if ((c == '/') && in.get(vj)) {
73             /* it might be one or the other type of comment */
74             if (vj == '/') {
75                 // single line comment.
76                 while (c != '\n')
77                     if (!in.get(c))
78                         break;
79                 continue;
80             }
81             else if (vj == '*') {
82                 // multiline comment.
83                 while (true) {
84                     if (!in.get(vj))
85                         break;
86                     if (vj == '*') {
87                         in.get(vj);
88                         if (vj == '/')
89                             break;
90                     }
91                 }
92                 continue;
93             }
94             else
95                 in.putback(vj);
96         }
97         break; // we read the / character and it wasn't part of a comment
98     }
99     buffer[0] = c;
100
101     if ((buffer[0] == '{') || (buffer[0] == '}')) {
102         buffer[1] = '\0';
103         retval = true;
104     }
105     else if (buffer[0] == '"') {
106         /* do a quoted string */
107         if (quoted)
108             *quoted = true;
109         for (i = 0; i < size; i++) {
110             in.get(buffer[i]);
111             if (buffer[i] == '"') {
112                 buffer[i] = '\0';
113                 break;
114             }
115         }
116         if (i == size) {
117             i--; // so that we're writing to the last element of buffer
118             while (in.get(buffer[i]) && (buffer[i] != '"'))
119                 ;
120             buffer[i] = '\0';
121             vjDEBUG (vjDBG_ERROR,0) << clrOutNORM(clrRED, "ERROR:") << " Truncated string in config file: '"
122                                     << buffer << "'\n" << vjDEBUG_FLUSH;
123         }
124         retval = true;
125     }
126     else {
127         // should add cleaner overflow handling like above...
128         for (i = 1; i < size-1; i++) {
129             in.get(buffer[i]);
130             if (buffer[i] == '}') {
131                 // wanna push back
132                 in.putback(buffer[i]);
133                 break;
134             }
135             if (isspace(buffer[i]))
136                 break;
137         }
138         buffer[i] = '\0';
139         retval = true;
140     }
141     //cout << "readString: read string: '" << buffer << "'" << endl;
142     if (!retval)
143         buffer[0] = '\0'; // so it's safe to read it...
144     return retval;
145 }
146
147
148
149 VarType readType (std::istream &in) {
150     char str[256];
151
152     if (!readString (in, str, 256))
153         return T_INVALID;
154
155     if (!strcasecmp (str, int_TOKEN))
156         return T_INT;
157     if (!strcasecmp (str, integer_TOKEN))
158         return T_INT;
159     if (!strcasecmp (str, float_TOKEN))
160         return T_FLOAT;
161     if (!strcasecmp (str, bool_TOKEN))
162         return T_BOOL;
163     if (!strcasecmp (str, boolean_TOKEN))
164         return T_BOOL;
165     if (!strcasecmp (str, string_TOKEN))
166         return T_STRING;
167     if (!strcasecmp (str, distance_TOKEN))
168         return T_DISTANCE;
169     if (!strcasecmp (str, chunk_TOKEN))
170         return T_CHUNK;
171     if (!strcasecmp (str, embeddedchunk_TOKEN))
172         return T_EMBEDDEDCHUNK;
173
174     return T_INVALID;
175 }
176
177
178
179 char *typeString (VarType t) {
180     switch (t) {
181     case T_INT:
182         return "Int";
183     case T_BOOL:
184         return "Bool";
185     case T_FLOAT:
186         return "Float";
187     case T_STRING:
188         return "String";
189     case T_CHUNK:
190         return "Chunk";
191     case T_EMBEDDEDCHUNK:
192         return "EmbeddedChunk";
193     default:
194         return "Unrecognized_Type";
195     }
196 }
197
198
199
200 char *unitString (CfgUnit t) {
201     switch (t) {
202     case U_Feet:
203         return "Feet";
204     case U_Inches:
205         return "Inches";
206     case U_Meters:
207         return "Meters";
208     case U_Centimeters:
209         return "Centimeters";
210     default:
211         return "Invalid Unit Type";
212     }
213 }
214
215 float toFeet (float val, CfgUnit unit) {
216     switch (unit) {
217     case U_Feet:
218         return val;
219     case U_Inches:
220         return (val/12);
221     case U_Meters:
222         return (val*3.28084);
223     case U_Centimeters:
224         return (val*0.0328084);
225     default:
226         return val;
227     }
228 }
229
230
231 bool vjstrcasecmp (const std::string& a, const std::string& b) {
232     if (a.size() != b.size())
233         return true;
234     for (unsigned int i = 0; i < a.size(); i++)
235         if (toupper(a[i]) != toupper(b[i]))
236             return true;
237     return false;
238 }
239
240
241 bool vjstrncasecmp (const std::string& a, const std::string& b, int _n) {
242
243     int n = VJ_MIN2 (a.size(), b.size());
244     if (_n >= 0)
245         n = VJ_MIN2 (n, _n);
246
247     for (int i = 0; i < n; i++)
248         if (toupper(a[i]) != toupper(b[i]))
249             return true;
250     return false;
251 }
252
253
254
255 bool vjstrncmp (const std::string& a, const std::string& b, int _n) {
256
257     int n = VJ_MIN2 (a.size(), b.size());
258     if (_n >= 0)
259         n = VJ_MIN2 (n, _n);
260
261     for (int i = 0; i < n; i++)
262         if (a[i] != b[i])
263             return true;
264     return false;
265 }
266
267
268 /** filename handling routines **/
269
270 //: Returns a copy of s with all environment variable names replaced
271 //+ with their values.
272 std::string replaceEnvVars (const std::string& s) {
273     unsigned int i, j;
274     int lastpos = 0;
275     std::string result = "";
276     for (i = 0; i < s.length(); i++) {
277         if (s[i] == '$') {
278             //process an env var
279             result += std::string(s, lastpos, i - lastpos);
280             i++; // skip $
281             if (s[i] == '{') {
282                 for (j = i; j < s.length(); j++)
283                     if (s[j] == '}')
284                         break;
285                 std::string var(s,i+1,j-i-1);
286                 //cout << "searching for env var '" << var.c_str() << '\'' << endl;
287                 std::string res;
288                 char* env = getenv(var.c_str());
289                 if (env)
290                   res = env;
291                 result += res;
292                 i = j+1;
293                 lastpos = i;
294             }
295             else {
296                 for (j = i; j < s.length(); j++)
297                     if (s[j] == '/' || s[j] == '\\')
298                         break;
299                 std::string var(s,i,j-i);
300                 //cout << "searching for env var '" << var.c_str() << '\'' << endl;
301                 std::string res;
302                 char* env = getenv(var.c_str());
303                 if (env)
304                   res = env;
305                 result += res;
306                 i = j;
307                 lastpos = i;
308             }
309         }
310     }
311     result += std::string(s, lastpos, s.length() - lastpos);
312     return result;
313 }
314
315
316
317 //: is n an absolute path name?
318 bool isAbsolutePathName (const std::string& n) {
319 #ifdef WIN32
320     return ((n.length() > 0) && (n[0] == '\\'))
321         || ((n.length() > 2) && (n[1] == ':') && (n[2] == '\\'));
322 #else
323     return (n.length() > 0) && (n[0] == '/');
324 #endif
325 }
326
327
328
329 std::string demangleFileName (const std::string& n, std::string parentfile) {
330
331     std::string fname = replaceEnvVars (n);
332
333     if (!isAbsolutePathName(fname)) {
334         // it's a relative pathname... so we have to add in the path part
335         // of parentfile...
336 //         cout << "demangling relative pathname '" << fname.c_str() << "' with parent dir '"
337 //              << parentfile.c_str() << "'\n" << endl;
338         int lastslash = 0;
339         for (unsigned int i = 0; i < parentfile.length(); i++) {
340             if (parentfile[i] == '/')
341                 lastslash = i;
342 #ifdef WIN32
343             if (parentfile[i] == '\\')
344                 lastslash = i;
345 #endif
346       }
347         if (lastslash) {
348             std::string s(parentfile, 0, lastslash+1);
349             fname = s + fname;
350         }
351     }
352
353     return fname;
354 }
Note: See TracBrowser for help on using the browser.