2 file-path.cc - implement File_path
4 source file of the Flower Library
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "file-path.hh"
21 #include <sys/cygwin.h>
24 #include "file-name.hh"
31 File_path::parse_path (String p)
34 while ((len = p.length ()) )
36 int i = p.index (PATHSEP);
39 append (p.left_string (i));
40 p = p.right_string (len - i - 1);
46 Seach in the current dir (DUH! FIXME?), in the construction-arg
47 (what's that?, and in any other appended directory, in this order.
50 The file name if found, or empty string if not found. */
53 File_path::find (String name) const
55 if (!name.length () || (name == "-") )
59 TODO: should check for absolute path
61 if (FILE *f =fopen (name.to_str0 (), "r"))
67 for (int i = 0; i < size (); i++)
69 String file_name = elem (i);
70 String sep = ::to_string (DIRSEP);
71 String right (file_name.right_string (1));
72 if (file_name.length () && right != sep)
73 file_name += ::to_string (DIRSEP);
77 #if 0 /* Check if directory. TODO: encapsulate for autoconf */
79 if (stat (file_name.to_str0 (), &sbuf) != 0)
82 if (! (sbuf.st_mode & __S_IFREG))
85 #if !STAT_MACROS_BROKEN
88 if (stat (file_name.to_str0 (), &sbuf) != 0)
91 if (S_ISDIR (sbuf.st_mode))
96 FILE *f = fopen (file_name.to_str0 (), "r");
108 Seach in the current dir (DUH! FIXME?), in the construction-arg
109 (what's that?, and in any other appended directory, in this order.
111 Search for NAME, or name without extension, or name with any of
112 EXTENSIONS, in that order.
115 The file name if found, or empty string if not found. */
117 File_path::find (String name, char const *extensions[])
119 File_name file_name (name);
120 if (name.is_empty () || name == "-")
121 file_name.base_ = "-";
124 String orig_ext = file_name.ext_;
125 for (int i = 0; extensions[i]; i++)
127 file_name.ext_ = orig_ext;
128 if (*extensions[i] && !file_name.ext_.is_empty ())
129 file_name.ext_ += ".";
130 file_name.ext_ += extensions[i];
131 if (!find (file_name.to_string ()).is_empty ())
134 /* Reshuffle extension */
135 file_name = File_name (file_name.to_string ());
137 return file_name.to_string ();
140 /** Append a directory, return false if failed. */
142 File_path::try_append (String s)
146 if (FILE *f = fopen (s.to_str0 (), "r"))
156 File_path::to_string () const
160 for (int i = 0; i < n; i++)