]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_path.cxx
dd0b70683e1f5049f32157ef3a074669e7b1885a
[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
34 #include "sg_path.hxx"
35
36
37 /**
38  * define directory path separators
39  */
40
41 #if defined( macintosh )
42 static const char sgDirPathSep = ':';
43 static const char sgDirPathSepBad = '/';
44 #else
45 static const char sgDirPathSep = '/';
46 static const char sgDirPathSepBad = '\\';
47 #endif
48
49 #if defined( WIN32 ) && !defined(__CYGWIN__)
50 static const char sgSearchPathSep = ';';
51 #else
52 static const char sgSearchPathSep = ':';
53 #endif
54
55
56 // If Unix, replace all ":" with "/".  If MacOS, replace all "/" with
57 // ":" it should go without saying that neither of these characters
58 // should be used in file or directory names.  In windoze, allow the
59 // second character to be a ":" for things like c:\foo\bar
60
61 void
62 SGPath::fix()
63 {
64     for ( string::size_type i = 0; i < path.size(); ++i ) {
65 #if defined( WIN32 )
66         // for windoze, don't replace the ":" for the second character
67         if ( i == 1 ) {
68             continue;
69         }
70 #endif
71         if ( path[i] == sgDirPathSepBad ) {
72             path[i] = sgDirPathSep;
73         }
74     }
75 }
76
77
78 // default constructor
79 SGPath::SGPath()
80     : path("")
81 {
82 }
83
84
85 // create a path based on "path"
86 SGPath::SGPath( const std::string& p )
87     : path(p)
88 {
89     fix();
90 }
91
92
93 // destructor
94 SGPath::~SGPath() {
95 }
96
97
98 // set path
99 void SGPath::set( const string& p ) {
100     path = p;
101     fix();
102 }
103
104
105 // append another piece to the existing path
106 void SGPath::append( const string& p ) {
107     if ( path.size() == 0 ) {
108         path = p;
109     } else {
110         if ( p[0] != sgDirPathSep ) {
111             path += sgDirPathSep;
112         }
113         path += p;
114     }
115     fix();
116 }
117
118 //add a new path component to the existing path string
119 void SGPath::add( const string& p ) {
120     append( sgSearchPathSep+p );
121 }
122
123
124 // concatenate a string to the end of the path without inserting a
125 // path separator
126 void SGPath::concat( const string& p ) {
127     if ( path.size() == 0 ) {
128         path = p;
129     } else {
130         path += p;
131     }
132     fix();
133 }
134
135
136 // Get the file part of the path (everything after the last path sep)
137 string SGPath::file() const {
138     int index = path.rfind(sgDirPathSep);
139     if (index >= 0) {
140         return path.substr(index + 1);
141     } else {
142         return "";
143     }
144 }
145   
146
147 // get the directory part of the path.
148 string SGPath::dir() const {
149     int index = path.rfind(sgDirPathSep);
150     if (index >= 0) {
151         return path.substr(0, index);
152     } else {
153         return "";
154     }
155 }
156
157 // get the base part of the path (everything but the extension.)
158 string SGPath::base() const {
159     int index = path.rfind(".");
160     if ((index >= 0) && (path.find("/", index) == string::npos)) {
161         return path.substr(0, index);
162     } else {
163         return "";
164     }
165 }
166
167 // get the extention (everything after the final ".")
168 // but make sure no "/" follows the "." character (otherwise it
169 // is has to be a directory name containing a ".").
170 string SGPath::extension() const {
171     int index = path.rfind(".");
172     if ((index >= 0)  && (path.find("/", index) == string::npos)) {
173         return path.substr(index + 1);
174     } else {
175         return "";
176     }
177 }
178
179 bool SGPath::exists() const {
180     FILE* fp = fopen( path.c_str(), "r");
181     if (fp == 0) {
182         return false;
183     }
184     fclose(fp);
185     return true;
186 }
187
188
189 void SGPath::create_dir( mode_t mode ) {
190     string_list dirlist = sgPathSplit(dir());
191     SGPath dir = dirlist[0];
192     int i;
193     for(i=1; dir.exists() && i < dirlist.size(); i++) {
194         dir.add(dirlist[i]);
195     }
196     for(;i < dirlist.size(); i++) {
197         string subdir = dirlist[i];
198         if ( mkdir( subdir.c_str(), mode) ) {
199             SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
200             break;
201         }
202         dir.add(subdir);
203     }
204 }
205
206 string_list sgPathSplit( const string &search_path ) {
207     string tmp = search_path;
208     string_list result;
209     result.clear();
210
211     bool done = false;
212
213     while ( !done ) {
214         int index = tmp.find(sgSearchPathSep);
215         if (index >= 0) {
216             result.push_back( tmp.substr(0, index) );
217             tmp = tmp.substr( index + 1 );
218         } else {
219             if ( !tmp.empty() )
220                 result.push_back( tmp );
221             done = true;
222         }
223     }
224
225     return result;
226 }