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"
12 #include "std-string.hh"
23 #include <sys/cygwin.h>
26 #include "file-name.hh"
34 File_path::directories () const
40 TODO: use split_string.
46 File_path::parse_path (string p)
49 while ((len = p.length ()))
51 ssize i = p.find (PATHSEP);
54 append (p.substr (0, i));
55 p = p.substr (min (len, i + 1));
60 is_file (string file_name)
62 #if !STAT_MACROS_BROKEN
64 if (stat (file_name.c_str (), &sbuf) != 0)
67 return !S_ISDIR (sbuf.st_mode);
70 if (FILE *f = fopen (file_name.c_str (), "r"))
80 is_dir (string file_name)
82 #if !STAT_MACROS_BROKEN
84 if (stat (file_name.c_str (), &sbuf) != 0)
87 return S_ISDIR (sbuf.st_mode);
90 if (FILE *f = fopen (file_name.c_str (), "r"))
100 Check absolute file name, search in the current dir (DUH! FIXME!),
101 in the construction-arg (what's that?), and in any other appended
102 directory, in this order.
105 The file name if found, or empty string if not found. */
108 File_path::find (string name) const
110 if (!name.length () || (name == "-"))
114 if (name.find ('\\') != NPOS)
115 programming_error ("file name not normalized: " + name);
116 #endif /* __MINGW32__ */
118 /* Handle absolute file name. */
119 File_name file_name (name);
120 if (file_name.dir_[0] == DIRSEP && is_file (file_name.to_string ()))
121 return file_name.to_string ();
123 for (vsize i = 0; i < dirs_.size (); i++)
125 File_name file_name (name);
126 File_name dir = (string) dirs_[i];
127 file_name.root_ = dir.root_;
129 if (file_name.dir_.empty ())
130 file_name.dir_ = dir.to_string ();
131 else if (!dir.to_string ().empty ())
132 file_name.dir_ = dir.to_string ()
133 + ::to_string (DIRSEP) + file_name.dir_;
134 if (is_file (file_name.to_string ()))
135 return file_name.to_string ();
145 where EXT is from EXTENSIONS.
148 File_path::find (string name, char const *extensions[])
150 if (name.empty () || name == "-")
153 File_name file_name (name);
154 string orig_ext = file_name.ext_;
155 for (int i = 0; extensions[i]; i++)
157 file_name.ext_ = orig_ext;
158 if (*extensions[i] && !file_name.ext_.empty ())
159 file_name.ext_ += ".";
160 file_name.ext_ += extensions[i];
161 string found = find (file_name.to_string ());
169 /** Append a directory, return false if failed. */
171 File_path::try_append (string s)
184 File_path::to_string () const
187 for (vsize i = 0; i < dirs_.size (); i++)
190 if (i < dirs_.size () - 1)
191 s += ::to_string (PATHSEP);
197 File_path::append (string str)
199 dirs_.push_back (str);
203 File_path::prepend (string str)
205 dirs_.insert (dirs_.begin (), str);