]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-name.cc
Issue 4550 (2/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
29 #include "config.hh"
30
31 #if HAVE_SYS_STAT_H
32 #include <sys/stat.h>
33 #endif
34
35 using std::string;
36 using std::vector;
37
38 #ifndef ROOTSEP
39 #define ROOTSEP ':'
40 #endif
41
42 #ifndef DIRSEP
43 #define DIRSEP '/'
44 #endif
45
46 #ifndef EXTSEP
47 #define EXTSEP '.'
48 #endif
49
50 /** Use slash as directory separator.  On Windows, they can pretty
51     much be exchanged.  */
52 #if 0
53 static /* avoid warning */
54 #endif
55 string
56 slashify (string file_name)
57 {
58   replace_all (&file_name, '\\', '/');
59   replace_all (&file_name, string ("//"), "/");
60   return file_name;
61 }
62
63 string
64 dir_name (const string &file_name)
65 {
66   string s = file_name;
67   s = slashify (s);
68   ssize n = s.length ();
69   if (n && s[n - 1] == '/')
70     s[n - 1] = 0;
71   if (s.rfind ('/') != NPOS)
72     s = s.substr (0, s.rfind ('/'));
73   else
74     s = "";
75
76   return s;
77 }
78
79 string
80 get_working_directory ()
81 {
82   char cwd[PATH_MAX];
83   // getcwd returns NULL upon a failure, contents of cwd would be undefined!
84   return string (getcwd (cwd, PATH_MAX));
85 }
86
87 /* Join components to full file_name. */
88 string
89 File_name::dir_part () const
90 {
91   string s;
92   if (!root_.empty ())
93     s = root_ + ::to_string (ROOTSEP);
94
95   if (!dir_.empty ())
96     {
97       s += dir_;
98     }
99
100   return s;
101 }
102
103 string
104 File_name::file_part () const
105 {
106   string s;
107   s = base_;
108   if (!ext_.empty ())
109     s += ::to_string (EXTSEP) + ext_;
110   return s;
111 }
112
113 string
114 File_name::to_string () const
115 {
116   string d = dir_part ();
117   string f = file_part ();
118
119   if (!f.empty ()
120       && !dir_.empty ())
121     {
122       d += ::to_string (DIRSEP);
123     }
124
125   return d + f;
126 }
127
128 File_name::File_name (string file_name)
129 {
130 #ifdef __MINGW32__
131   file_name = slashify (file_name);
132 #endif
133
134   ssize i = file_name.find (ROOTSEP);
135   if (i != NPOS)
136     {
137       root_ = file_name.substr (0, i);
138       file_name = file_name.substr (i + 1);
139     }
140
141   i = file_name.rfind (DIRSEP);
142   if (i != NPOS)
143     {
144       dir_ = file_name.substr (0, i);
145       file_name = file_name.substr (i + 1);
146     }
147
148   i = file_name.rfind ('.');
149   if (i != NPOS)
150     {
151       base_ = file_name.substr (0, i);
152       ext_ = file_name.substr (i + 1);
153     }
154   else
155     base_ = file_name;
156 }
157
158 bool
159 File_name::is_absolute () const
160 {
161   /*
162     Hmm. Is c:foo absolute?
163    */
164   return (dir_.length () && dir_[0] == DIRSEP) || root_.length ();
165 }
166
167 File_name
168 File_name::canonicalized () const
169 {
170   File_name c = *this;
171
172   replace_all (&c.dir_, string ("//"), string ("/"));
173
174   vector<string> components = string_split (c.dir_, '/');
175   vector<string> new_components;
176
177   for (vsize i = 0; i < components.size (); i++)
178     {
179       if (components[i] == "..")
180         new_components.pop_back ();
181       else
182         new_components.push_back (components[i]);
183     }
184
185   c.dir_ = string_join (new_components, "/");
186   return c;
187 }