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
.get_copy_str0 ();
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
.get_copy_str0 ();
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. */
75 Path::to_string () const
79 s
= root
+ ::to_string (ROOTSEP
);
81 s
+= dir
+ ::to_string (DIRSEP
);
84 s
+= ::to_string (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.
99 if (!(testing_level_global
& 1))
100 path
= dos_to_posix (path
);
104 int i
= path
.index (ROOTSEP
);
107 p
.root
= path
.left_string (i
);
108 path
= path
.right_string (path
.length () - i
- 1);
111 i
= path
.index_last (DIRSEP
);
114 p
.dir
= path
.left_string (i
);
115 path
= path
.right_string (path
.length () - i
- 1);
118 i
= path
.index_last ('.');
121 p
.base
= path
.left_string (i
);
122 p
.ext
= path
.right_string (path
.length () - i
- 1);
130 File_path::parse_path (String p
)
133 if (testing_level_global
& 4)
134 p
= dos_to_posix_list (p
);
139 while ((l
= p
.length ()) )
141 int i
= p
.index (PATHSEP
);
144 add (p
.left_string (i
));
145 p
= p
.right_string (l
- i
- 1);
153 It will search in the current dir, in the construction-arg, and
154 in any other added path, in this order.
157 The full path if found, or empty string if not found
160 File_path::find (String nm
) const
162 if (!nm
.length () || (nm
== "-") )
164 for (int i
=0; i
< size (); i
++)
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
);
177 Check if directory. TODO: encapsulate for autoconf
180 if (stat (path
.to_str0 (), &sbuf
) != 0)
183 if (! (sbuf
.st_mode
& __S_IFREG
))
186 #if !STAT_MACROS_BROKEN
189 if (stat (path
.to_str0 (), &sbuf
) != 0)
192 if (S_ISDIR (sbuf
.st_mode
))
196 FILE *f
= fopen (path
.to_str0 (), "r"); // ugh!
207 Add a directory, return false if failed
210 File_path::try_add (String s
)
214 FILE * f
= fopen (s
.to_str0 (), "r");
224 File_path::add (String s
)
227 if (testing_level_global
& 2)
228 s
= dos_to_posix (s
);
235 File_path::to_string () const
238 for (int i
=0; i
< size (); i
++)