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
42 File_path::parse_path (std::string p)
45 while ((len = p.length ()))
47 ssize i = p.find (PATHSEP);
50 append (p.substr (0, i));
51 p = p.substr (std::min (len, i + 1));
56 is_file (std::string file_name)
58 #if 0 /* Check if directory. TODO: encapsulate for autoconf */
60 if (stat (file_name.c_str (), &sbuf) != 0)
63 if (! (sbuf.st_mode & __S_IFREG))
67 #if !STAT_MACROS_BROKEN
69 if (stat (file_name.c_str (), &sbuf) != 0)
72 return !S_ISDIR (sbuf.st_mode);
75 if (FILE *f = fopen (file_name.c_str (), "r"))
85 is_dir (std::string file_name)
87 #if !STAT_MACROS_BROKEN
89 if (stat (file_name.c_str (), &sbuf) != 0)
92 return S_ISDIR (sbuf.st_mode);
95 if (FILE *f = fopen (file_name.c_str (), "r"))
105 Check absolute file name, search in the current dir (DUH! FIXME!),
106 in the construction-arg (what's that?), and in any other appended
107 directory, in this order.
110 The file name if found, or empty string if not found. */
113 File_path::find (std::string name) const
115 if (!name.length () || (name == "-"))
119 if (name.find ('\\') != NPOS)
120 programming_error ("file name not normalized: " + name);
121 #endif /* __MINGW32__ */
123 /* Handle absolute file name. */
124 File_name file_name (name);
125 if (file_name.dir_[0] == DIRSEP && is_file (file_name.to_string ()))
126 return file_name.to_string ();
128 for (int i = 0; i < dirs_.size (); i++)
130 File_name file_name (name);
131 File_name dir = (std::string) dirs_[i];
132 file_name.root_ = dir.root_;
134 if (file_name.dir_.empty ())
135 file_name.dir_ = dir.to_string ();
136 else if (!dir.to_string ().empty ())
137 file_name.dir_ = dir.to_string ()
138 + std::to_string (DIRSEP) + file_name.dir_;
139 if (is_file (file_name.to_string ()))
140 return file_name.to_string ();
150 where EXT is from EXTENSIONS.
153 File_path::find (std::string name, char const *extensions[])
155 if (name.empty () || name == "-")
158 File_name file_name (name);
159 std::string orig_ext = file_name.ext_;
160 for (int i = 0; extensions[i]; i++)
162 file_name.ext_ = orig_ext;
163 if (*extensions[i] && !file_name.ext_.empty ())
164 file_name.ext_ += ".";
165 file_name.ext_ += extensions[i];
166 std::string found = find (file_name.to_string ());
174 /** Append a directory, return false if failed. */
176 File_path::try_append (std::string s)
189 File_path::to_string () const
192 for (int i = 0; i < dirs_.size (); i++)
195 if (i < dirs_.size () - 1)
196 s += std::to_string (PATHSEP);
202 File_path::append (std::string str)
208 File_path::prepend (std::string str)
210 dirs_.insert (str, 0);