2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 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"
48 /** Use slash as directory separator. On Windows, they can pretty
51 static /* avoid warning */
54 slashify (string file_name)
56 replace_all (&file_name, '\\', '/');
57 replace_all (&file_name, string ("//"), "/");
62 dir_name (const string &file_name)
66 ssize n = s.length ();
67 if (n && s[n - 1] == '/')
69 if (s.rfind ('/') != NPOS)
70 s = s.substr (0, s.rfind ('/'));
78 get_working_directory ()
81 vector<char> cwd (PATH_MAX);
83 vector<char> cwd (1024);
85 while (getcwd (cwd.data (), cwd.size ()) == NULL)
92 cwd.resize (cwd.size () * 2);
94 return string (cwd.data ());
97 /* Join components to full file_name. */
99 File_name::dir_part () const
103 s = root_ + ::to_string (ROOTSEP);
114 File_name::file_part () const
119 s += ::to_string (EXTSEP) + ext_;
124 File_name::to_string () const
126 string d = dir_part ();
127 string f = file_part ();
132 d += ::to_string (DIRSEP);
138 File_name::File_name (string file_name)
141 file_name = slashify (file_name);
144 ssize i = file_name.find (ROOTSEP);
147 root_ = file_name.substr (0, i);
148 file_name = file_name.substr (i + 1);
151 i = file_name.rfind (DIRSEP);
154 dir_ = file_name.substr (0, i);
155 file_name = file_name.substr (i + 1);
158 i = file_name.rfind ('.');
161 base_ = file_name.substr (0, i);
162 ext_ = file_name.substr (i + 1);
169 File_name::is_absolute () const
172 Hmm. Is c:foo absolute?
174 return (dir_.length () && dir_[0] == DIRSEP) || root_.length ();
178 File_name::canonicalized () const
182 replace_all (&c.dir_, string ("//"), string ("/"));
184 vector<string> components = string_split (c.dir_, '/');
185 vector<string> new_components;
187 for (vsize i = 0; i < components.size (); i++)
189 if (components[i] == "..")
190 new_components.pop_back ();
192 new_components.push_back (components[i]);
195 c.dir_ = string_join (new_components, "/");