]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-path.cc
``slikken kreng''
[lilypond.git] / flower / file-path.cc
1 /*
2    path.cc - manipulation of paths and filenames.
3 */
4
5 #include "config.h"
6 #include <stdio.h>
7 #include <errno.h>
8 #include <limits.h>
9
10 #if HAVE_SYS_STAT_H 
11 #include <sys/stat.h>
12 #endif
13
14 #ifdef __CYGWIN__
15 #include <sys/cygwin.h>
16
17 // URGURG
18 #include "../lily/include/scm-option.hh"
19 #endif
20
21 #include "file-path.hh"
22
23
24 #ifndef PATHSEP
25 #define PATHSEP ':'
26 #endif
27
28 /* We don't have multiple roots, set this to '\0'? */
29 #ifndef ROOTSEP
30 #define ROOTSEP ':'
31 #endif
32
33 #ifndef DIRSEP
34 #define DIRSEP '/'
35 #endif
36
37 #ifndef EXTSEP
38 #define EXTSEP '.'
39 #endif
40
41
42
43 #ifdef __CYGWIN__
44 static String
45 dos_to_posix (String path)
46 {
47   char buf[PATH_MAX];
48   char *filename = path.get_copy_str0 ();
49   /* urg, wtf? char const* argument gets modified! */
50   cygwin_conv_to_posix_path (filename, buf);
51   delete filename;
52   return buf;
53 }
54
55 static String
56 dos_to_posix_list (String path)
57 {
58   char *filename = path.get_copy_str0 ();
59   int len = cygwin_win32_to_posix_path_list_buf_size (filename);
60   if (len < PATH_MAX)
61     len = PATH_MAX;
62   char *buf = new char[len];
63   /* urg, wtf? char const* argument gets modified! */
64   cygwin_win32_to_posix_path_list (filename, buf);
65   delete filename;
66   
67   String ret = buf;
68   delete buf;
69   return ret;
70 }
71 #endif /* __CYGWIN__ */
72
73 /* Join components to full path. */
74 String
75 Path::string () const
76 {
77   String s;
78   if (!root.empty_b ())
79     s = root + to_string (ROOTSEP);
80   if (!dir.empty_b ())
81     s += dir + to_string (DIRSEP);
82   s += base;
83   if (!ext.empty_b ())
84     s += to_string (EXTSEP) + ext;
85   return s;
86 }
87
88 /**
89    @param path the original full filename
90    @return 4 components of the path. They can be empty
91 */
92 Path
93 split_path (String path)
94 {
95 #ifdef __CYGWIN__
96   /* All system functions would work, even if we don't convert to
97      posix path, but we'd think that \foe\bar\baz.ly is in the cwd.
98      On by default.  */
99   if (!(testing_level_global & 1))
100     path = dos_to_posix (path);
101 #endif
102
103   Path p;
104   int i = path.index (ROOTSEP);
105   if (i >= 0)
106     {
107       p.root = path.left_string (i);
108       path = path.right_string (path.length () - i - 1);
109     }
110
111   i = path.index_last (DIRSEP);
112   if (i >= 0)
113     {
114       p.dir = path.left_string (i);
115       path = path.right_string (path.length () - i - 1);
116     }
117
118   i = path.index_last ('.');
119   if (i >= 0)
120     {
121       p.base = path.left_string (i);
122       p.ext = path.right_string (path.length () - i - 1);
123     }
124   else
125     p.base = path;
126   return p;
127 }
128
129 void
130 File_path::parse_path (String p)
131 {
132 #ifdef __CYGWIN__
133   if (testing_level_global & 4)
134     p = dos_to_posix_list (p);
135 #endif
136
137   int l;
138   
139   while ((l = p.length ()) )
140     {
141       int i = p.index (PATHSEP);
142       if (i <0) 
143         i = l;
144       add (p.left_string (i));
145       p = p.right_string (l- i - 1);
146     }
147 }
148
149
150
151
152 /** Find a file.
153   It will search in the current dir, in the construction-arg, and
154   in any other added path, in this order.
155
156   @return
157   The full path if found, or empty string if not found
158   */
159 String
160 File_path::find (String nm) const
161 {
162   if (!nm.length () || (nm == "-") )
163     return nm;
164   for (int i=0; i < size (); i++)
165     {
166       String path  = elem (i);
167       String sep = to_string (DIRSEP);
168       String right (path.right_string (1));
169       if (path.length () && right != sep)
170         path += to_string (DIRSEP);
171
172       path += nm;
173
174
175 #if 0
176       /*
177         Check if directory. TODO: encapsulate for autoconf
178        */
179       struct stat sbuf;
180       if (stat (path.to_str0 (), &sbuf) == ENOENT)
181         continue;
182       
183       if (! (sbuf.st_mode & __S_IFREG))
184         continue;
185 #endif
186 #if !STAT_MACROS_BROKEN
187       struct stat sbuf;
188       if (stat (path.to_str0 (), &sbuf) == ENOENT)
189         continue;
190       
191       if (S_ISDIR (sbuf.st_mode))
192         continue;
193 #endif
194
195       FILE *f = fopen (path.to_str0 (), "r"); // ugh!
196       if (f)
197         {
198           fclose (f);
199           return path;
200         }
201     }
202   return "";
203 }
204
205 /**
206    Add a directory, return false if failed
207  */
208 bool
209 File_path::try_add (String s)
210 {
211   if (s == "")
212     s =  ".";
213   FILE  * f = fopen (s.to_str0 (), "r");
214   if (!f)
215     return false;
216   fclose (f);
217     
218   add (s);
219   return true;
220 }
221
222 void
223 File_path::add (String s)
224 {
225 #ifdef __CYGWIN__
226   if (testing_level_global & 2)
227     s = dos_to_posix (s);
228 #endif
229
230   push (s);
231 }
232
233 String
234 File_path::string () const
235 {
236   String s;
237   for (int i=0; i< size (); i++)
238     {
239       s = s + elem (i);
240       if (i < size () -1 )
241         s += ":";
242     }
243   return s;
244 }