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"
50 /** Use slash as directory separator. On Windows, they can pretty
53 static /* avoid warning */
56 slashify (string file_name)
58 replace_all (&file_name, '\\', '/');
59 replace_all (&file_name, string ("//"), "/");
64 dir_name (const string &file_name)
68 ssize n = s.length ();
69 if (n && s[n - 1] == '/')
71 if (s.rfind ('/') != NPOS)
72 s = s.substr (0, s.rfind ('/'));
80 get_working_directory ()
83 // getcwd returns NULL upon a failure, contents of cwd would be undefined!
84 return string (getcwd (cwd, PATH_MAX));
87 /* Join components to full file_name. */
89 File_name::dir_part () const
93 s = root_ + ::to_string (ROOTSEP);
104 File_name::file_part () const
109 s += ::to_string (EXTSEP) + ext_;
114 File_name::to_string () const
116 string d = dir_part ();
117 string f = file_part ();
122 d += ::to_string (DIRSEP);
128 File_name::File_name (string file_name)
131 file_name = slashify (file_name);
134 ssize i = file_name.find (ROOTSEP);
137 root_ = file_name.substr (0, i);
138 file_name = file_name.substr (i + 1);
141 i = file_name.rfind (DIRSEP);
144 dir_ = file_name.substr (0, i);
145 file_name = file_name.substr (i + 1);
148 i = file_name.rfind ('.');
151 base_ = file_name.substr (0, i);
152 ext_ = file_name.substr (i + 1);
159 File_name::is_absolute () const
162 Hmm. Is c:foo absolute?
164 return (dir_.length () && dir_[0] == DIRSEP) || root_.length ();
168 File_name::canonicalized () const
172 replace_all (&c.dir_, string ("//"), string ("/"));
174 vector<string> components = string_split (c.dir_, '/');
175 vector<string> new_components;
177 for (vsize i = 0; i < components.size (); i++)
179 if (components[i] == "..")
180 new_components.pop_back ();
182 new_components.push_back (components[i]);
185 c.dir_ = string_join (new_components, "/");