]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-path.cc
*** empty log message ***
[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 Array<std::string>
35 File_path::directories () const
36 {
37   return dirs_;
38 }
39
40 #include <algorithm>
41 void
42 File_path::parse_path (std::string p)
43 {
44   ssize len;
45   while ((len = p.length ()))
46     {
47       ssize i = p.find (PATHSEP);
48       if (i == NPOS)
49         i = len;
50       append (p.substr (0, i));
51       //p = p.substr (min (len, i + 1));
52       //ssize x = min (len, i + 1);
53       ssize a = len;
54       ssize b = i + 1;
55       ssize x = std::max (a, b);
56       p = p.substr (x);
57     }
58 }
59
60 bool
61 is_file (std::string file_name)
62 {
63 #if 0 /* Check if directory. TODO: encapsulate for autoconf */
64   struct stat sbuf;
65   if (stat (file_name.c_str (), &sbuf) != 0)
66     return false;
67
68   if (! (sbuf.st_mode & __S_IFREG))
69     return false;
70 #endif
71
72 #if !STAT_MACROS_BROKEN
73   struct stat sbuf;
74   if (stat (file_name.c_str (), &sbuf) != 0)
75     return false;
76
77   return !S_ISDIR (sbuf.st_mode);
78 #endif
79
80   if (FILE *f = fopen (file_name.c_str (), "r"))
81     {
82       fclose (f);
83       return true;
84     }
85
86   return false;
87 }
88
89 bool
90 is_dir (std::string file_name)
91 {
92 #if !STAT_MACROS_BROKEN
93   struct stat sbuf;
94   if (stat (file_name.c_str (), &sbuf) != 0)
95     return false;
96
97   return S_ISDIR (sbuf.st_mode);
98 #endif
99
100   if (FILE *f = fopen (file_name.c_str (), "r"))
101     {
102       fclose (f);
103       return true;
104     }
105   return false;
106 }
107
108 /** Find a file.
109
110 Check absolute file name, search in the current dir (DUH! FIXME!),
111 in the construction-arg (what's that?), and in any other appended
112 directory, in this order.
113
114 @return
115 The file name if found, or empty string if not found. */
116
117 std::string
118 File_path::find (std::string name) const
119 {
120   if (!name.length () || (name == "-"))
121     return name;
122
123 #ifdef __MINGW32__
124   if (name.find ('\\') != NPOS)
125     programming_error ("file name not normalized: " + name);
126 #endif /* __MINGW32__ */
127
128   /* Handle absolute file name.  */
129   File_name file_name (name);
130   if (file_name.dir_[0] == DIRSEP && is_file (file_name.to_string ()))
131     return file_name.to_string ();
132
133   for (int i = 0; i < dirs_.size (); i++)
134     {
135       File_name file_name (name);
136       File_name dir = (std::string) dirs_[i];
137       file_name.root_ = dir.root_;
138       dir.root_ = "";
139       if (file_name.dir_.empty ())
140         file_name.dir_ = dir.to_string ();
141       else if (!dir.to_string ().empty ())
142         file_name.dir_ = dir.to_string ()
143           + std::to_string (DIRSEP) + file_name.dir_;
144       if (is_file (file_name.to_string ()))
145         return file_name.to_string ();
146     }
147   return "";
148 }
149
150 /*
151   Try to find
152
153   file.EXT,
154
155   where EXT is from EXTENSIONS.
156 */
157 std::string
158 File_path::find (std::string name, char const *extensions[])
159 {
160   if (name.empty () || name == "-")
161     return name;
162   
163   File_name file_name (name);
164   std::string orig_ext = file_name.ext_;
165   for (int i = 0; extensions[i]; i++)
166     {
167       file_name.ext_ = orig_ext;
168       if (*extensions[i] && !file_name.ext_.empty ())
169         file_name.ext_ += ".";
170       file_name.ext_ += extensions[i];
171       std::string found = find (file_name.to_string ());
172       if (!found.empty ())
173         return found;
174     }
175   
176   return "";
177 }
178
179 /** Append a directory, return false if failed.  */
180 bool
181 File_path::try_append (std::string s)
182 {
183   if (s == "")
184     s = ".";
185   if (is_dir (s))
186     {
187       append (s);
188       return true;
189     }
190   return false;
191 }
192
193 std::string
194 File_path::to_string () const
195 {
196   std::string s;
197   for (int i = 0; i < dirs_.size (); i++)
198     {
199       s = s + dirs_[i];
200       if (i < dirs_.size () - 1)
201         s += std::to_string (PATHSEP);
202     }
203   return s;
204 }
205
206 void
207 File_path::append (std::string str)
208 {
209   dirs_.push (str);
210 }
211
212 void
213 File_path::prepend (std::string str)
214 {
215   dirs_.insert (str, 0);
216 }