Remove redundant acknowledger in Bar_number_engraver.
[lilypond/mpolesky.git] / flower / file-path.cc
blobcba472c7a66e250b8aa8ba1e8a45db5996544e2d
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2010 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"
23 #include <cstdio>
24 #include <cerrno>
26 #include "config.hh"
27 #if HAVE_SYS_STAT_H
28 #include <sys/stat.h>
29 #endif
31 #ifdef __CYGWIN__
32 #include <sys/cygwin.h>
33 #endif
35 #include "file-name.hh"
36 #include "warn.hh"
38 #ifndef PATHSEP
39 #define PATHSEP ':'
40 #endif
42 vector<string>
43 File_path::directories () const
45 return dirs_;
48 #include <algorithm>
49 void
50 File_path::parse_path (string p)
52 concat (dirs_, string_split (p, PATHSEP));
55 bool
56 is_file (string file_name)
58 #if !STAT_MACROS_BROKEN
59 struct stat sbuf;
60 if (stat (file_name.c_str (), &sbuf) != 0)
61 return false;
63 return !S_ISDIR (sbuf.st_mode);
64 #endif
66 if (FILE *f = fopen (file_name.c_str (), "r"))
68 fclose (f);
69 return true;
72 return false;
75 bool
76 is_dir (string file_name)
79 canonicalize; in particular, trailing slashes should disappear.
81 file_name = File_name (file_name).to_string ();
83 #if !STAT_MACROS_BROKEN
84 struct stat sbuf;
85 if (stat (file_name.c_str (), &sbuf) != 0)
86 return false;
88 return S_ISDIR (sbuf.st_mode);
89 #endif
91 if (FILE *f = fopen (file_name.c_str (), "r"))
93 fclose (f);
94 return true;
96 return false;
99 /** Find a file.
101 Check absolute file name, search in the current dir (DUH! FIXME!),
102 in the construction-arg (what's that?), and in any other appended
103 directory, in this order.
105 @return
106 The file name if found, or empty string if not found. */
108 string
109 File_path::find (string name) const
111 if (!name.length () || (name == "-"))
112 return name;
114 #ifdef __MINGW32__
115 if (name.find ('\\') != NPOS)
116 programming_error ("file name not normalized: " + name);
117 #endif /* __MINGW32__ */
119 /* Handle absolute file name. */
120 File_name file_name (name);
121 if (file_name.dir_[0] == DIRSEP && is_file (file_name.to_string ()))
122 return file_name.to_string ();
124 for (vsize i = 0; i < dirs_.size (); i++)
126 File_name file_name (name);
127 File_name dir = (string) dirs_[i];
128 file_name.root_ = dir.root_;
129 dir.root_ = "";
130 if (file_name.dir_.empty ())
131 file_name.dir_ = dir.to_string ();
132 else if (!dir.to_string ().empty ())
133 file_name.dir_ = dir.to_string ()
134 + ::to_string (DIRSEP) + file_name.dir_;
135 if (is_file (file_name.to_string ()))
136 return file_name.to_string ();
138 return "";
142 Try to find
144 file.EXT,
146 where EXT is from EXTENSIONS.
148 string
149 File_path::find (string name, char const *extensions[])
151 if (name.empty () || name == "-")
152 return name;
154 File_name file_name (name);
155 string orig_ext = file_name.ext_;
156 for (int i = 0; extensions[i]; i++)
158 file_name.ext_ = orig_ext;
159 if (*extensions[i] && !file_name.ext_.empty ())
160 file_name.ext_ += ".";
161 file_name.ext_ += extensions[i];
162 string found = find (file_name.to_string ());
163 if (!found.empty ())
164 return found;
167 return "";
170 /** Append a directory, return false if failed. */
171 bool
172 File_path::try_append (string s)
174 if (s == "")
175 s = ".";
176 if (is_dir (s))
178 append (s);
179 return true;
181 return false;
184 string
185 File_path::to_string () const
187 string s;
188 for (vsize i = 0; i < dirs_.size (); i++)
190 s = s + dirs_[i];
191 if (i < dirs_.size () - 1)
192 s += ::to_string (PATHSEP);
194 return s;
197 void
198 File_path::append (string str)
200 dirs_.push_back (str);
203 void
204 File_path::prepend (string str)
206 dirs_.insert (dirs_.begin (), str);