]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-path.cc
Web-ja: update introduction
[lilypond.git] / flower / file-path.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5   Jan Nieuwenhuizen <janneke@gnu.org>
6
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.
11
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.
16
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/>.
19 */
20
21 #include "file-path.hh"
22
23 #include <cstdio>
24 #include <cerrno>
25
26 #include "config.hh"
27 #if HAVE_SYS_STAT_H
28 #include <sys/stat.h>
29 #endif
30
31 #ifdef __CYGWIN__
32 #include <sys/cygwin.h>
33 #endif
34
35 #include "file-name.hh"
36 #include "warn.hh"
37
38 #ifndef PATHSEP
39 #define PATHSEP ':'
40 #endif
41
42 #include <algorithm>
43
44 vector<string>
45 File_path::directories () const
46 {
47   return dirs_;
48 }
49
50 void
51 File_path::parse_path (const string &p)
52 {
53   concat (dirs_, string_split (p, PATHSEP));
54 }
55
56 bool
57 is_file (const string &file_name)
58 {
59 #if !STAT_MACROS_BROKEN
60   struct stat sbuf;
61   if (stat (file_name.c_str (), &sbuf) != 0)
62     return false;
63
64   return !S_ISDIR (sbuf.st_mode);
65 #endif
66
67   if (FILE *f = fopen (file_name.c_str (), "r"))
68     {
69       fclose (f);
70       return true;
71     }
72
73   return false;
74 }
75
76 bool
77 is_dir (string file_name)
78 {
79   /*
80     canonicalize; in particular, trailing slashes should disappear.
81    */
82   file_name = File_name (file_name).to_string ();
83
84 #if !STAT_MACROS_BROKEN
85   struct stat sbuf;
86   if (stat (file_name.c_str (), &sbuf) != 0)
87     return false;
88
89   return S_ISDIR (sbuf.st_mode);
90 #endif
91
92   if (FILE *f = fopen (file_name.c_str (), "r"))
93     {
94       fclose (f);
95       return true;
96     }
97   return false;
98 }
99
100 /** Find a file.
101
102 Check absolute file name, search in the current dir (DUH! FIXME!),
103 in the construction-arg (what's that?), and in any other appended
104 directory, in this order.
105
106 @return
107 The file name if found, or empty string if not found. */
108
109 string
110 File_path::find (const string &name) const
111 {
112   if (!name.length () || (name == "-"))
113     return name;
114
115 #ifdef __MINGW32__
116   if (name.find ('\\') != NPOS)
117     programming_error ("file name not normalized: " + name);
118 #endif /* __MINGW32__ */
119
120   /* Handle absolute file name.  */
121   File_name file_name (name);
122   if (file_name.dir_[0] == DIRSEP && is_file (file_name.to_string ()))
123     return file_name.to_string ();
124
125   for (vsize i = 0; i < dirs_.size (); i++)
126     {
127       File_name file_name (name);
128       File_name dir = (string) dirs_[i];
129       file_name.root_ = dir.root_;
130       dir.root_ = "";
131       if (file_name.dir_.empty ())
132         file_name.dir_ = dir.to_string ();
133       else if (!dir.to_string ().empty ())
134         file_name.dir_ = dir.to_string ()
135                          + ::to_string (DIRSEP) + file_name.dir_;
136       if (is_file (file_name.to_string ()))
137         return file_name.to_string ();
138     }
139   return "";
140 }
141
142 /*
143   Try to find
144
145   file.EXT,
146
147   where EXT is from EXTENSIONS.
148 */
149 string
150 File_path::find (const string &name, char const *extensions[])
151 {
152   if (name.empty () || name == "-")
153     return name;
154
155   File_name file_name (name);
156   string orig_ext = file_name.ext_;
157   for (int i = 0; extensions[i]; i++)
158     {
159       file_name.ext_ = orig_ext;
160       if (*extensions[i] && !file_name.ext_.empty ())
161         file_name.ext_ += ".";
162       file_name.ext_ += extensions[i];
163       string found = find (file_name.to_string ());
164       if (!found.empty ())
165         return found;
166     }
167
168   return "";
169 }
170
171 /** Append a directory, return false if failed.  */
172 bool
173 File_path::try_append (string s)
174 {
175   if (s == "")
176     s = ".";
177   if (is_dir (s))
178     {
179       append (s);
180       return true;
181     }
182   return false;
183 }
184
185 string
186 File_path::to_string () const
187 {
188   string s;
189   for (vsize i = 0; i < dirs_.size (); i++)
190     {
191       s = s + dirs_[i];
192       if (i < dirs_.size () - 1)
193         s += ::to_string (PATHSEP);
194     }
195   return s;
196 }
197
198 void
199 File_path::append (const string &str)
200 {
201   dirs_.push_back (str);
202 }
203
204 void
205 File_path::prepend (const string &str)
206 {
207   dirs_.insert (dirs_.begin (), str);
208 }