]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-path.cc
(make-ps-images): Bugfix: `Pages: 1\n' is not
[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   if (FILE *f = fopen (file_name.to_str0 (), "r"))
72     {
73       fclose (f);
74       return true;
75     }
76   
77   return false;
78 }
79
80 bool
81 is_dir (String file_name)
82 {
83 #if !STAT_MACROS_BROKEN
84   struct stat sbuf;
85   if (stat (file_name.to_str0 (), &sbuf) != 0)
86     return false;
87   
88   return S_ISDIR (sbuf.st_mode);
89 #endif
90
91   if (FILE *f = fopen (file_name.to_str0 (), "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 String
109 File_path::find (String name) const
110 {
111   if (!name.length () || (name == "-"))
112     return name;
113
114 #ifdef __MINGW32__
115   if (name.index ('\\') >= 0)
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 (int i = 0; i < dirs_.size (); i++)
125     {
126       File_name file_name (name);
127       File_name dir = dirs_[i];
128       file_name.root_ = dir.root_;
129       dir.root_ = "";
130       if (file_name.dir_.is_empty ())
131         file_name.dir_ = dir.to_string ();
132       else if (!dir.to_string ().is_empty())
133         file_name.dir_ += ::to_string (DIRSEP) + dir.to_string ();
134         
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 String
149 File_path::find (String name, char const *extensions[])
150 {
151   File_name file_name (name);
152   if (name.is_empty () || name == "-")
153     file_name.base_ = "-";
154   else
155     {
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_.is_empty ())
161             file_name.ext_ += ".";
162           file_name.ext_ += extensions[i];
163           if (!find (file_name.to_string ()).is_empty ())
164             break;
165         }
166       
167       /* Reshuffle extension */
168       file_name = File_name (file_name.to_string ());
169     }
170   return file_name.to_string ();
171 }
172
173 /** Append a directory, return false if failed.  */
174 bool
175 File_path::try_append (String s)
176 {
177   if (s == "")
178     s = ".";
179   if (is_dir (s))
180     {
181       append (s);
182       return true;
183     }
184   return false;
185 }
186
187 String
188 File_path::to_string () const
189 {
190   String s;
191   for (int i = 0; i < dirs_.size (); i++)
192     {
193       s = s + dirs_[i];
194       if (i < dirs_.size() - 1)
195         s += ::to_string (PATHSEP);
196     }
197   return s;
198 }
199
200 void
201 File_path::append (String str)
202 {
203   dirs_.push (str);
204 }
205
206 void
207 File_path::prepend (String str)
208 {
209   dirs_.insert (str, 0);
210 }