2 file-path.cc - implement File_path
4 source file of the Flower Library
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "file-path.hh"
22 #include <sys/cygwin.h>
25 #include "file-name.hh"
33 File_path::directories () const
39 File_path::parse_path (Std_string p)
42 while ((len = p.length ()))
44 int i = p.find (PATHSEP);
47 append (String (p, 0, i));
48 p = String (p, i + 1);
53 is_file (Std_string file_name)
55 #if 0 /* Check if directory. TODO: encapsulate for autoconf */
57 if (stat (file_name.c_str (), &sbuf) != 0)
60 if (! (sbuf.st_mode & __S_IFREG))
64 #if !STAT_MACROS_BROKEN
66 if (stat (file_name.c_str (), &sbuf) != 0)
69 return !S_ISDIR (sbuf.st_mode);
72 if (FILE *f = fopen (file_name.c_str (), "r"))
82 is_dir (Std_string file_name)
84 #if !STAT_MACROS_BROKEN
86 if (stat (file_name.c_str (), &sbuf) != 0)
89 return S_ISDIR (sbuf.st_mode);
92 if (FILE *f = fopen (file_name.c_str (), "r"))
102 Check absolute file name, search in the current dir (DUH! FIXME!),
103 in the construction-arg (what's that?), and in any other appended
104 directory, in this order.
107 The file name if found, or empty string if not found. */
110 File_path::find (Std_string name) const
112 if (!name.length () || (name == "-"))
116 if (name.find ('\\') >= 0)
117 programming_error ("file name not normalized: " + name);
118 #endif /* __MINGW32__ */
120 /* Handle absolute file name. */
121 File_name file_name (name);
122 if (file_name.dir_[0] == DIRSEP && is_file (file_name.to_string ()))
123 return file_name.to_string ();
125 for (int i = 0; i < dirs_.size (); i++)
127 File_name file_name (name);
128 File_name dir = (Std_string) dirs_[i];
129 file_name.root_ = dir.root_;
131 if (file_name.dir_.empty ())
132 file_name.dir_ = dir.to_string ();
133 else if (!dir.to_string ().empty ())
134 file_name.dir_ = dir.to_string ()
135 + ::to_string (DIRSEP) + file_name.dir_;
136 if (is_file (file_name.to_string ()))
137 return file_name.to_string ();
147 where EXT is from EXTENSIONS.
150 File_path::find (Std_string name, char const *extensions[])
152 if (name.empty () || name == "-")
155 File_name file_name (name);
156 Std_string orig_ext = file_name.ext_;
157 for (int i = 0; extensions[i]; i++)
159 file_name.ext_ = orig_ext;
160 if (*extensions[i] && !file_name.ext_.empty ())
161 file_name.ext_ += ".";
162 file_name.ext_ += extensions[i];
163 Std_string found = find (file_name.to_string ());
171 /** Append a directory, return false if failed. */
173 File_path::try_append (Std_string s)
186 File_path::to_string () const
189 for (int i = 0; i < dirs_.size (); i++)
192 if (i < dirs_.size () - 1)
193 s += ::to_string (PATHSEP);
199 File_path::append (Std_string str)
205 File_path::prepend (Std_string str)
207 dirs_.insert (str, 0);