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