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
)
70 canonicalize; in particular, trailing slashes should disappear.
72 file_name
= File_name (file_name
).to_string ();
74 #if !STAT_MACROS_BROKEN
76 if (stat (file_name
.c_str (), &sbuf
) != 0)
79 return S_ISDIR (sbuf
.st_mode
);
82 if (FILE *f
= fopen (file_name
.c_str (), "r"))
92 Check absolute file name, search in the current dir (DUH! FIXME!),
93 in the construction-arg (what's that?), and in any other appended
94 directory, in this order.
97 The file name if found, or empty string if not found. */
100 File_path::find (string name
) const
102 if (!name
.length () || (name
== "-"))
106 if (name
.find ('\\') != NPOS
)
107 programming_error ("file name not normalized: " + name
);
108 #endif /* __MINGW32__ */
110 /* Handle absolute file name. */
111 File_name
file_name (name
);
112 if (file_name
.dir_
[0] == DIRSEP
&& is_file (file_name
.to_string ()))
113 return file_name
.to_string ();
115 for (vsize i
= 0; i
< dirs_
.size (); i
++)
117 File_name
file_name (name
);
118 File_name dir
= (string
) dirs_
[i
];
119 file_name
.root_
= dir
.root_
;
121 if (file_name
.dir_
.empty ())
122 file_name
.dir_
= dir
.to_string ();
123 else if (!dir
.to_string ().empty ())
124 file_name
.dir_
= dir
.to_string ()
125 + ::to_string (DIRSEP
) + file_name
.dir_
;
126 if (is_file (file_name
.to_string ()))
127 return file_name
.to_string ();
137 where EXT is from EXTENSIONS.
140 File_path::find (string name
, char const *extensions
[])
142 if (name
.empty () || name
== "-")
145 File_name
file_name (name
);
146 string orig_ext
= file_name
.ext_
;
147 for (int i
= 0; extensions
[i
]; i
++)
149 file_name
.ext_
= orig_ext
;
150 if (*extensions
[i
] && !file_name
.ext_
.empty ())
151 file_name
.ext_
+= ".";
152 file_name
.ext_
+= extensions
[i
];
153 string found
= find (file_name
.to_string ());
161 /** Append a directory, return false if failed. */
163 File_path::try_append (string s
)
176 File_path::to_string () const
179 for (vsize i
= 0; i
< dirs_
.size (); i
++)
182 if (i
< dirs_
.size () - 1)
183 s
+= ::to_string (PATHSEP
);
189 File_path::append (string str
)
191 dirs_
.push_back (str
);
195 File_path::prepend (string str
)
197 dirs_
.insert (dirs_
.begin (), str
);