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"
24 #include <sys/cygwin.h>
27 #include "file-name.hh"
35 File_path::directories () const
41 TODO: use split_string.
47 File_path::parse_path (std::string p)
50 while ((len = p.length ()))
52 ssize i = p.find (PATHSEP);
55 append (p.substr (0, i));
56 p = p.substr (std::min (len, i + 1));
61 is_file (std::string file_name)
63 #if 0 /* Check if directory. TODO: encapsulate for autoconf */
65 if (stat (file_name.c_str (), &sbuf) != 0)
68 if (! (sbuf.st_mode & __S_IFREG))
72 #if !STAT_MACROS_BROKEN
74 if (stat (file_name.c_str (), &sbuf) != 0)
77 return !S_ISDIR (sbuf.st_mode);
80 if (FILE *f = fopen (file_name.c_str (), "r"))
90 is_dir (std::string file_name)
92 #if !STAT_MACROS_BROKEN
94 if (stat (file_name.c_str (), &sbuf) != 0)
97 return S_ISDIR (sbuf.st_mode);
100 if (FILE *f = fopen (file_name.c_str (), "r"))
110 Check absolute file name, search in the current dir (DUH! FIXME!),
111 in the construction-arg (what's that?), and in any other appended
112 directory, in this order.
115 The file name if found, or empty string if not found. */
118 File_path::find (std::string name) const
120 if (!name.length () || (name == "-"))
124 if (name.find ('\\') != NPOS)
125 programming_error ("file name not normalized: " + name);
126 #endif /* __MINGW32__ */
128 /* Handle absolute file name. */
129 File_name file_name (name);
130 if (file_name.dir_[0] == DIRSEP && is_file (file_name.to_string ()))
131 return file_name.to_string ();
133 for (int i = 0; i < dirs_.size (); i++)
135 File_name file_name (name);
136 File_name dir = (std::string) dirs_[i];
137 file_name.root_ = dir.root_;
139 if (file_name.dir_.empty ())
140 file_name.dir_ = dir.to_string ();
141 else if (!dir.to_string ().empty ())
142 file_name.dir_ = dir.to_string ()
143 + std::to_string (DIRSEP) + file_name.dir_;
144 if (is_file (file_name.to_string ()))
145 return file_name.to_string ();
155 where EXT is from EXTENSIONS.
158 File_path::find (std::string name, char const *extensions[])
160 if (name.empty () || name == "-")
163 File_name file_name (name);
164 std::string orig_ext = file_name.ext_;
165 for (int i = 0; extensions[i]; i++)
167 file_name.ext_ = orig_ext;
168 if (*extensions[i] && !file_name.ext_.empty ())
169 file_name.ext_ += ".";
170 file_name.ext_ += extensions[i];
171 std::string found = find (file_name.to_string ());
179 /** Append a directory, return false if failed. */
181 File_path::try_append (std::string s)
194 File_path::to_string () const
197 for (int i = 0; i < dirs_.size (); i++)
200 if (i < dirs_.size () - 1)
201 s += std::to_string (PATHSEP);
207 File_path::append (std::string str)
213 File_path::prepend (std::string str)
215 dirs_.insert (str, 0);