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