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