osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarth/FileUtils.cpp

Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
00003  * Copyright 2008-2010 Pelican Mapping
00004  * http://osgearth.org
00005  *
00006  * osgEarth is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018  */
00019 
00020 #include <osgEarth/FileUtils>
00021 #include <osgDB/FileNameUtils>
00022 #include <osg/Notify>
00023 #include <list>
00024 
00025 using namespace osgEarth;
00026 
00027 bool osgEarth::isRelativePath(const std::string& fileName)
00028 {
00029     //If it is a URL, it is not relative
00030     if (osgDB::containsServerAddress( fileName ) ) return false;
00031 
00032     std::string native = osgDB::convertFileNameToNativeStyle(fileName);
00033 #if defined(WIN32)  && !defined(__CYGWIN__)
00034     //Check to see if the path begins with a drive letter like "C:\data"
00035     if (native.size() >= 3 && native[1] == ':' && native[2] == '\\') return false;
00036 
00037     //Check to see if the path is network path like "\\server1\data"
00038     if (native.size() >= 2 && native[0] == '\\' && native[1] == '\\') return false;
00039 
00040     //The path must be relative
00041     return true;
00042 #else
00043     //Absolute paths in Unix will start with a '/'
00044     return !(fileName.size() >= 1 && fileName[0] == '/');
00045 #endif
00046 }
00047 
00048 std::string osgEarth::getFullPath(const std::string& relativeTo, const std::string &relativePath)
00049 {
00050         if (!isRelativePath(relativePath) || relativeTo.empty())
00051     {
00052         //OE_NOTICE << relativePath << " is not a relative path " << std::endl;
00053         return relativePath;
00054     }
00055 
00056     //If they didn't specify a relative path, just return the relativeTo
00057     if (relativePath.empty()) return relativeTo;
00058 
00059 
00060     //Note:  Modified from VPB
00061 
00062     //Concatinate the paths together
00063     std::string filename;
00064     if ( !osgDB::containsServerAddress( relativeTo ) )
00065         filename = osgDB::concatPaths( osgDB::getFilePath( osgDB::getRealPath( relativeTo )), relativePath);
00066     else
00067         filename = osgDB::concatPaths( osgDB::getFilePath( relativeTo ), relativePath);
00068 
00069 
00070     std::list<std::string> directories;
00071     int start = 0;
00072     for (unsigned int i = 0; i < filename.size(); ++i)
00073     {
00074         if (filename[i] == '\\' || filename[i] == '/')
00075         {
00076             //Get the current directory
00077             std::string dir = filename.substr(start, i-start);
00078 
00079             if (dir != "..")
00080             {
00081                 if (dir != ".")
00082                 {
00083                   directories.push_back(dir);
00084                 }
00085             }
00086             else if (!directories.empty())
00087             {
00088                 directories.pop_back();
00089             }
00090             start = i + 1;
00091         }
00092     }
00093 
00094     std::string path;
00095     for (std::list<std::string>::iterator itr = directories.begin();
00096          itr != directories.end();
00097          ++itr)
00098     {
00099         path += *itr;
00100         path += "/";
00101     }
00102 
00103     path += filename.substr(start, std::string::npos);
00104 
00105     //OE_NOTICE << "FullPath " << path << std::endl;
00106     return path;
00107 }
00108 
00109 bool osgEarth::isZipPath(const std::string &path)
00110 {
00111     return (path.find(".zip") != std::string::npos);
00112 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines