Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

FileIO.cpp

Go to the documentation of this file.
00001 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00002  *
00003  * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Christopher Just,
00007  *   Patrick Hartling, Kevin Meinert,
00008  *   Carolina Cruz-Neira, Albert Baker
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Library General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Library General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Library General Public
00021  * License along with this library; if not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00023  * Boston, MA 02111-1307, USA.
00024  *
00025  * -----------------------------------------------------------------
00026  * File:          $RCSfile: FileIO.cpp,v $
00027  * Date modified: $Date: 2003/03/06 17:12:08 $
00028  * Version:       $Revision: 1.14 $
00029  * -----------------------------------------------------------------
00030  *
00031  *************** <auto-copyright.pl END do not edit this line> ***************/
00032 
00033 #include <vrj/vrjConfig.h>
00034 
00035 #include <vpr/Util/FileUtils.h>
00036 #include <vrj/Util/Debug.h>
00037 #include <vrj/Util/FileIO.h>
00038 
00039 namespace vrj
00040 {
00041 
00042 std::vector<std::string> FileIO::mPaths;
00043 
00044 void FileIO::addFilePath( const std::string& filepath )
00045 {
00046    std::string demangled = vpr::replaceEnvVars( filepath );
00047    mPaths.push_back( demangled );
00048 }
00049 
00050 //: true -
00051 bool FileIO::fileExists( const char* const name )
00052 {
00053    std::string stdstring_name = name;
00054    std::string demangled_name = demangleFileName( stdstring_name, "" );
00055     FILE* file = ::fopen( demangled_name.c_str(), "r" );
00056     if (file == NULL)
00057     {
00058         return false;
00059     }
00060 
00061     else
00062     {
00063         ::fclose( file );
00064         return true;
00065     }
00066 }
00067 
00068 bool FileIO::fileExists( const std::string& name )
00069 {
00070    return fileExists( name.c_str() );
00071 }
00072 
00073 bool FileIO::fileExistsResolvePath( const char* const filename, std::string& realPath )
00074 {
00075    realPath = resolvePathForName( filename );
00076    return fileExists( realPath.c_str() );
00077 }
00078 
00079 bool FileIO::fileExistsResolvePath( const std::string& filename, std::string& realPath )
00080 {
00081    return fileExistsResolvePath( filename.c_str(), realPath );
00082 }
00083 
00084 std::string FileIO::resolvePathForName( const char* const filename )
00085 {
00086    std::string stdstring_name = filename;
00087    std::string demangled_name = demangleFileName( stdstring_name, "" );
00088 
00089    for (unsigned int x = 0; x < FileIO::mPaths.size(); ++x)
00090    {
00091       std::string slash = "/";
00092       std::string temp  = FileIO::mPaths[x] + slash + demangled_name;
00093 
00094       // if this path works, then return it.
00095       if (fileExists( temp ))
00096       {
00097          std::cout<<"Fixed path: "<<temp.c_str()<<"\n"<<std::flush;
00098          return temp;
00099       }
00100    }
00101 
00102    // couldn't find any that matched, so just return the filename.
00103    //cout<<"Did not need to fix path: "<<demangled_name<<"\n"<<flush;
00104    return demangled_name;
00105 }
00106 
00110 bool FileIO::isAbsolutePathName (const std::string& n)
00111 {
00112 #ifdef WIN32
00113     return ((n.length() > 0) && (n[0] == '\\'))
00114         || ((n.length() > 2) && (n[1] == ':') && (n[2] == '\\'));
00115 #else
00116     return (n.length() > 0) && (n[0] == '/');
00117 #endif
00118 }
00119 
00120 
00121 
00122 std::string FileIO::demangleFileName (const std::string& n, std::string parentfile)
00123 {
00124 
00125    std::string fname = vpr::replaceEnvVars (n);
00126 
00127    if (!isAbsolutePathName(fname))
00128    {
00129       // it's a relative pathname... so we have to add in the path part
00130       // of parentfile...
00131       //         cout << "demangling relative pathname '" << fname.c_str() << "' with parent dir '"
00132       //              << parentfile.c_str() << "'\n" << endl;
00133       int lastslash = 0;
00134       for (unsigned int i = 0; i < parentfile.length(); i++)
00135       {
00136             if (parentfile[i] == '/')
00137                 lastslash = i;
00138 #ifdef WIN32
00139             if (parentfile[i] == '\\')
00140                 lastslash = i;
00141 #endif
00142       }
00143       if (lastslash)
00144       {
00145          std::string s(parentfile, 0, lastslash+1);
00146          fname = s + n;
00147       }
00148    }
00149 
00150    return fname;
00151 }
00152 
00153 };

Generated on Sun May 2 15:10:16 2004 for VR Juggler by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002