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