/*
- path.cc - manipulation of paths and filenames.
-*/
+ file-path.cc - implement File_path
-#include "config.h"
-#include <stdio.h>
-#include <errno.h>
+ source file of the Flower Library
-#if HAVE_SYS_STAT_H
-#include <sys/stat.h>
-#endif
+ (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+ Jan Nieuwenhuizen <janneke@gnu.org>
+*/
#include "file-path.hh"
-#include "flower-debug.hh"
-#ifndef PATHSEP
-#define PATHSEP ':'
-#endif
+#include <cstdio>
+#include <cerrno>
-/* We don't have multiple roots, set this to '\0'? */
-#ifndef ROOTSEP
-#define ROOTSEP ':'
+#include "config.hh"
+#if HAVE_SYS_STAT_H
+#include <sys/stat.h>
#endif
-#ifndef DIRSEP
-#define DIRSEP '/'
+#ifdef __CYGWIN__
+#include <sys/cygwin.h>
#endif
-#ifndef EXTSEP
-#define EXTSEP '.'
-#endif
+#include "file-name.hh"
-/* Join components to full path. */
-String
-Path::str () const
-{
- String s;
- if (!root.empty_b ())
- s = root + to_str (ROOTSEP);
- if (!dir.empty_b ())
- s += dir + to_str (DIRSEP);
- s += base;
- if (!ext.empty_b ())
- s += to_str (EXTSEP) + ext;
- return s;
-}
+#ifndef PATHSEP
+#define PATHSEP ':'
+#endif
-/**
- @param path the original full filename
- @return 4 components of the path. They can be empty
-*/
-Path
-split_path (String path)
+Array<String>
+File_path::directories () const
{
- Path p;
- int i = path.index_i (ROOTSEP);
- if (i >= 0)
- {
- p.root = path.left_str (i);
- path = path.right_str (path.length_i () - i - 1);
- }
-
- i = path.index_last_i (DIRSEP);
- if (i >= 0)
- {
- p.dir = path.left_str (i);
- path = path.right_str (path.length_i () - i - 1);
- }
-
- i = path.index_last_i ('.');
- if (i >= 0)
- {
- p.base = path.left_str (i);
- p.ext = path.right_str (path.length_i () - i - 1);
- }
- else
- p.base = path;
- return p;
+ return *this;
}
void
File_path::parse_path (String p)
{
- int l;
-
- while ( (l = p.length_i ()) )
+ int len;
+ while ((len = p.length ()) )
{
- int i = p.index_i(PATHSEP);
+ int i = p.index (PATHSEP);
if (i <0)
- i = l;
- add (p.left_str(i));
- p = p.right_str (l- i - 1);
+ i = len;
+ append (p.left_string (i));
+ p = p.right_string (len - i - 1);
}
}
+/** 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. */
-/** Find a file.
- It will search in the current dir, in the construction-arg, and
- in any other added path, in this order.
-
- @return
- The full path if found, or empty string if not found
- */
String
-File_path::find (String nm) const
+File_path::find (String name) const
{
- DEBUG_OUT << "looking for" << nm << ": ";
- if (!nm.length_i() || (nm == "-") )
- return nm;
- for (int i=0; i < size(); i++)
+ if (!name.length () || (name == "-"))
+ return name;
+
+ /* Handle absolute file name. */
+ if (name[0] == DIRSEP)
{
- String path = elem(i);
- String sep = to_str (DIRSEP);
- String right(path.right_str (1));
- if (path.length_i () && right != sep)
- path += to_str (DIRSEP);
+ if (FILE *f = fopen (name.to_str0 (), "r"))
+ {
+ fclose (f);
+ return name;
+ }
+ }
- path += nm;
+ 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);
- DEBUG_OUT << path << "? ";
+ file_name += name;
-#if 0
- /*
- Check if directory. TODO: encapsulate for autoconf
- */
+#if 0 /* Check if directory. TODO: encapsulate for autoconf */
struct stat sbuf;
- if (stat (path.ch_C(), &sbuf) == ENOENT)
+ if (stat (file_name.to_str0 (), &sbuf) != 0)
continue;
-
- if (!(sbuf.st_mode & __S_IFREG))
+
+ if (! (sbuf.st_mode & __S_IFREG))
continue;
#endif
#if !STAT_MACROS_BROKEN
+
struct stat sbuf;
- if (stat (path.ch_C (), &sbuf) == ENOENT)
+ if (stat (file_name.to_str0 (), &sbuf) != 0)
continue;
-
+
if (S_ISDIR (sbuf.st_mode))
continue;
#endif
- FILE *f = fopen (path.ch_C(), "r"); // ugh!
+ /* ugh */
+ FILE *f = fopen (file_name.to_str0 (), "r");
if (f)
{
- DEBUG_OUT << "found\n";
fclose (f);
- return path;
+ return file_name;
}
}
- DEBUG_OUT << '\n';
return "";
}
-/**
- Add a directory, return false if failed
- */
-bool
-File_path::try_add (String s)
+/** 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.
+
+@return
+The file name if found, or empty string if not found. */
+String
+File_path::find (String name, char const *extensions[])
{
- if (s == "")
- s = ".";
- FILE * f = fopen (s.ch_C(), "r");
- if (!f)
- return false;
- fclose (f);
-
- push (s);
- return true;
+ File_name file_name (name);
+ if (name.is_empty () || name == "-")
+ file_name.base_ = "-";
+ else
+ {
+ String orig_ext = file_name.ext_;
+ for (int i = 0; extensions[i]; i++)
+ {
+ file_name.ext_ = orig_ext;
+ if (*extensions[i] && !file_name.ext_.is_empty ())
+ file_name.ext_ += ".";
+ file_name.ext_ += extensions[i];
+ if (!find (file_name.to_string ()).is_empty ())
+ break;
+ }
+ /* Reshuffle extension */
+ file_name = File_name (file_name.to_string ());
+ }
+ return file_name.to_string ();
}
-void
-File_path::add (String s)
+/** Append a directory, return false if failed. */
+bool
+File_path::try_append (String s)
{
- push (s);
+ if (s == "")
+ s = ".";
+ if (FILE *f = fopen (s.to_str0 (), "r"))
+ {
+ fclose (f);
+ append (s);
+ return true;
+ }
+ return false;
}
String
-File_path::str () const
+File_path::to_string () const
{
String s;
- for (int i=0; i< size (); i++)
+ int n = size ();
+ for (int i = 0; i < n; i++)
{
s = s + elem (i);
- if (i < size () -1 )
+ if (i < n - 1)
s += ":";
}
return s;