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-path.hh"
32 #include <sys/cygwin.h>
35 #include "file-name.hh"
48 File_path::directories () const
54 File_path::parse_path (const string &p)
56 concat (dirs_, string_split (p, PATHSEP));
60 is_file (const string &file_name)
62 #if !STAT_MACROS_BROKEN
64 if (stat (file_name.c_str (), &sbuf) != 0)
67 return !S_ISDIR (sbuf.st_mode);
70 if (FILE *f = fopen (file_name.c_str (), "r"))
80 is_dir (string file_name)
83 canonicalize; in particular, trailing slashes should disappear.
85 file_name = File_name (file_name).to_string ();
87 #if !STAT_MACROS_BROKEN
89 if (stat (file_name.c_str (), &sbuf) != 0)
92 return S_ISDIR (sbuf.st_mode);
95 if (FILE *f = fopen (file_name.c_str (), "r"))
105 Check absolute file name, search in the current dir (DUH! FIXME!),
106 in the construction-arg (what's that?), and in any other appended
107 directory, in this order.
110 The file name if found, or empty string if not found. */
113 File_path::find (const string &name) const
115 if (!name.length () || (name == "-"))
119 if (name.find ('\\') != NPOS)
120 programming_error ("file name not normalized: " + name);
121 #endif /* __MINGW32__ */
123 /* Handle absolute file name. */
124 File_name file_name (name);
125 if (file_name.dir_[0] == DIRSEP && is_file (file_name.to_string ()))
126 return file_name.to_string ();
128 for (vsize i = 0; i < dirs_.size (); i++)
130 File_name file_name (name);
131 File_name dir = (string) dirs_[i];
132 file_name.root_ = dir.root_;
134 if (file_name.dir_.empty ())
135 file_name.dir_ = dir.to_string ();
136 else if (!dir.to_string ().empty ())
137 file_name.dir_ = dir.to_string ()
138 + ::to_string (DIRSEP) + file_name.dir_;
139 if (is_file (file_name.to_string ()))
140 return file_name.to_string ();
150 where EXT is from EXTENSIONS.
153 File_path::find (const string &name, char const *extensions[])
155 if (name.empty () || name == "-")
158 File_name file_name (name);
159 string orig_ext = file_name.ext_;
160 for (int i = 0; extensions[i]; i++)
162 file_name.ext_ = orig_ext;
163 if (*extensions[i] && !file_name.ext_.empty ())
164 file_name.ext_ += ".";
165 file_name.ext_ += extensions[i];
166 string found = find (file_name.to_string ());
174 /** Append a directory, return false if failed. */
176 File_path::try_append (string s)
189 File_path::to_string () const
192 for (vsize i = 0; i < dirs_.size (); i++)
195 if (i < dirs_.size () - 1)
196 s += ::to_string (PATHSEP);
202 File_path::append (const string &str)
204 dirs_.push_back (str);
208 File_path::prepend (const string &str)
210 dirs_.insert (dirs_.begin (), str);