]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_path.cxx
a72175ab43e87f60f8c3ac2742ddf126c80d8432
[simgear.git] / simgear / misc / sg_path.cxx
1 // sg_path.cxx -- routines to abstract out path separator differences
2 //               between MacOS and the rest of the world
3 //
4 // Written by Curtis L. Olson, started April 1999.
5 //
6 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // Library General Public License for more details.
17 //
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the
20 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 // Boston, MA  02111-1307, USA.
22 //
23 // $Id$
24
25
26 #include <simgear/compiler.h>
27
28 #include <simgear_config.h>
29 #include <simgear/debug/logstream.hxx>
30 #include <stdio.h>
31 #include <sys/stat.h>
32 #include <sys/stat.h>
33 #ifdef _MSC_VER
34 #  include <direct.h>
35 #endif
36 #include "sg_path.hxx"
37
38
39 /**
40  * define directory path separators
41  */
42
43 #if defined( macintosh )
44 static const char sgDirPathSep = ':';
45 static const char sgDirPathSepBad = '/';
46 #else
47 static const char sgDirPathSep = '/';
48 static const char sgDirPathSepBad = '\\';
49 #endif
50
51 #if defined( WIN32 ) && !defined(__CYGWIN__)
52 static const char sgSearchPathSep = ';';
53 #else
54 static const char sgSearchPathSep = ':';
55 #endif
56
57
58 // If Unix, replace all ":" with "/".  If MacOS, replace all "/" with
59 // ":" it should go without saying that neither of these characters
60 // should be used in file or directory names.  In windoze, allow the
61 // second character to be a ":" for things like c:\foo\bar
62
63 void
64 SGPath::fix()
65 {
66     for ( string::size_type i = 0; i < path.size(); ++i ) {
67 #if defined( WIN32 )
68         // for windoze, don't replace the ":" for the second character
69         if ( i == 1 ) {
70             continue;
71         }
72 #endif
73         if ( path[i] == sgDirPathSepBad ) {
74             path[i] = sgDirPathSep;
75         }
76     }
77 }
78
79
80 // default constructor
81 SGPath::SGPath()
82     : path("")
83 {
84 }
85
86
87 // create a path based on "path"
88 SGPath::SGPath( const std::string& p )
89     : path(p)
90 {
91     fix();
92 }
93
94
95 // destructor
96 SGPath::~SGPath() {
97 }
98
99
100 // set path
101 void SGPath::set( const string& p ) {
102     path = p;
103     fix();
104 }
105
106
107 // append another piece to the existing path
108 void SGPath::append( const string& p ) {
109     if ( path.size() == 0 ) {
110         path = p;
111     } else {
112         if ( p[0] != sgDirPathSep ) {
113             path += sgDirPathSep;
114         }
115         path += p;
116     }
117     fix();
118 }
119
120 //add a new path component to the existing path string
121 void SGPath::add( const string& p ) {
122     append( sgSearchPathSep+p );
123 }
124
125
126 // concatenate a string to the end of the path without inserting a
127 // path separator
128 void SGPath::concat( const string& p ) {
129     if ( path.size() == 0 ) {
130         path = p;
131     } else {
132         path += p;
133     }
134     fix();
135 }
136
137
138 // Get the file part of the path (everything after the last path sep)
139 string SGPath::file() const {
140     int index = path.rfind(sgDirPathSep);
141     if (index >= 0) {
142         return path.substr(index + 1);
143     } else {
144         return "";
145     }
146 }
147   
148
149 // get the directory part of the path.
150 string SGPath::dir() const {
151     int index = path.rfind(sgDirPathSep);
152     if (index >= 0) {
153         return path.substr(0, index);
154     } else {
155         return "";
156     }
157 }
158
159 // get the base part of the path (everything but the extension.)
160 string SGPath::base() const {
161     int index = path.rfind(".");
162     if ((index >= 0) && (path.find("/", index) == string::npos)) {
163         return path.substr(0, index);
164     } else {
165         return "";
166     }
167 }
168
169 // get the extention (everything after the final ".")
170 // but make sure no "/" follows the "." character (otherwise it
171 // is has to be a directory name containing a ".").
172 string SGPath::extension() const {
173     int index = path.rfind(".");
174     if ((index >= 0)  && (path.find("/", index) == string::npos)) {
175         return path.substr(index + 1);
176     } else {
177         return "";
178     }
179 }
180
181 bool SGPath::exists() const {
182     FILE* fp = fopen( path.c_str(), "r");
183     if (fp == 0) {
184         return false;
185     }
186     fclose(fp);
187     return true;
188 }
189
190 #ifdef _MSC_VER
191 #  define sgMkDir(d,m)       _mkdir(d)
192 #else
193 #  define sgMkDir(d,m)       mkdir(d,m)
194 #endif
195
196
197 void SGPath::create_dir( mode_t mode ) {
198     string_list dirlist = sgPathSplit(dir());
199     string path = dirlist[0];
200     string_list path_elements;
201     string element;
202     while ( path.size() ) {
203         size_t p = path.find( sgDirPathSep );
204         if ( p != string::npos ) {
205             element = path.substr( 0, p );
206             path.erase( 0, p + 1 );
207         } else {
208             element = path;
209             path = "";
210         }
211         if ( element.size() )
212             path_elements.push_back( element );
213     }
214
215     int i = 1;
216     SGPath dir = path_elements[0];
217 #ifdef WIN32
218     if ( path_elements.size() >= 2 ) {
219         dir.append( path_elements[1] );
220         i = 2;
221     }
222 #endif
223     struct stat info;
224     int r;
225     for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
226         dir.append(path_elements[i]);
227     }
228     if ( r == 0 ) {
229         return; // Directory already exists
230     }
231     if ( sgMkDir( dir.c_str(), mode) ) {
232         SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
233         return;
234     }
235     for(;i < path_elements.size(); i++) {
236         dir.append(path_elements[i]);
237         if ( sgMkDir( dir.c_str(), mode) ) {
238             SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
239             break;
240         }
241     }
242 }
243
244 string_list sgPathSplit( const string &search_path ) {
245     string tmp = search_path;
246     string_list result;
247     result.clear();
248
249     bool done = false;
250
251     while ( !done ) {
252         int index = tmp.find(sgSearchPathSep);
253         if (index >= 0) {
254             result.push_back( tmp.substr(0, index) );
255             tmp = tmp.substr( index + 1 );
256         } else {
257             if ( !tmp.empty() )
258                 result.push_back( tmp );
259             done = true;
260         }
261     }
262
263     return result;
264 }