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 // getcwd returns NULL upon a failure, contents of cwd would be undefined!
82 return string (getcwd (cwd, PATH_MAX));
85 /* Join components to full file_name. */
87 File_name::dir_part () const
91 s = root_ + ::to_string (ROOTSEP);
102 File_name::file_part () const
107 s += ::to_string (EXTSEP) + ext_;
112 File_name::to_string () const
114 string d = dir_part ();
115 string f = file_part ();
120 d += ::to_string (DIRSEP);
126 File_name::File_name (string file_name)
129 file_name = slashify (file_name);
132 ssize i = file_name.find (ROOTSEP);
135 root_ = file_name.substr (0, i);
136 file_name = file_name.substr (i + 1);
139 i = file_name.rfind (DIRSEP);
142 dir_ = file_name.substr (0, i);
143 file_name = file_name.substr (i + 1);
146 i = file_name.rfind ('.');
149 base_ = file_name.substr (0, i);
150 ext_ = file_name.substr (i + 1);
157 File_name::is_absolute () const
160 Hmm. Is c:foo absolute?
162 return (dir_.length () && dir_[0] == DIRSEP) || root_.length ();
166 File_name::canonicalized () const
170 replace_all (&c.dir_, string ("//"), string ("/"));
172 vector<string> components = string_split (c.dir_, '/');
173 vector<string> new_components;
175 for (vsize i = 0; i < components.size (); i++)
177 if (components[i] == "..")
178 new_components.pop_back ();
180 new_components.push_back (components[i]);
183 c.dir_ = string_join (new_components, "/");