nux-1.14.0
|
00001 /* 00002 * Copyright 2010 Inalogic® Inc. 00003 * 00004 * This program is free software: you can redistribute it and/or modify it 00005 * under the terms of the GNU Lesser General Public License, as 00006 * published by the Free Software Foundation; either version 2.1 or 3.0 00007 * of the License. 00008 * 00009 * This program is distributed in the hope that it will be useful, but 00010 * WITHOUT ANY WARRANTY; without even the implied warranties of 00011 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 00012 * PURPOSE. See the applicable version of the GNU Lesser General Public 00013 * License for more details. 00014 * 00015 * You should have received a copy of both the GNU Lesser General Public 00016 * License along with this program. If not, see <http://www.gnu.org/licenses/> 00017 * 00018 * Authored by: Jay Taoko <jaytaoko@inalogic.com> 00019 * 00020 */ 00021 00022 00023 #include "NuxCore.h" 00024 00025 #include "FilePath.h" 00026 00027 namespace nux 00028 { 00029 00030 FilePath::FilePath() 00031 { 00032 if (std::find (m_SearchPath.begin(), m_SearchPath.end(), TEXT ("") ) != m_SearchPath.end() ) 00033 return; 00034 00035 m_SearchPath.push_back (TEXT ("") ); // for fully qualified path 00036 } 00037 00038 FilePath::~FilePath() 00039 { 00040 00041 } 00042 00043 void FilePath::AddSearchPath (const NString &searchpath) 00044 { 00045 if (std::find (m_SearchPath.begin(), m_SearchPath.end(), searchpath) != m_SearchPath.end() ) 00046 return; 00047 00048 m_SearchPath.push_back (searchpath); 00049 } 00050 00051 void FilePath::AddSearchPath (const std::vector<NString>& searchpath) 00052 { 00053 for (t_u32 i = 0; i < searchpath.size(); i++) 00054 { 00055 if (std::find (m_SearchPath.begin(), m_SearchPath.end(), searchpath[i]) == m_SearchPath.end() ) 00056 m_SearchPath.push_back (searchpath[i]); 00057 } 00058 } 00059 00060 NString FilePath::GetPathToFile (const TCHAR *filename) const 00061 { 00062 NString FileName = GetFile (filename); 00063 00064 int loc = (int) FileName.FindLastOccurence (TEXT ('\\') ); 00065 00066 if (loc == -1) 00067 { 00068 loc = (int) FileName.FindLastOccurence (TEXT ('/') ); 00069 } 00070 00071 if (loc != -1) 00072 FileName = FileName.GetSubString (0, loc); 00073 else 00074 FileName = NString (TEXT (".") ); 00075 00076 return FileName; 00077 } 00078 00079 NString FilePath::GetFile (const TCHAR *filename) const 00080 { 00081 NUX_RETURN_VALUE_IF_NULL (filename, NString (TEXT ("") ) ); 00082 00083 if (NString (filename) == NString (TEXT ("") ) ) 00084 return NString (TEXT ("") ); 00085 00086 NString FileName = filename; 00087 00088 if (GFileManager.FileExist (FileName.GetTCharPtr() ) ) 00089 return FileName; 00090 00091 for (t_u32 i = 0; i < m_SearchPath.size(); i++) 00092 { 00093 if (m_SearchPath[i].Size() == 0) 00094 continue; 00095 00096 NString FilePath; 00097 00098 if ((m_SearchPath[i].GetLastChar () == TEXT('/')) || (m_SearchPath[i].GetLastChar () == TEXT('\\'))) 00099 FilePath = m_SearchPath[i] + filename; 00100 else 00101 FilePath = m_SearchPath[i] + NUX_PATH_SEPARATOR_STRING + filename; 00102 00103 00104 if (GFileManager.FileExist (FilePath.GetTCharPtr() ) ) 00105 return FilePath; 00106 } 00107 00108 // Still not found. Then peel off the root of the filename and append our custom search path. 00109 // filename = "MediaProg/UI3DGraphics/MyFile.txt" 00110 // search for: 00111 // CustomPath0/UI3DGraphics/MyFile.txt 00112 // CustomPath1/UI3DGraphics/MyFile.txt 00113 // CustomPath2/UI3DGraphics/MyFile.txt 00114 // CustomPath3/UI3DGraphics/MyFile.txt 00115 // CustomPath0/MyFile.txt 00116 // CustomPath1/MyFile.txt 00117 // CustomPath2/MyFile.txt 00118 // CustomPath3/MyFile.txt 00119 // 00120 00121 FileName = filename; 00122 00123 for (t_size i = 0; i < m_SearchPath.size(); i++) 00124 { 00125 t_size pos; 00126 NString PathName; 00127 00128 while (FileName.FindFirstOccurenceOf (TEXT ("\\/") ) != std::string::npos) 00129 { 00130 pos = FileName.FindFirstOccurenceOf (TEXT ("\\/") ) + 1; 00131 00132 FileName = FileName.GetSubString (pos, FileName.Length() - pos); 00133 00134 if ((m_SearchPath[i].GetLastChar () == TEXT('/')) || (m_SearchPath[i].GetLastChar () == TEXT('\\'))) 00135 PathName = m_SearchPath[i] + FileName; 00136 else 00137 PathName = m_SearchPath[i] + NUX_PATH_SEPARATOR_STRING + FileName; 00138 00139 00140 if (GFileManager.FileExist (PathName.GetTCharPtr() ) ) 00141 return PathName; 00142 } 00143 } 00144 00145 nuxDebugMsg (TEXT ("[FilePath::GetFile] Cannot find file: %s"), filename); 00146 return NString (TEXT ("") ); 00147 } 00148 00149 }