2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 Jan Nieuwenhuizen <janneke@gnu.org>
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.
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.
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/>.
21 #include "file-name.hh"
37 #include <sys/cygwin.h>
54 dos_to_posix (const string &file_name)
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);
65 #endif /* __CYGWIN__ */
67 /** Use slash as directory separator. On Windows, they can pretty
70 static /* avoid warning */
73 slashify (string file_name)
75 replace_all (&file_name, '\\', '/');
76 replace_all (&file_name, string ("//"), "/");
81 dir_name (const string &file_name)
85 ssize n = s.length ();
86 if (n && s[n - 1] == '/')
88 if (s.rfind ('/') != NPOS)
89 s = s.substr (0, s.rfind ('/'));
97 get_working_directory ()
100 // getcwd returns NULL upon a failure, contents of cwd would be undefined!
101 return string (getcwd (cwd, PATH_MAX));
104 /* Join components to full file_name. */
106 File_name::dir_part () const
110 s = root_ + ::to_string (ROOTSEP);
121 File_name::file_part () const
126 s += ::to_string (EXTSEP) + ext_;
131 File_name::to_string () const
133 string d = dir_part ();
134 string f = file_part ();
139 d += ::to_string (DIRSEP);
145 File_name::File_name (string file_name)
148 /* All system functions would work, even if we do not convert to
149 posix file_name, but we would think that \foe\bar\baz.ly is in
151 file_name = dos_to_posix (file_name);
154 file_name = slashify (file_name);
157 ssize i = file_name.find (ROOTSEP);
160 root_ = file_name.substr (0, i);
161 file_name = file_name.substr (i + 1);
164 i = file_name.rfind (DIRSEP);
167 dir_ = file_name.substr (0, i);
168 file_name = file_name.substr (i + 1);
171 i = file_name.rfind ('.');
174 base_ = file_name.substr (0, i);
175 ext_ = file_name.substr (i + 1);
182 File_name::is_absolute () const
185 Hmm. Is c:foo absolute?
187 return (dir_.length () && dir_[0] == DIRSEP) || root_.length ();
191 File_name::canonicalized () const
195 replace_all (&c.dir_, string ("//"), string ("/"));
197 vector<string> components = string_split (c.dir_, '/');
198 vector<string> new_components;
200 for (vsize i = 0; i < components.size (); i++)
202 if (components[i] == "..")
203 new_components.pop_back ();
205 new_components.push_back (components[i]);
208 c.dir_ = string_join (new_components, "/");