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>
20 #include <sys/cygwin.h>
23 #include "file-name.hh"
24 #include "file-path.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 == "-") )
58 for (int i = 0; i < n; i++)
60 String file_name = elem (i);
61 String sep = ::to_string (DIRSEP);
62 String right (file_name.right_string (1));
63 if (file_name.length () && right != sep)
64 file_name += ::to_string (DIRSEP);
68 #if 0 /* Check if directory. TODO: encapsulate for autoconf */
70 if (stat (file_name.to_str0 (), &sbuf) != 0)
73 if (! (sbuf.st_mode & __S_IFREG))
76 #if !STAT_MACROS_BROKEN
79 if (stat (file_name.to_str0 (), &sbuf) != 0)
82 if (S_ISDIR (sbuf.st_mode))
87 FILE *f = fopen (file_name.to_str0 (), "r");
99 Seach in the current dir (DUH! FIXME?), in the construction-arg
100 (what's that?, and in any other appended directory, in this order.
102 Search for NAME, or name without extension, or name with any of
103 EXTENSIONS, in that order.
106 The file name if found, or empty string if not found. */
108 File_path::find (String name, char const *extensions[])
110 File_name file_name (name);
111 if (name.is_empty () || name == "-")
112 file_name.base_ = "-";
115 String orig_ext = file_name.ext_;
116 for (int i = 0; extensions[i]; i++)
118 file_name.ext_ = orig_ext;
119 if (*extensions[i] && !file_name.ext_.is_empty ())
120 file_name.ext_ += ".";
121 file_name.ext_ += extensions[i];
122 if (!find (file_name.to_string ()).is_empty ())
125 /* Reshuffle extension */
126 file_name = File_name (file_name.to_string ());
128 return file_name.to_string ();
131 /** Append a directory, return false if failed. */
133 File_path::try_append (String s)
137 if (FILE *f = fopen (s.to_str0 (), "r"))
147 File_path::to_string () const
151 for (int i = 0; i < n; i++)