2 file-path.cc - implement File_path
4 source file of the Flower Library
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "file-path.hh"
21 #include <sys/cygwin.h>
24 #include "file-name.hh"
31 File_path::parse_path (String p)
34 while ((len = p.length ()) )
36 int i = p.index (PATHSEP);
39 append (p.left_string (i));
40 p = p.right_string (len - i - 1);
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.
51 The file name if found, or empty string if not found. */
54 File_path::find (String name) const
56 if (!name.length () || (name == "-") )
59 /* Handle absolute file name. */
60 if (name[0] == DIRSEP)
62 if (FILE *f = fopen (name.to_str0 (), "r"))
69 for (int i = 0; i < size (); i++)
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);
79 #if 0 /* Check if directory. TODO: encapsulate for autoconf */
81 if (stat (file_name.to_str0 (), &sbuf) != 0)
84 if (! (sbuf.st_mode & __S_IFREG))
87 #if !STAT_MACROS_BROKEN
90 if (stat (file_name.to_str0 (), &sbuf) != 0)
93 if (S_ISDIR (sbuf.st_mode))
98 FILE *f = fopen (file_name.to_str0 (), "r");
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.
113 Search for NAME, or name without extension, or name with any of
114 EXTENSIONS, in that order.
117 The file name if found, or empty string if not found. */
119 File_path::find (String name, char const *extensions[])
121 File_name file_name (name);
122 if (name.is_empty () || name == "-")
123 file_name.base_ = "-";
126 String orig_ext = file_name.ext_;
127 for (int i = 0; extensions[i]; i++)
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 ())
136 /* Reshuffle extension */
137 file_name = File_name (file_name.to_string ());
139 return file_name.to_string ();
142 /** Append a directory, return false if failed. */
144 File_path::try_append (String s)
148 if (FILE *f = fopen (s.to_str0 (), "r"))
158 File_path::to_string () const
162 for (int i = 0; i < n; i++)