root/juggler/tags/1.0.5/Utils/vjFileIO.cpp

Revision 2828, 6.4 kB (checked in by patrickh, 8 years ago)

Updated the copyright to what ISU's lawyers decided they want now.
The vast majority of this was done using Kevin's auto-copyright.pl script
which definitely made this easier. All the copyright blocks now have
begin and end tags so that if and when we have to update the copyright
information again, it will be even simpler.

  • 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 by Iowa State University
4  *
5  * Original Authors:
6  *   Allen Bierbaum, Christopher Just,
7  *   Patrick Hartling, Kevin Meinert,
8  *   Carolina Cruz-Neira, Albert Baker
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  * -----------------------------------------------------------------
26  * File:          $RCSfile$
27  * Date modified: $Date$
28  * Version:       $Revision$
29  * -----------------------------------------------------------------
30  *
31  *************** <auto-copyright.pl END do not edit this line> ***************/
32
33 #include <Kernel/vjDebug.h>
34 #include <Utils/vjFileIO.h>
35
36 std::vector<std::string> vjFileIO::mPaths;
37
38 //: true -
39 bool vjFileIO::fileExists( const char* const name )
40 {
41    std::string stdstring_name = name;
42    std::string demangled_name = demangleFileName( stdstring_name, "" );
43         FILE* file = ::fopen( demangled_name.c_str(), "r" );
44         if (file == NULL)
45         {
46                 return false;
47         }
48
49         else
50         {
51                 ::fclose( file );
52                 return true;
53         }
54 }
55
56 bool vjFileIO::fileExists( const std::string& name )
57 {
58    return fileExists( name.c_str() );
59 }
60
61 bool vjFileIO::fileExistsResolvePath( const char* const filename, std::string& realPath )
62 {
63    realPath = resolvePathForName( filename );
64    return fileExists( realPath.c_str() );
65 }
66
67 bool vjFileIO::fileExistsResolvePath( const std::string& filename, std::string& realPath )
68 {
69    return fileExistsResolvePath( filename.c_str(), realPath );
70 }
71
72 std::string vjFileIO::resolvePathForName( const char* const filename )
73 {
74    std::string stdstring_name = filename;
75    std::string demangled_name = demangleFileName( stdstring_name, "" );
76        
77    for (unsigned int x = 0; x < vjFileIO::mPaths.size(); ++x)
78    {
79       std::string slash = "/";
80       std::string temp  = vjFileIO::mPaths[x] + slash + demangled_name;
81      
82       // if this path works, then return it.
83       if (fileExists( temp ))
84       {
85          std::cout<<"Fixed path: "<<temp.c_str()<<"\n"<<std::flush;
86          return temp;
87       }
88    }
89    
90    // couldn't find any that matched, so just return the filename.
91    //cout<<"Did not need to fix path: "<<demangled_name<<"\n"<<flush;
92    return demangled_name;
93 }
94
95 /** filename handling routines **/
96
97 //: Returns a copy of s with all environment variable names replaced
98 //+ with their values.
99 std::string vjFileIO::replaceEnvVars( const std::string& s )
100 {
101     unsigned int i, j;
102     int lastpos = 0;
103     std::string result = "";
104     for (i = 0; i < s.length(); i++)
105     {
106         if (s[i] == '$')
107         {
108             //process an env var
109             result += std::string(s, lastpos, i - lastpos);
110             i++; // skip $
111             if (s[i] == '{')
112             {
113                // now search for the closing brace...
114                 for (j = i; j < s.length(); j++)
115                     if (s[j] == '}')
116                         break;
117                 std::string var(s,i+1,j-i-1);
118                 //cout << "searching for env var '" << var.c_str() << '\'' << endl;
119                 std::string res = getenv( var.c_str() );
120                 if (res == "")
121                 {
122                    vjDEBUG( vjDBG_ALL, 0 ) << clrOutNORM(clrYELLOW,"!!! WARNING: ENV VARIABLE NOT FOUND !!!:") << var.c_str() <<" does not exist in your environment, please set it... Your application's behaviour is now undefined!\n" << vjDEBUG_FLUSH;
123                    result += var; // don't replace the var (or replace var with var...)
124                 }
125                 else
126                 {
127                    result += res; // replace var with the found env variable.
128                 }
129                 i = j+1;
130                 lastpos = i;
131             }
132             else {
133                 for (j = i; j < s.length(); j++)
134                     if (s[j] == '/' || s[j] == '\\')
135                         break;
136                 std::string var(s,i,j-i);
137                 //cout << "searching for env var '" << var.c_str() << '\'' << endl;
138                 std::string res = getenv (var.c_str());
139                 if (res == "")
140                 {
141                    vjDEBUG( vjDBG_ALL, 0 ) << clrOutNORM(clrYELLOW,"!!! WARNING: ENV VARIABLE NOT FOUND !!!:") << var.c_str() <<" does not exist in your environment, please set it... Your application's behaviour is now undefined!\n" << vjDEBUG_FLUSH;
142                    result += var; // don't replace the var (or replace var with var...)
143                 }
144                 else
145                 {
146                    result += res; // replace var with the found env variable.
147                 }
148                 i = j;
149                 lastpos = i;
150             }
151         }
152     }
153     result += std::string(s, lastpos, s.length() - lastpos);
154     return result;
155 }
156
157
158
159 //: is n an absolute path name?
160 bool vjFileIO::isAbsolutePathName (const std::string& n)
161 {
162 #ifdef WIN32
163     return ((n.length() > 0) && (n[0] == '\\'))
164         || ((n.length() > 2) && (n[1] == ':') && (n[2] == '\\'));
165 #else
166     return (n.length() > 0) && (n[0] == '/');
167 #endif
168 }
169
170
171
172 std::string vjFileIO::demangleFileName (const std::string& n, std::string parentfile)
173 {
174
175    std::string fname = replaceEnvVars (n);
176
177    if (!isAbsolutePathName(fname))
178    {
179       // it's a relative pathname... so we have to add in the path part
180       // of parentfile...
181       //         cout << "demangling relative pathname '" << fname.c_str() << "' with parent dir '"
182       //              << parentfile.c_str() << "'\n" << endl;
183       int lastslash = 0;
184       for (unsigned int i = 0; i < parentfile.length(); i++)
185       {
186             if (parentfile[i] == '/')
187                 lastslash = i;
188 #ifdef WIN32
189             if (parentfile[i] == '\\')
190                 lastslash = i;
191 #endif
192       }
193       if (lastslash)
194       {
195          std::string s(parentfile, 0, lastslash+1);
196          fname = s + n;
197       }
198    }
199
200    return fname;
201 }
Note: See TracBrowser for help on using the browser.