]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-path.cc
* lily/tie-column.cc (set_manual_tie_configuration): new function.
[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 using namespace std;
15
16 #include "config.hh"
17 #if HAVE_SYS_STAT_H
18 #include <sys/stat.h>
19 #endif
20
21 #ifdef __CYGWIN__
22 #include <sys/cygwin.h>
23 #endif
24
25 #include "file-name.hh"
26 #include "warn.hh"
27
28 #ifndef PATHSEP
29 #define PATHSEP ':'
30 #endif
31
32 Array<String>
33 File_path::directories () const
34 {
35   return dirs_;
36 }
37
38 void
39 File_path::parse_path (String p)
40 {
41   int len;
42   while ((len = p.length ()))
43     {
44       int i = p.index (PATHSEP);
45       if (i < 0)
46         i = len;
47       append (p.left_string (i));
48       p = p.right_string (len - i - 1);
49     }
50 }
51
52 bool
53 is_file (String file_name)
54 {
55 #if 0 /* Check if directory. TODO: encapsulate for autoconf */
56   struct stat sbuf;
57   if (stat (file_name.to_str0 (), &sbuf) != 0)
58     return false;
59
60   if (! (sbuf.st_mode & __S_IFREG))
61     return false;
62 #endif
63
64 #if !STAT_MACROS_BROKEN
65   struct stat sbuf;
66   if (stat (file_name.to_str0 (), &sbuf) != 0)
67     return false;
68
69   return !S_ISDIR (sbuf.st_mode);
70 #endif
71
72   if (FILE *f = fopen (file_name.to_str0 (), "r"))
73     {
74       fclose (f);
75       return true;
76     }
77
78   return false;
79 }
80
81 bool
82 is_dir (String file_name)
83 {
84 #if !STAT_MACROS_BROKEN
85   struct stat sbuf;
86   if (stat (file_name.to_str0 (), &sbuf) != 0)
87     return false;
88
89   return S_ISDIR (sbuf.st_mode);
90 #endif
91
92   if (FILE *f = fopen (file_name.to_str0 (), "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 (String name) const
111 {
112   if (!name.length () || (name == "-"))
113     return name;
114
115 #ifdef __MINGW32__
116   if (name.index ('\\') >= 0)
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 (int i = 0; i < dirs_.size (); i++)
126     {
127       File_name file_name (name);
128       File_name dir = dirs_[i];
129       file_name.root_ = dir.root_;
130       dir.root_ = "";
131       if (file_name.dir_.is_empty ())
132         file_name.dir_ = dir.to_string ();
133       else if (!dir.to_string ().is_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 (String name, char const *extensions[])
151 {
152   File_name file_name (name);
153   if (name.is_empty () || name == "-")
154     file_name.base_ = "-";
155   else
156     {
157       String orig_ext = file_name.ext_;
158       for (int i = 0; extensions[i]; i++)
159         {
160           file_name.ext_ = orig_ext;
161           if (*extensions[i] && !file_name.ext_.is_empty ())
162             file_name.ext_ += ".";
163           file_name.ext_ += extensions[i];
164           if (!find (file_name.to_string ()).is_empty ())
165             break;
166         }
167
168       /* Reshuffle extension */
169       file_name = File_name (file_name.to_string ());
170     }
171   return file_name.to_string ();
172 }
173
174 /** Append a directory, return false if failed.  */
175 bool
176 File_path::try_append (String s)
177 {
178   if (s == "")
179     s = ".";
180   if (is_dir (s))
181     {
182       append (s);
183       return true;
184     }
185   return false;
186 }
187
188 String
189 File_path::to_string () const
190 {
191   String s;
192   for (int i = 0; i < dirs_.size (); i++)
193     {
194       s = s + dirs_[i];
195       if (i < dirs_.size () - 1)
196         s += ::to_string (PATHSEP);
197     }
198   return s;
199 }
200
201 void
202 File_path::append (String str)
203 {
204   dirs_.push (str);
205 }
206
207 void
208 File_path::prepend (String str)
209 {
210   dirs_.insert (str, 0);
211 }