]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-name.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / flower / file-name.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5   Jan Nieuwenhuizen <janneke@gnu.org>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "file-name.hh"
22
23 #include <cstdio>
24 #include <cerrno>
25 #include <unistd.h>
26 #include <limits.h>
27
28 using namespace std;
29
30 #include "config.hh"
31
32 #if HAVE_SYS_STAT_H
33 #include <sys/stat.h>
34 #endif
35
36 using std::string;
37 using std::vector;
38
39 #ifndef ROOTSEP
40 #define ROOTSEP ':'
41 #endif
42
43 #ifndef DIRSEP
44 #define DIRSEP '/'
45 #endif
46
47 #ifndef EXTSEP
48 #define EXTSEP '.'
49 #endif
50
51 /** Use slash as directory separator.  On Windows, they can pretty
52     much be exchanged.  */
53 #if 0
54 static /* avoid warning */
55 #endif
56 string
57 slashify (string file_name)
58 {
59   replace_all (&file_name, '\\', '/');
60   replace_all (&file_name, string ("//"), "/");
61   return file_name;
62 }
63
64 string
65 dir_name (const string &file_name)
66 {
67   string s = file_name;
68   s = slashify (s);
69   ssize n = s.length ();
70   if (n && s[n - 1] == '/')
71     s[n - 1] = 0;
72   if (s.rfind ('/') != NPOS)
73     s = s.substr (0, s.rfind ('/'));
74   else
75     s = "";
76
77   return s;
78 }
79
80 string
81 get_working_directory ()
82 {
83   char cwd[PATH_MAX];
84   // getcwd returns NULL upon a failure, contents of cwd would be undefined!
85   return string (getcwd (cwd, PATH_MAX));
86 }
87
88 /* Join components to full file_name. */
89 string
90 File_name::dir_part () const
91 {
92   string s;
93   if (!root_.empty ())
94     s = root_ + ::to_string (ROOTSEP);
95
96   if (!dir_.empty ())
97     {
98       s += dir_;
99     }
100
101   return s;
102 }
103
104 string
105 File_name::file_part () const
106 {
107   string s;
108   s = base_;
109   if (!ext_.empty ())
110     s += ::to_string (EXTSEP) + ext_;
111   return s;
112 }
113
114 string
115 File_name::to_string () const
116 {
117   string d = dir_part ();
118   string f = file_part ();
119
120   if (!f.empty ()
121       && !dir_.empty ())
122     {
123       d += ::to_string (DIRSEP);
124     }
125
126   return d + f;
127 }
128
129 File_name::File_name (string file_name)
130 {
131 #ifdef __MINGW32__
132   file_name = slashify (file_name);
133 #endif
134
135   ssize i = file_name.find (ROOTSEP);
136   if (i != NPOS)
137     {
138       root_ = file_name.substr (0, i);
139       file_name = file_name.substr (i + 1);
140     }
141
142   i = file_name.rfind (DIRSEP);
143   if (i != NPOS)
144     {
145       dir_ = file_name.substr (0, i);
146       file_name = file_name.substr (i + 1);
147     }
148
149   i = file_name.rfind ('.');
150   if (i != NPOS)
151     {
152       base_ = file_name.substr (0, i);
153       ext_ = file_name.substr (i + 1);
154     }
155   else
156     base_ = file_name;
157 }
158
159 bool
160 File_name::is_absolute () const
161 {
162   /*
163     Hmm. Is c:foo absolute?
164    */
165   return (dir_.length () && dir_[0] == DIRSEP) || root_.length ();
166 }
167
168 File_name
169 File_name::canonicalized () const
170 {
171   File_name c = *this;
172
173   replace_all (&c.dir_, string ("//"), string ("/"));
174
175   vector<string> components = string_split (c.dir_, '/');
176   vector<string> new_components;
177
178   for (vsize i = 0; i < components.size (); i++)
179     {
180       if (components[i] == "..")
181         new_components.pop_back ();
182       else
183         new_components.push_back (components[i]);
184     }
185
186   c.dir_ = string_join (new_components, "/");
187   return c;
188 }