/*
file-path.cc - implement File_path
-
+
source file of the Flower Library
-
+
(c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
- Jan Nieuwenhuizen <janneke@gnu.org>
+ Jan Nieuwenhuizen <janneke@gnu.org>
*/
#include "file-path.hh"
#include <cerrno>
#include "config.hh"
-#if HAVE_SYS_STAT_H
+#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#endif
#include "file-name.hh"
+#include "warn.hh"
#ifndef PATHSEP
#define PATHSEP ':'
}
}
-/** Find a file.
-
- Check absolute file name, search in the current dir (DUH! FIXME!),
- in the construction-arg (what's that?), and in any other appended
- directory, in this order.
+static bool
+is_file (String file_name)
+{
+#if 0 /* Check if directory. TODO: encapsulate for autoconf */
+ struct stat sbuf;
+ if (stat (file_name.to_str0 (), &sbuf) != 0)
+ return false;
+
+ if (!(sbuf.st_mode & __S_IFREG))
+ return false;
+#endif
+#if !STAT_MACROS_BROKEN
+ struct stat sbuf;
+ if (stat (file_name.to_str0 (), &sbuf) != 0)
+ return false;
+
+ return !S_ISDIR (sbuf.st_mode);
+#endif
+
+ if (FILE *f = fopen (file_name.to_str0 (), "r"))
+ {
+ fclose (f);
+ return true;
+ }
- @return
- The file name if found, or empty string if not found. */
+ return false;
+}
-String
-File_path::find (String name) const
+static bool
+is_dir (String file_name)
{
- if (!name.length () || (name == "-") )
- return name;
+#if !STAT_MACROS_BROKEN
+ struct stat sbuf;
+ if (stat (file_name.to_str0 (), &sbuf) != 0)
+ return false;
+
+ return S_ISDIR (sbuf.st_mode);
+#endif
- /* Handle absolute file name. */
- if (name[0] == DIRSEP)
+ if (FILE *f = fopen (file_name.to_str0 (), "r"))
{
- if (FILE *f = fopen (name.to_str0 (), "r"))
- {
- fclose (f);
- return name;
- }
+ fclose (f);
+ return true;
}
-
- for (int i = 0; i < size (); i++)
- {
- String file_name = elem (i);
- String sep = ::to_string (DIRSEP);
- String right (file_name.right_string (1));
- if (file_name.length () && right != sep)
- file_name += ::to_string (DIRSEP);
+ return false;
+}
- file_name += name;
-#if 0 /* Check if directory. TODO: encapsulate for autoconf */
- struct stat sbuf;
- if (stat (file_name.to_str0 (), &sbuf) != 0)
- continue;
-
- if (! (sbuf.st_mode & __S_IFREG))
- continue;
-#endif
-#if !STAT_MACROS_BROKEN
-
- struct stat sbuf;
- if (stat (file_name.to_str0 (), &sbuf) != 0)
- continue;
+/** Find a file.
- if (S_ISDIR (sbuf.st_mode))
- continue;
-#endif
+Check absolute file name, search in the current dir (DUH! FIXME!),
+in the construction-arg (what's that?), and in any other appended
+directory, in this order.
- /* ugh */
- FILE *f = fopen (file_name.to_str0 (), "r");
- if (f)
- {
- fclose (f);
- return file_name;
- }
+@return
+The file name if found, or empty string if not found. */
+
+String
+File_path::find (String name) const
+{
+ if (!name.length () || (name == "-"))
+ return name;
+
+#ifdef __MINGW32__
+ if (name[0] == '\\' || (name.length () > 2 && name[2] == '\\'))
+ programming_error ("file name not normalized: " + name);
+#endif /* __MINGW32__ */
+
+ /* Handle absolute file name. */
+ File_name file_name (name);
+ if (file_name.dir_[0] == DIRSEP && is_file (file_name.to_string ()))
+ return file_name.to_string ();
+
+ for (int i = 0, n = size (); i < n; i++)
+ {
+ File_name dir = elem (i);
+ file_name.root_ = dir.root_;
+ dir.root_ = "";
+ file_name.dir_ = dir.to_string ();
+ if (is_file (file_name.to_string ()))
+ return file_name.to_string ();
}
return "";
}
/** Find a file.
-
- Seach in the current dir (DUH! FIXME?), in the construction-arg
- (what's that?, and in any other appended directory, in this order.
- Search for NAME, or name without extension, or name with any of
- EXTENSIONS, in that order.
+Seach in the current dir (DUH! FIXME?), in the construction-arg
+(what's that?, and in any other appended directory, in this order.
+
+Search for NAME, or name without extension, or name with any of
+EXTENSIONS, in that order.
- @return
- The file name if found, or empty string if not found. */
+@return
+The file name if found, or empty string if not found. */
String
File_path::find (String name, char const *extensions[])
{
File_path::try_append (String s)
{
if (s == "")
- s = ".";
- if (FILE *f = fopen (s.to_str0 (), "r"))
+ s = ".";
+ if (is_dir (s))
{
- fclose (f);
append (s);
return true;
}
File_path::to_string () const
{
String s;
- int n = size ();
- for (int i = 0; i < n; i++)
+ for (int i = 0, n = size (); i < n; i++)
{
s = s + elem (i);
if (i < n - 1)
- s += ":";
+ s += ::to_string (PATHSEP);
}
return s;
}