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 char *cwd = get_current_dir_name();
87 // getcwd returns NULL upon a failure, contents of cwd would be undefined!
88 return string (getcwd (cwd, PATH_MAX));
92 /* Join components to full file_name. */
94 File_name::dir_part () const
98 s = root_ + ::to_string (ROOTSEP);
109 File_name::file_part () const
114 s += ::to_string (EXTSEP) + ext_;
119 File_name::to_string () const
121 string d = dir_part ();
122 string f = file_part ();
127 d += ::to_string (DIRSEP);
133 File_name::File_name (string file_name)
136 file_name = slashify (file_name);
139 ssize i = file_name.find (ROOTSEP);
142 root_ = file_name.substr (0, i);
143 file_name = file_name.substr (i + 1);
146 i = file_name.rfind (DIRSEP);
149 dir_ = file_name.substr (0, i);
150 file_name = file_name.substr (i + 1);
153 i = file_name.rfind ('.');
156 base_ = file_name.substr (0, i);
157 ext_ = file_name.substr (i + 1);
164 File_name::is_absolute () const
167 Hmm. Is c:foo absolute?
169 return (dir_.length () && dir_[0] == DIRSEP) || root_.length ();
173 File_name::canonicalized () const
177 replace_all (&c.dir_, string ("//"), string ("/"));
179 vector<string> components = string_split (c.dir_, '/');
180 vector<string> new_components;
182 for (vsize i = 0; i < components.size (); i++)
184 if (components[i] == "..")
185 new_components.pop_back ();
187 new_components.push_back (components[i]);
190 c.dir_ = string_join (new_components, "/");