/[pcsx2_0.9.7]/trunk/common/src/Utilities/PathUtils.cpp
ViewVC logotype

Contents of /trunk/common/src/Utilities/PathUtils.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (show annotations) (download)
Thu Dec 23 12:02:12 2010 UTC (9 years, 1 month ago) by william
File size: 6648 byte(s)
re-commit (had local access denied errors when committing)
1 /* PCSX2 - PS2 Emulator for PCs
2 * Copyright (C) 2002-2010 PCSX2 Dev Team
3 *
4 * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5 * of the GNU Lesser General Public License as published by the Free Software Found-
6 * ation, either version 3 of the License, or (at your option) any later version.
7 *
8 * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 * PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with PCSX2.
13 * If not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #include "PrecompiledHeader.h"
17 #include "Path.h"
18
19 #include <wx/file.h>
20 #include <wx/utils.h>
21
22 // ---------------------------------------------------------------------------------
23 // wxDirName (implementations)
24 // ---------------------------------------------------------------------------------
25
26 wxFileName wxDirName::Combine( const wxFileName& right ) const
27 {
28 pxAssertMsg( IsDir(), L"Warning: Malformed directory name detected during wxDirName concatenation." );
29 if( right.IsAbsolute() )
30 return right;
31
32 // Append any directory parts from right, and then set the filename.
33 // Except we can't do that because our m_members are private (argh!) and there is no API
34 // for getting each component of the path. So instead let's use Normalize:
35
36 wxFileName result( right );
37 result.Normalize( wxPATH_NORM_ENV_VARS | wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE, GetPath() );
38 return result;
39 }
40
41 wxDirName wxDirName::Combine( const wxDirName& right ) const
42 {
43 pxAssertMsg( IsDir() && right.IsDir(), L"Warning: Malformed directory name detected during wDirName concatenation." );
44
45 wxDirName result( right );
46 result.Normalize( wxPATH_NORM_ENV_VARS | wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE, GetPath() );
47 return result;
48 }
49
50 wxDirName& wxDirName::Normalize( int flags, const wxString& cwd )
51 {
52 pxAssertMsg( IsDir(), L"Warning: Malformed directory name detected during wDirName normalization." );
53 if( !wxFileName::Normalize( flags, cwd ) )
54 throw Exception::ParseError().SetDiagMsg( L"wxDirName::Normalize operation failed." );
55 return *this;
56 }
57
58 wxDirName& wxDirName::MakeRelativeTo( const wxString& pathBase )
59 {
60 pxAssertMsg( IsDir(), L"Warning: Malformed directory name detected during wDirName normalization." );
61 if( !wxFileName::MakeRelativeTo( pathBase ) )
62 throw Exception::ParseError().SetDiagMsg( L"wxDirName::MakeRelativeTo operation failed." );
63 return *this;
64 }
65
66 wxDirName& wxDirName::MakeAbsolute( const wxString& cwd )
67 {
68 pxAssertMsg( IsDir(), L"Warning: Malformed directory name detected during wDirName normalization." );
69 if( !wxFileName::MakeAbsolute( cwd ) )
70 throw Exception::ParseError().SetDiagMsg( L"wxDirName::MakeAbsolute operation failed." );
71 return *this;
72 }
73
74 void wxDirName::Rmdir()
75 {
76 if( !Exists() ) return;
77 wxFileName::Rmdir();
78 // TODO : Throw exception if operation failed? Do we care?
79 }
80
81 bool wxDirName::Mkdir()
82 {
83 if( Exists() ) return true;
84
85 // Recursively create child directories as needed:
86 wxDirName recurse( *this );
87 recurse.RemoveLastDir();
88 if( !recurse.Mkdir() ) return false;
89
90 return wxFileName::Mkdir();
91 }
92
93
94 // ---------------------------------------------------------------------------------
95 // Path namespace (wxFileName helpers)
96 // ---------------------------------------------------------------------------------
97
98
99 bool Path::IsRelative( const wxString& path )
100 {
101 return wxDirName( path ).IsRelative();
102 }
103
104 // Returns -1 if the file does not exist.
105 s64 Path::GetFileSize( const wxString& path )
106 {
107 if( !wxFile::Exists( path.c_str() ) ) return -1;
108 return (s64)wxFileName::GetSize( path ).GetValue();
109 }
110
111
112 wxString Path::Normalize( const wxString& src )
113 {
114 wxFileName normalize( src );
115 normalize.Normalize();
116 return normalize.GetFullPath();
117 }
118
119 wxString Path::Normalize( const wxDirName& src )
120 {
121 return wxDirName(src).Normalize().ToString();
122 }
123
124 wxString Path::MakeAbsolute( const wxString& src )
125 {
126 wxFileName absolute( src );
127 absolute.MakeAbsolute();
128 return absolute.GetFullPath();
129 }
130
131 // Concatenates two pathnames together, inserting delimiters (backslash on win32)
132 // as needed! Assumes the 'dest' is allocated to at least g_MaxPath length.
133 //
134 wxString Path::Combine( const wxString& srcPath, const wxString& srcFile )
135 {
136 return (wxDirName( srcPath ) + srcFile).GetFullPath();
137 }
138
139 wxString Path::Combine( const wxDirName& srcPath, const wxFileName& srcFile )
140 {
141 return (srcPath + srcFile).GetFullPath();
142 }
143
144 wxString Path::Combine( const wxString& srcPath, const wxDirName& srcFile )
145 {
146 return (wxDirName( srcPath ) + srcFile).ToString();
147 }
148
149 // Replaces the extension of the file with the one given.
150 // This function works for path names as well as file names.
151 wxString Path::ReplaceExtension( const wxString& src, const wxString& ext )
152 {
153 wxFileName jojo( src );
154 jojo.SetExt( ext );
155 return jojo.GetFullPath();
156 }
157
158 wxString Path::ReplaceFilename( const wxString& src, const wxString& newfilename )
159 {
160 wxFileName jojo( src );
161 jojo.SetFullName( newfilename );
162 return jojo.GetFullPath();
163 }
164
165 wxString Path::GetFilename( const wxString& src )
166 {
167 return wxFileName(src).GetFullName();
168 }
169
170 wxString Path::GetFilenameWithoutExt( const wxString& src )
171 {
172 return wxFileName(src).GetName();
173 }
174
175 wxString Path::GetDirectory( const wxString& src )
176 {
177 return wxFileName(src).GetPath();
178 }
179
180
181 // returns the base/root directory of the given path.
182 // Example /this/that/something.txt -> dest == "/"
183 wxString Path::GetRootDirectory( const wxString& src )
184 {
185 size_t pos = src.find_first_of( wxFileName::GetPathSeparators() );
186 if( pos == wxString::npos )
187 return wxString();
188 else
189 return wxString( src.begin(), src.begin()+pos );
190 }
191
192 // ------------------------------------------------------------------------
193 // Launches the specified file according to its mime type
194 //
195 void pxLaunch( const wxString& filename )
196 {
197 wxLaunchDefaultBrowser( filename );
198 }
199
200 void pxLaunch(const char *filename)
201 {
202 pxLaunch( fromUTF8(filename) );
203 }
204
205 // ------------------------------------------------------------------------
206 // Launches a file explorer window on the specified path. If the given path is not
207 // a qualified URI (with a prefix:// ), file:// is automatically prepended. This
208 // bypasses wxWidgets internal filename checking, which can end up launching things
209 // through browser more often than desired.
210 //
211 void pxExplore( const wxString& path )
212 {
213 wxLaunchDefaultBrowser( !path.Contains( L"://") ? L"file://" + path : path );
214 }
215
216 void pxExplore(const char *path)
217 {
218 pxExplore( fromUTF8(path) );
219 }

  ViewVC Help
Powered by ViewVC 1.1.22