2 file-path.cc - implement File_path
4 source file of the Flower Library
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "file-path.hh"
21 #include <sys/cygwin.h>
24 #include "file-name.hh"
32 File_path::directories () const
39 File_path::parse_path (string p)
41 concat (dirs_, string_split (p, PATHSEP));
45 is_file (string file_name)
47 #if !STAT_MACROS_BROKEN
49 if (stat (file_name.c_str (), &sbuf) != 0)
52 return !S_ISDIR (sbuf.st_mode);
55 if (FILE *f = fopen (file_name.c_str (), "r"))
65 is_dir (string file_name)
68 canonicalize; in particular, trailing slashes should disappear.
70 file_name = File_name (file_name).to_string ();
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 Check absolute file name, search in the current dir (DUH! FIXME!),
91 in the construction-arg (what's that?), and in any other appended
92 directory, in this order.
95 The file name if found, or empty string if not found. */
98 File_path::find (string name) const
100 if (!name.length () || (name == "-"))
104 if (name.find ('\\') != NPOS)
105 programming_error ("file name not normalized: " + name);
106 #endif /* __MINGW32__ */
108 /* Handle absolute file name. */
109 File_name file_name (name);
110 if (file_name.dir_[0] == DIRSEP && is_file (file_name.to_string ()))
111 return file_name.to_string ();
113 for (vsize i = 0; i < dirs_.size (); i++)
115 File_name file_name (name);
116 File_name dir = (string) dirs_[i];
117 file_name.root_ = dir.root_;
119 if (file_name.dir_.empty ())
120 file_name.dir_ = dir.to_string ();
121 else if (!dir.to_string ().empty ())
122 file_name.dir_ = dir.to_string ()
123 + ::to_string (DIRSEP) + file_name.dir_;
124 if (is_file (file_name.to_string ()))
125 return file_name.to_string ();
135 where EXT is from EXTENSIONS.
138 File_path::find (string name, char const *extensions[])
140 if (name.empty () || name == "-")
143 File_name file_name (name);
144 string orig_ext = file_name.ext_;
145 for (int i = 0; extensions[i]; i++)
147 file_name.ext_ = orig_ext;
148 if (*extensions[i] && !file_name.ext_.empty ())
149 file_name.ext_ += ".";
150 file_name.ext_ += extensions[i];
151 string found = find (file_name.to_string ());
159 /** Append a directory, return false if failed. */
161 File_path::try_append (string s)
174 File_path::to_string () const
177 for (vsize i = 0; i < dirs_.size (); i++)
180 if (i < dirs_.size () - 1)
181 s += ::to_string (PATHSEP);
187 File_path::append (string str)
189 dirs_.push_back (str);
193 File_path::prepend (string str)
195 dirs_.insert (dirs_.begin (), str);