]> git.mxchange.org Git - simgear.git/blob - simgear/misc/fgpath.cxx
Added a touch of error checking to the screen dump routine, i.e. don't
[simgear.git] / simgear / misc / fgpath.cxx
1 //
2 // fgpath.cxx -- routines to abstract out path separator differences
3 //               between MacOS and the rest of the world
4 //
5 // Written by Curtis L. Olson, started April 1999.
6 //
7 // Copyright (C) 1999  Curtis L. Olson - curt@flightgear.org
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Library General Public
11 // License as published by the Free Software Foundation; either
12 // version 2 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // Library General Public License for more details.
18 //
19 // You should have received a copy of the GNU Library General Public
20 // License along with this library; if not, write to the
21 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 // Boston, MA  02111-1307, USA.
23 //
24 // $Id$
25
26
27 #include "fgpath.hxx"
28
29
30 // If Unix, replace all ":" with "/".  If MacOS, replace all "/" with
31 // ":" it should go without saying that neither of these characters
32 // should be used in file or directory names.  In windoze, allow the
33 // second character to be a ":" for things like c:\foo\bar
34
35 static string fix_path( const string path ) {
36     string result = path;
37
38     for ( int i = 0; i < (int)path.size(); ++i ) {
39 #if defined( WIN32 )
40         // for windoze, don't replace the ":" for the second character
41         if ( i == 1 ) {
42             continue;
43         }
44 #endif
45         if ( result[i] == FG_BAD_PATH_SEP ) {
46             result[i] = FG_PATH_SEP;
47         }
48     }
49
50     return result;
51 }
52
53
54 // default constructor
55 FGPath::FGPath() {
56     path = "";
57 }
58
59
60 // create a path based on "path"
61 FGPath::FGPath( const string p ) {
62     set( p );
63 }
64
65
66 // destructor
67 FGPath::~FGPath() {
68 }
69
70
71 // set path
72 void FGPath::set( const string p ) {
73     path = fix_path( p );
74 }
75
76
77 // append another piece to the existing path
78 void FGPath::append( const string p ) {
79     string part = fix_path( p );
80
81     if ( path.size() == 0 ) {
82         path = part;
83     } else {
84         if ( part[0] != FG_PATH_SEP ) {
85             path += FG_PATH_SEP;
86         }
87         path += part;
88     }
89 }
90
91
92 // concatenate a string to the end of the path without inserting a
93 // path separator
94 void FGPath::concat( const string p ) {
95     string part = fix_path( p );
96
97     if ( path.size() == 0 ) {
98         path = part;
99     } else {
100         path += part;
101     }
102 }