]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-path.cc
c896a650feb4621edec08c82218d5e34ba0ecd3d
[lilypond.git] / flower / file-path.cc
1 /*
2   file-path.cc - implement File_path
3
4   source file of the Flower Library
5
6   (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include "file-path.hh"
11
12 #include "std-string.hh"
13
14 #include <cstdio>
15 #include <cerrno>
16 using namespace std;
17
18 #include "config.hh"
19 #if HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22
23 #ifdef __CYGWIN__
24 #include <sys/cygwin.h>
25 #endif
26
27 #include "file-name.hh"
28 #include "warn.hh"
29
30 #ifndef PATHSEP
31 #define PATHSEP ':'
32 #endif
33
34 std::vector<std::string>
35 File_path::directories () const
36 {
37   return dirs_;
38 }
39
40 /*
41   TODO: use split_string.
42   
43  */
44
45 #include <algorithm>
46 void
47 File_path::parse_path (std::string p)
48 {
49   ssize len;
50   while ((len = p.length ()))
51     {
52       ssize i = p.find (PATHSEP);
53       if (i == NPOS)
54         i = len;
55       append (p.substr (0, i));
56       p = p.substr (std::min (len, i + 1));
57     }
58 }
59
60 bool
61 is_file (std::string file_name)
62 {
63 #if !STAT_MACROS_BROKEN
64   struct stat sbuf;
65   if (stat (file_name.c_str (), &sbuf) != 0)
66     return false;
67
68   return !S_ISDIR (sbuf.st_mode);
69 #endif
70
71   if (FILE *f = fopen (file_name.c_str (), "r"))
72     {
73       fclose (f);
74       return true;
75     }
76
77   return false;
78 }
79
80 bool
81 is_dir (std::string file_name)
82 {
83 #if !STAT_MACROS_BROKEN
84   struct stat sbuf;
85   if (stat (file_name.c_str (), &sbuf) != 0)
86     return false;
87
88   return S_ISDIR (sbuf.st_mode);
89 #endif
90
91   if (FILE *f = fopen (file_name.c_str (), "r"))
92     {
93       fclose (f);
94       return true;
95     }
96   return false;
97 }
98
99 /** Find a file.
100
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.
104
105 @return
106 The file name if found, or empty string if not found. */
107
108 std::string
109 File_path::find (std::string name) const
110 {
111   if (!name.length () || (name == "-"))
112     return name;
113
114 #ifdef __MINGW32__
115   if (name.find ('\\') != NPOS)
116     programming_error ("file name not normalized: " + name);
117 #endif /* __MINGW32__ */
118
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 ();
123
124   for (vsize i = 0; i < dirs_.size (); i++)
125     {
126       File_name file_name (name);
127       File_name dir = (std::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           + std::to_string (DIRSEP) + file_name.dir_;
135       if (is_file (file_name.to_string ()))
136         return file_name.to_string ();
137     }
138   return "";
139 }
140
141 /*
142   Try to find
143
144   file.EXT,
145
146   where EXT is from EXTENSIONS.
147 */
148 std::string
149 File_path::find (std::string name, char const *extensions[])
150 {
151   if (name.empty () || name == "-")
152     return name;
153   
154   File_name file_name (name);
155   std::string orig_ext = file_name.ext_;
156   for (int i = 0; extensions[i]; i++)
157     {
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       std::string found = find (file_name.to_string ());
163       if (!found.empty ())
164         return found;
165     }
166   
167   return "";
168 }
169
170 /** Append a directory, return false if failed.  */
171 bool
172 File_path::try_append (std::string s)
173 {
174   if (s == "")
175     s = ".";
176   if (is_dir (s))
177     {
178       append (s);
179       return true;
180     }
181   return false;
182 }
183
184 std::string
185 File_path::to_string () const
186 {
187   std::string s;
188   for (vsize i = 0; i < dirs_.size (); i++)
189     {
190       s = s + dirs_[i];
191       if (i < dirs_.size () - 1)
192         s += std::to_string (PATHSEP);
193     }
194   return s;
195 }
196
197 void
198 File_path::append (std::string str)
199 {
200   dirs_.push_back (str);
201 }
202
203 void
204 File_path::prepend (std::string str)
205 {
206   dirs_.insert (dirs_.begin (), str);
207 }