]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/file-path.cc
patch::: 1.3.101.jcn2
[lilypond.git] / flower / file-path.cc
index 89c05dfb165f7791936d5263bf9ace46d2b2dadd..80c34db4c1701eb9e7dec6dbe065cdece72e9688 100644 (file)
@@ -1,8 +1,14 @@
 /*
    path.cc - manipulation of paths and filenames.
 */
+
+#include "config.h"
 #include <stdio.h>
-#include "config.hh"
+#include <errno.h>
+#if HAVE_SYS_STAT_H 
+#include <sys/stat.h>
+#endif
+
 #include "file-path.hh"
 #include "flower-debug.hh"
 
@@ -72,7 +78,7 @@ File_path::parse_path (String p)
 
 
 
-/** find a file.
+/** Find a file.
   It will search in the current dir, in the construction-arg, and
   in any other added path, in this order.
 
@@ -82,7 +88,7 @@ File_path::parse_path (String p)
 String
 File_path::find (String nm) const
 {
-  fdebug << "looking for" << nm << ": ";
+  DEBUG_OUT << "looking for" << nm << ": ";
   if (!nm.length_i() || (nm == "-") )
     return nm;
   for (int i=0; i < size(); i++)
@@ -95,21 +101,72 @@ File_path::find (String nm) const
 
       path += nm;
 
-      fdebug << path << "? ";
+      DEBUG_OUT << path << "? ";
+
+#if 0
+      /*
+       Check if directory. TODO: encapsulate for autoconf
+       */
+      struct stat sbuf;
+      if (stat (path.ch_C(), &sbuf) == ENOENT)
+       continue;
+      
+      if (!(sbuf.st_mode & __S_IFREG))
+       continue;
+#endif
+#if !STAT_MACROS_BROKEN
+      struct stat sbuf;
+      if (stat (path.ch_C (), &sbuf) == ENOENT)
+       continue;
+      
+      if (S_ISDIR (sbuf.st_mode))
+       continue;
+#endif
+
       FILE *f = fopen (path.ch_C(), "r"); // ugh!
       if (f)
        {
-         fdebug << "found\n";
+         DEBUG_OUT << "found\n";
          fclose (f);
          return path;
        }
     }
-  fdebug << '\n';
+  DEBUG_OUT << '\n';
   return "";
 }
 
+/**
+   Add a directory, return false if failed
+ */
+bool
+File_path::try_add (String s)
+{
+  if (s == "")
+    s =  ".";
+  FILE  * f = fopen (s.ch_C(), "r");
+  if (!f)
+    return false;
+  fclose (f);
+    
+  push (s);
+  return true;
+}
+
 void
 File_path::add (String s)
 {
-   push (s); 
+  push (s);
+}
+
+String
+File_path::str () const
+{
+  String s;
+  for (int i=0; i< size (); i++)
+    {
+      s = s + elem (i);
+      if (i < size () -1 )
+       s += ":";
+    }
+  return s;
 }