]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-name.cc
0ff89b8d874fb32f6f189470eb3c596c6cfd22f8
[lilypond.git] / flower / file-name.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2012 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 #ifdef __CYGWIN__
37 #include <sys/cygwin.h>
38 #endif
39
40 #ifndef ROOTSEP
41 #define ROOTSEP ':'
42 #endif
43
44 #ifndef DIRSEP
45 #define DIRSEP '/'
46 #endif
47
48 #ifndef EXTSEP
49 #define EXTSEP '.'
50 #endif
51
52 #ifdef __CYGWIN__
53 static string
54 dos_to_posix (const string &file_name)
55 {
56   char buf[PATH_MAX] = "";
57   char s[PATH_MAX] = {0};
58   file_name.copy (s, PATH_MAX - 1);
59   /* ugh: char const* argument gets modified.  */
60   int fail = cygwin_conv_to_posix_path (s, buf);
61   if (!fail)
62     return buf;
63   return file_name;
64 }
65 #endif /* __CYGWIN__ */
66
67 /** Use slash as directory separator.  On Windows, they can pretty
68     much be exchanged.  */
69 #if 0
70 static /* avoid warning */
71 #endif
72 string
73 slashify (string file_name)
74 {
75   replace_all (&file_name, '\\', '/');
76   replace_all (&file_name, string ("//"), "/");
77   return file_name;
78 }
79
80 string
81 dir_name (const string &file_name)
82 {
83   string s = file_name;
84   s = slashify (s);
85   ssize n = s.length ();
86   if (n && s[n - 1] == '/')
87     s[n - 1] = 0;
88   if (s.rfind ('/') != NPOS)
89     s = s.substr (0, s.rfind ('/'));
90   else
91     s = "";
92
93   return s;
94 }
95
96 string
97 get_working_directory ()
98 {
99 #ifdef _GNU_SOURCE_
100   return string (get_current_dir_name());
101 #else
102   char cwd[PATH_MAX];
103   // getcwd returns NULL upon a failure, contents of cwd would be undefined!
104   return string (getcwd (cwd, PATH_MAX));
105 #endif
106 }
107
108 /* Join components to full file_name. */
109 string
110 File_name::dir_part () const
111 {
112   string s;
113   if (!root_.empty ())
114     s = root_ + ::to_string (ROOTSEP);
115
116   if (!dir_.empty ())
117     {
118       s += dir_;
119     }
120
121   return s;
122 }
123
124 string
125 File_name::file_part () const
126 {
127   string s;
128   s = base_;
129   if (!ext_.empty ())
130     s += ::to_string (EXTSEP) + ext_;
131   return s;
132 }
133
134 string
135 File_name::to_string () const
136 {
137   string d = dir_part ();
138   string f = file_part ();
139
140   if (!f.empty ()
141       && !dir_.empty ())
142     {
143       d += ::to_string (DIRSEP);
144     }
145
146   return d + f;
147 }
148
149 File_name::File_name (string file_name)
150 {
151 #ifdef __CYGWIN__
152   /* All system functions would work, even if we do not convert to
153      posix file_name, but we would think that \foe\bar\baz.ly is in
154      the cwd.  */
155   file_name = dos_to_posix (file_name);
156 #endif
157 #ifdef __MINGW32__
158   file_name = slashify (file_name);
159 #endif
160
161   ssize i = file_name.find (ROOTSEP);
162   if (i != NPOS)
163     {
164       root_ = file_name.substr (0, i);
165       file_name = file_name.substr (i + 1);
166     }
167
168   i = file_name.rfind (DIRSEP);
169   if (i != NPOS)
170     {
171       dir_ = file_name.substr (0, i);
172       file_name = file_name.substr (i + 1);
173     }
174
175   i = file_name.rfind ('.');
176   if (i != NPOS)
177     {
178       base_ = file_name.substr (0, i);
179       ext_ = file_name.substr (i + 1);
180     }
181   else
182     base_ = file_name;
183 }
184
185 bool
186 File_name::is_absolute () const
187 {
188   /*
189     Hmm. Is c:foo absolute?
190    */
191   return (dir_.length () && dir_[0] == DIRSEP) || root_.length ();
192 }
193
194 File_name
195 File_name::canonicalized () const
196 {
197   File_name c = *this;
198
199   replace_all (&c.dir_, string ("//"), string ("/"));
200
201   vector<string> components = string_split (c.dir_, '/');
202   vector<string> new_components;
203
204   for (vsize i = 0; i < components.size (); i++)
205     {
206       if (components[i] == "..")
207         new_components.pop_back ();
208       else
209         new_components.push_back (components[i]);
210     }
211
212   c.dir_ = string_join (new_components, "/");
213   return c;
214 }