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"
51 /** Use slash as directory separator. On Windows, they can pretty
54 static /* avoid warning */
57 slashify (string file_name)
59 replace_all (&file_name, '\\', '/');
60 replace_all (&file_name, string ("//"), "/");
65 dir_name (const string &file_name)
69 ssize n = s.length ();
70 if (n && s[n - 1] == '/')
72 if (s.rfind ('/') != NPOS)
73 s = s.substr (0, s.rfind ('/'));
81 get_working_directory ()
84 // getcwd returns NULL upon a failure, contents of cwd would be undefined!
85 return string (getcwd (cwd, PATH_MAX));
88 /* Join components to full file_name. */
90 File_name::dir_part () const
94 s = root_ + ::to_string (ROOTSEP);
105 File_name::file_part () const
110 s += ::to_string (EXTSEP) + ext_;
115 File_name::to_string () const
117 string d = dir_part ();
118 string f = file_part ();
123 d += ::to_string (DIRSEP);
129 File_name::File_name (string file_name)
132 file_name = slashify (file_name);
135 ssize i = file_name.find (ROOTSEP);
138 root_ = file_name.substr (0, i);
139 file_name = file_name.substr (i + 1);
142 i = file_name.rfind (DIRSEP);
145 dir_ = file_name.substr (0, i);
146 file_name = file_name.substr (i + 1);
149 i = file_name.rfind ('.');
152 base_ = file_name.substr (0, i);
153 ext_ = file_name.substr (i + 1);
160 File_name::is_absolute () const
163 Hmm. Is c:foo absolute?
165 return (dir_.length () && dir_[0] == DIRSEP) || root_.length ();
169 File_name::canonicalized () const
173 replace_all (&c.dir_, string ("//"), string ("/"));
175 vector<string> components = string_split (c.dir_, '/');
176 vector<string> new_components;
178 for (vsize i = 0; i < components.size (); i++)
180 if (components[i] == "..")
181 new_components.pop_back ();
183 new_components.push_back (components[i]);
186 c.dir_ = string_join (new_components, "/");