]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_path.cxx
Updates to build system to better support automake-1.5
[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 - curt@flightgear.org
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_config.h>
27
28 #include "sg_path.hxx"
29
30
31 // If Unix, replace all ":" with "/".  If MacOS, replace all "/" with
32 // ":" it should go without saying that neither of these characters
33 // should be used in file or directory names.  In windoze, allow the
34 // second character to be a ":" for things like c:\foo\bar
35
36 static string fix_path( const string path ) {
37     string result = path;
38
39     for ( int i = 0; i < (int)path.size(); ++i ) {
40 #if defined( WIN32 )
41         // for windoze, don't replace the ":" for the second character
42         if ( i == 1 ) {
43             continue;
44         }
45 #endif
46         if ( result[i] == SG_BAD_PATH_SEP ) {
47             result[i] = SG_PATH_SEP;
48         }
49     }
50
51     return result;
52 }
53
54
55 // default constructor
56 SGPath::SGPath() {
57     path = "";
58 }
59
60
61 // create a path based on "path"
62 SGPath::SGPath( const string p ) {
63     set( p );
64 }
65
66
67 // destructor
68 SGPath::~SGPath() {
69 }
70
71
72 // set path
73 void SGPath::set( const string p ) {
74     path = fix_path( p );
75 }
76
77
78 // append another piece to the existing path
79 void SGPath::append( const string p ) {
80     string part = fix_path( p );
81
82     if ( path.size() == 0 ) {
83         path = part;
84     } else {
85         if ( part[0] != SG_PATH_SEP ) {
86             path += SG_PATH_SEP;
87         }
88         path += part;
89     }
90 }
91
92
93 // concatenate a string to the end of the path without inserting a
94 // path separator
95 void SGPath::concat( const string p ) {
96     string part = fix_path( p );
97
98     if ( path.size() == 0 ) {
99         path = part;
100     } else {
101         path += part;
102     }
103 }
104
105
106 // get the directory part of the path.
107 string SGPath::dir() {
108     int index = path.rfind(SG_PATH_SEP);
109     if (index >= 0) {
110         return path.substr(0, index);
111     } else {
112         return "";
113     }
114 }