]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-path.cc
* flower/file-path.cc (find): try to open directly as well, so we
[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--2004 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
26 #ifndef PATHSEP
27 #define PATHSEP ':'
28 #endif
29
30 void
31 File_path::parse_path (String p)
32 {
33   int len;
34   while ((len = p.length ()) )
35     {
36       int i = p.index (PATHSEP);
37       if (i <0) 
38         i = len;
39       append (p.left_string (i));
40       p = p.right_string (len - i - 1);
41     }
42 }
43
44 /** Find a file.
45     
46   Seach in the current dir (DUH! FIXME?), in the construction-arg
47   (what's that?, and in any other appended directory, in this order.
48
49   @return
50   The file name if found, or empty string if not found. */
51
52 String
53 File_path::find (String name) const
54 {
55   if (!name.length () || (name == "-") )
56     return name;
57
58   /*
59     TODO:  should check for absolute path
60    */
61   if (FILE *f =fopen (name.to_str0 (), "r"))
62     {
63       fclose (f);
64       return name;
65     }
66        
67   for (int i = 0; i < size (); i++)
68     {
69       String file_name = elem (i);
70       String sep = ::to_string (DIRSEP);
71       String right (file_name.right_string (1));
72       if (file_name.length () && right != sep)
73         file_name += ::to_string (DIRSEP);
74
75       file_name += name;
76
77 #if 0 /* Check if directory. TODO: encapsulate for autoconf */
78       struct stat sbuf;
79       if (stat (file_name.to_str0 (), &sbuf) != 0)
80         continue;
81       
82       if (! (sbuf.st_mode & __S_IFREG))
83         continue;
84 #endif
85 #if !STAT_MACROS_BROKEN
86       
87       struct stat sbuf;
88       if (stat (file_name.to_str0 (), &sbuf) != 0)
89         continue;
90
91       if (S_ISDIR (sbuf.st_mode))
92         continue;
93 #endif
94
95       /* ugh */
96       FILE *f = fopen (file_name.to_str0 (), "r");
97       if (f)
98         {
99           fclose (f);
100           return file_name;
101         }
102     }
103   return "";
104 }
105
106 /** Find a file.
107     
108   Seach in the current dir (DUH! FIXME?), in the construction-arg
109   (what's that?, and in any other appended directory, in this order.
110
111   Search for NAME, or name without extension, or name with any of
112   EXTENSIONS, in that order.
113
114   @return
115   The file name if found, or empty string if not found. */
116 String
117 File_path::find (String name, char const *extensions[])
118 {
119   File_name file_name (name);
120   if (name.is_empty () || name == "-")
121     file_name.base_ = "-";
122   else
123     {
124       String orig_ext = file_name.ext_;
125       for (int i = 0; extensions[i]; i++)
126         {
127           file_name.ext_ = orig_ext;
128           if (*extensions[i] && !file_name.ext_.is_empty ())
129             file_name.ext_ += ".";
130           file_name.ext_ += extensions[i];
131           if (!find (file_name.to_string ()).is_empty ())
132             break;
133         }
134       /* Reshuffle extension */
135       file_name = File_name (file_name.to_string ());
136     }
137   return file_name.to_string ();
138 }
139
140 /** Append a directory, return false if failed.  */
141 bool
142 File_path::try_append (String s)
143 {
144   if (s == "")
145     s =  ".";
146   if (FILE *f = fopen (s.to_str0 (), "r"))
147     {
148       fclose (f);
149       append (s);
150       return true;
151     }
152   return false;
153 }
154
155 String
156 File_path::to_string () const
157 {
158   String s;
159   int n = size ();
160   for (int i = 0; i < n; i++)
161     {
162       s = s + elem (i);
163       if (i < n - 1)
164         s += ":";
165     }
166   return s;
167 }