]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/file-path.cc
Nitpick run.
[lilypond.git] / flower / file-path.cc
index eb08b9251a7702e91672deb9efa4e16807d013fc..d15c92caf4ca18a4bc9b548cab84612300002d19 100644 (file)
@@ -1,10 +1,10 @@
 /*
   file-path.cc - implement File_path
-   
+
   source file of the Flower Library
-  
-  (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
-                 Jan Nieuwenhuizen <janneke@gnu.org>
+
+  (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
 #include "file-path.hh"
@@ -13,7 +13,7 @@
 #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 ':'
 #endif
 
+Array<String>
+File_path::directories () const
+{
+  return dirs_;
+}
+
 void
 File_path::parse_path (String p)
 {
   int len;
-  while ((len = p.length ()) )
+  while ((len = p.length ()))
     {
       int i = p.index (PATHSEP);
-      if (i <0) 
+      if (i < 0)
        i = len;
       append (p.left_string (i));
       p = p.right_string (len - i - 1);
     }
 }
 
+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 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"))
+    {
+      fclose (f);
+      return true;
+    }
+  return false;
+}
+
 /** 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.
 
-  @return
-  The file name if found, or empty string if not found. */
+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 == "-") )
+  if (!name.length () || (name == "-"))
     return name;
-  int n = size ();
-  for (int i = 0; i < n; 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);
 
-      file_name += name;
+#ifdef __MINGW32__
+  if (name.index ('\\') >= 0)
+    programming_error ("file name not normalized: " + name);
+#endif /* __MINGW32__ */
 
-#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;
-
-      if (S_ISDIR (sbuf.st_mode))
-       continue;
-#endif
+  /* 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 ();
 
-      /* ugh */
-      FILE *f = fopen (file_name.to_str0 (), "r");
-      if (f)
-       {
-         fclose (f);
-         return file_name;
-       }
+  for (int i = 0; i < dirs_.size (); i++)
+    {
+      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 "";
 }
 
-/** 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.
+/*
+  Try to find
 
-  Search for NAME, or name without extension, or name with any of
-  EXTENSIONS, in that order.
+  file.EXT,
 
-  @return
-  The file name if found, or empty string if not found. */
+  where EXT is from EXTENSIONS.
+*/
 String
 File_path::find (String name, char const *extensions[])
 {
@@ -122,6 +163,7 @@ File_path::find (String name, char const *extensions[])
          if (!find (file_name.to_string ()).is_empty ())
            break;
        }
+
       /* Reshuffle extension */
       file_name = File_name (file_name.to_string ());
     }
@@ -133,10 +175,9 @@ bool
 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;
     }
@@ -147,12 +188,23 @@ String
 File_path::to_string () const
 {
   String s;
-  int n = size ();
-  for (int i = 0; i < n; i++)
+  for (int i = 0; i < dirs_.size (); i++)
     {
-      s = s + elem (i);
-      if (i < n - 1)
-       s += ":";
+      s = s + dirs_[i];
+      if (i < dirs_.size () - 1)
+       s += ::to_string (PATHSEP);
     }
   return s;
 }
+
+void
+File_path::append (String str)
+{
+  dirs_.push (str);
+}
+
+void
+File_path::prepend (String str)
+{
+  dirs_.insert (str, 0);
+}