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