2 path.cc - manipulation of paths and filenames.
15 #include <sys/cygwin.h>
18 #include "../lily/include/scm-option.hh"
21 #include "file-path.hh"
28 /* We don't have multiple roots, set this to '\0'? */
45 dos_to_posix (String path)
48 char *filename = path.copy_ch_p ();
49 /* urg, wtf? char const* argument gets modified! */
50 cygwin_conv_to_posix_path (filename, buf);
56 dos_to_posix_list (String path)
58 char *filename = path.copy_ch_p ();
59 int len = cygwin_win32_to_posix_path_list_buf_size (filename);
62 char *buf = new char[len];
63 /* urg, wtf? char const* argument gets modified! */
64 cygwin_win32_to_posix_path_list (filename, buf);
71 #endif /* __CYGWIN__ */
73 /* Join components to full path. */
79 s = root + to_str (ROOTSEP);
81 s += dir + to_str (DIRSEP);
84 s += to_str (EXTSEP) + ext;
89 @param path the original full filename
90 @return 4 components of the path. They can be empty
93 split_path (String path)
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 if (testing_level_global & 1)
99 path = dos_to_posix (path);
103 int i = path.index_i (ROOTSEP);
106 p.root = path.left_str (i);
107 path = path.right_str (path.length_i () - i - 1);
110 i = path.index_last_i (DIRSEP);
113 p.dir = path.left_str (i);
114 path = path.right_str (path.length_i () - i - 1);
117 i = path.index_last_i ('.');
120 p.base = path.left_str (i);
121 p.ext = path.right_str (path.length_i () - i - 1);
129 File_path::parse_path (String p)
132 if (testing_level_global & 4)
133 p = dos_to_posix_list (p);
138 while ((l = p.length_i ()) )
140 int i = p.index_i (PATHSEP);
143 add (p.left_str (i));
144 p = p.right_str (l- i - 1);
152 It will search in the current dir, in the construction-arg, and
153 in any other added path, in this order.
156 The full path if found, or empty string if not found
159 File_path::find (String nm) const
161 if (!nm.length_i () || (nm == "-") )
163 for (int i=0; i < size (); i++)
165 String path = elem (i);
166 String sep = to_str (DIRSEP);
167 String right (path.right_str (1));
168 if (path.length_i () && right != sep)
169 path += to_str (DIRSEP);
176 Check if directory. TODO: encapsulate for autoconf
179 if (stat (path.ch_C (), &sbuf) == ENOENT)
182 if (! (sbuf.st_mode & __S_IFREG))
185 #if !STAT_MACROS_BROKEN
187 if (stat (path.ch_C (), &sbuf) == ENOENT)
190 if (S_ISDIR (sbuf.st_mode))
194 FILE *f = fopen (path.ch_C (), "r"); // ugh!
205 Add a directory, return false if failed
208 File_path::try_add (String s)
212 FILE * f = fopen (s.ch_C (), "r");
222 File_path::add (String s)
225 if (testing_level_global & 2)
226 s = dos_to_posix (s);
233 File_path::str () const
236 for (int i=0; i< size (); i++)