]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_path.cxx
056a84db1a1857f4727508ecd96035107a923926
[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
191 void SGPath::create_dir( mode_t mode ) {
192     string_list dirlist = sgPathSplit(dir());
193     SGPath dir = dirlist[0];
194     int i;
195     for(i=1; dir.exists() && i < dirlist.size(); i++) {
196         dir.add(dirlist[i]);
197     }
198     for(;i < dirlist.size(); i++) {
199         string subdir = dirlist[i];
200 #ifdef _MSC_VER
201         if ( _mkdir( subdir.c_str()) ) {
202 #else
203         if ( mkdir( subdir.c_str(), mode) ) {
204 #endif
205             SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
206             break;
207         }
208         dir.add(subdir);
209     }
210 }
211
212 string_list sgPathSplit( const string &search_path ) {
213     string tmp = search_path;
214     string_list result;
215     result.clear();
216
217     bool done = false;
218
219     while ( !done ) {
220         int index = tmp.find(sgSearchPathSep);
221         if (index >= 0) {
222             result.push_back( tmp.substr(0, index) );
223             tmp = tmp.substr( index + 1 );
224         } else {
225             if ( !tmp.empty() )
226                 result.push_back( tmp );
227             done = true;
228         }
229     }
230
231     return result;
232 }