/*
- path.cc - manipulation of paths and filenames.
+ file-path.cc - implement File_path
+
+ source file of the Flower Library
+
+ (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
+ Jan Nieuwenhuizen <janneke@gnu.org>
*/
-#include <stdio.h>
+
#include "file-path.hh"
-#include "flower-debug.hh"
-#ifndef DIRSEP
-#define DIRSEP '/'
+#include <cstdio>
+#include <cerrno>
+using namespace std;
+
+#include "config.hh"
+#if HAVE_SYS_STAT_H
+#include <sys/stat.h>
#endif
+#ifdef __CYGWIN__
+#include <sys/cygwin.h>
+#endif
+
+#include "file-name.hh"
+#include "warn.hh"
+
#ifndef PATHSEP
#define PATHSEP ':'
#endif
-/**
- @param path the original full filename
- @return 4 components of the path. They can be empty
-*/
+Array<String>
+File_path::directories () const
+{
+ return dirs_;
+}
+
void
-split_path (String path,
- String &drive, String &dirs, String &filebase, String &extension)
+File_path::parse_path (String p)
{
- // peel off components, one by one.
- int di = path.index_i (':');
- if (di >= 0)
+ int len;
+ while ((len = p.length ()))
{
- drive = path.left_str (di + 1);
- path = path.right_str (path.length_i () - di -1);
+ int i = p.index (PATHSEP);
+ if (i < 0)
+ i = len;
+ append (p.left_string (i));
+ p = p.right_string (len - i - 1);
}
- else
- drive = "";
+}
+
+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;
- di = path.index_last_i (DIRSEP);
- if (di >=0)
+ 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"))
{
- dirs = path.left_str (di + 1);
- path = path.right_str (path.length_i ()-di -1);
+ fclose (f);
+ return true;
}
- else
- dirs = "";
- di = path.index_last_i ('.');
- if (di >= 0)
+ return false;
+}
+
+bool
+is_dir (String file_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
+
+ if (FILE *f = fopen (file_name.to_str0 (), "r"))
{
- filebase = path.left_str (di);
- extension =path.right_str (path.length_i ()-di);
+ fclose (f);
+ return true;
}
- else
+ return false;
+}
+
+/** 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.
+
+@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.index ('\\') >= 0)
+ 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; i < dirs_.size (); i++)
{
- extension = "";
- filebase = path;
+ File_name file_name (name);
+ File_name dir = dirs_[i];
+ file_name.root_ = dir.root_;
+ dir.root_ = "";
+ if (file_name.dir_.is_empty ())
+ file_name.dir_ = dir.to_string ();
+ else if (!dir.to_string ().is_empty ())
+ file_name.dir_ = dir.to_string ()
+ + ::to_string (DIRSEP) + file_name.dir_;
+ if (is_file (file_name.to_string ()))
+ return file_name.to_string ();
}
+ return "";
}
-void
-File_path::parse_path (String p)
+/*
+ Try to find
+
+ file.EXT,
+
+ where EXT is from EXTENSIONS.
+*/
+String
+File_path::find (String name, char const *extensions[])
{
- int l;
+ if (name.is_empty () || name == "-")
+ return name;
- while ( (l = p.length_i ()) )
+ File_name file_name (name);
+ String orig_ext = file_name.ext_;
+ for (int i = 0; extensions[i]; i++)
{
- int i = p.index_i(PATHSEP);
- if (i <0)
- i = l;
- add (p.left_str(i));
- p = p.right_str (l- i - 1);
+ file_name.ext_ = orig_ext;
+ if (*extensions[i] && !file_name.ext_.is_empty ())
+ file_name.ext_ += ".";
+ file_name.ext_ += extensions[i];
+ String found = find (file_name.to_string ());
+ if (!found.is_empty ())
+ return found;
}
+
+ return "";
}
+/** Append a directory, return false if failed. */
+bool
+File_path::try_append (String s)
+{
+ if (s == "")
+ s = ".";
+ if (is_dir (s))
+ {
+ append (s);
+ return true;
+ }
+ return false;
+}
-
-
-/** find a file.
- It will search in the current dir, in the construction-arg, and
- in any other added path, in this order.
- */
String
-File_path::find (String nm) const
+File_path::to_string () const
{
- fdebug << _("looking for ") << nm << ": ";
- if (!nm.length_i() || (nm == "-") )
- return nm;
- for (int i=0; i < size(); i++)
+ String s;
+ for (int i = 0; i < dirs_.size (); i++)
{
- String path = elem(i);
- String sep (DIRSEP);
- String right(path.right_str (1));
- if (path.length_i () && right != sep)
- path += DIRSEP;
-
- path += nm;
-
- fdebug << path << "? ";
- FILE *f = fopen (path.ch_C(), "r"); // ugh!
- if (f)
- {
- fdebug << _("found\n");
- fclose (f);
- return path;
- }
+ s = s + dirs_[i];
+ if (i < dirs_.size () - 1)
+ s += ::to_string (PATHSEP);
}
- fdebug << "\n";
- return "";
+ return s;
+}
+
+void
+File_path::append (String str)
+{
+ dirs_.push (str);
}
void
-File_path::add (String s)
+File_path::prepend (String str)
{
- push (s);
+ dirs_.insert (str, 0);
}