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