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