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
41 File_path::parse_path (string p)
43 concat (dirs_, string_split (p, PATHSEP));
47 is_file (string file_name)
49 #if !STAT_MACROS_BROKEN
51 if (stat (file_name.c_str (), &sbuf) != 0)
54 return !S_ISDIR (sbuf.st_mode);
57 if (FILE *f = fopen (file_name.c_str (), "r"))
67 is_dir (string file_name)
69 #if !STAT_MACROS_BROKEN
71 if (stat (file_name.c_str (), &sbuf) != 0)
74 return S_ISDIR (sbuf.st_mode);
77 if (FILE *f = fopen (file_name.c_str (), "r"))
87 Check absolute file name, search in the current dir (DUH! FIXME!),
88 in the construction-arg (what's that?), and in any other appended
89 directory, in this order.
92 The file name if found, or empty string if not found. */
95 File_path::find (string name) const
97 if (!name.length () || (name == "-"))
101 if (name.find ('\\') != NPOS)
102 programming_error ("file name not normalized: " + name);
103 #endif /* __MINGW32__ */
105 /* Handle absolute file name. */
106 File_name file_name (name);
107 if (file_name.dir_[0] == DIRSEP && is_file (file_name.to_string ()))
108 return file_name.to_string ();
110 for (vsize i = 0; i < dirs_.size (); i++)
112 File_name file_name (name);
113 File_name dir = (string) dirs_[i];
114 file_name.root_ = dir.root_;
116 if (file_name.dir_.empty ())
117 file_name.dir_ = dir.to_string ();
118 else if (!dir.to_string ().empty ())
119 file_name.dir_ = dir.to_string ()
120 + ::to_string (DIRSEP) + file_name.dir_;
121 if (is_file (file_name.to_string ()))
122 return file_name.to_string ();
132 where EXT is from EXTENSIONS.
135 File_path::find (string name, char const *extensions[])
137 if (name.empty () || name == "-")
140 File_name file_name (name);
141 string orig_ext = file_name.ext_;
142 for (int i = 0; extensions[i]; i++)
144 file_name.ext_ = orig_ext;
145 if (*extensions[i] && !file_name.ext_.empty ())
146 file_name.ext_ += ".";
147 file_name.ext_ += extensions[i];
148 string found = find (file_name.to_string ());
156 /** Append a directory, return false if failed. */
158 File_path::try_append (string s)
171 File_path::to_string () const
174 for (vsize i = 0; i < dirs_.size (); i++)
177 if (i < dirs_.size () - 1)
178 s += ::to_string (PATHSEP);
184 File_path::append (string str)
186 dirs_.push_back (str);
190 File_path::prepend (string str)
192 dirs_.insert (dirs_.begin (), str);