]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/file-path.cc
* scripts/lilypond-invoke-editor.scm (dissect-uri): Handle URIs
[lilypond.git] / flower / file-path.cc
index b7ea0370d8357c1a88b0ee5ab4c268bf63c74a5d..f8504396cf0b9fcae5f09403457e709a317d4683 100644 (file)
 /*
-   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 <cstdio>
+#include <cerrno>
 
-#ifndef PATHSEP
-#define PATHSEP ':'
+#include "config.hh"
+#if HAVE_SYS_STAT_H
+#include <sys/stat.h>
 #endif
 
-/* We don't have multiple roots, set this to '\0'? */
-#ifndef ROOTSEP
-#define ROOTSEP ':'
+#ifdef __CYGWIN__
+#include <sys/cygwin.h>
 #endif
 
-#ifndef DIRSEP
-#define DIRSEP '/'
-#endif
+#include "file-name.hh"
+#include "warn.hh"
 
-#ifndef EXTSEP
-#define EXTSEP '.'
+#ifndef PATHSEP
+#define PATHSEP ':'
 #endif
 
-/* Join components to full path. */
-String
-Path::str () const
+Array<String>
+File_path::directories () 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;
+  return *this;
 }
 
-/**
-   @param path the original full filename
-   @return 4 components of the path. They can be empty
-*/
-Path
-split_path (String path)
+void
+File_path::parse_path (String p)
 {
-  Path p;
-  int i = path.index_i (ROOTSEP);
-  if (i >= 0)
+  int len;
+  while ((len = p.length ()) )
     {
-      p.root = path.left_str (i);
-      path = path.right_str (path.length_i () - i - 1);
+      int i = p.index (PATHSEP);
+      if (i <0) 
+       i = len;
+      append (p.left_string (i));
+      p = p.right_string (len - 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);
-    }
+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
 
-  i = path.index_last_i ('.');
-  if (i >= 0)
+  if (FILE *f = fopen (file_name.to_str0 (), "r"))
     {
-      p.base = path.left_str (i);
-      p.ext = path.right_str (path.length_i () - i - 1);
+      fclose (f);
+      return true;
     }
-  else
-    p.base = path;
-  return p;
+
+  return false;
 }
 
-void
-File_path::parse_path (String p)
+static bool
+is_dir (String file_name)
 {
-  int l;
+#if !STAT_MACROS_BROKEN
+  struct stat sbuf;
+  if (stat (file_name.to_str0 (), &sbuf) != 0)
+    return false;
   
-  while ((l = p.length_i ()) )
+  return S_ISDIR (sbuf.st_mode);
+#endif
+
+  if (FILE *f = fopen (file_name.to_str0 (), "r"))
     {
-      int i = p.index_i (PATHSEP);
-      if (i <0) 
-       i = l;
-      add (p.left_str (i));
-      p = p.right_str (l- i - 1);
+      fclose (f);
+      return true;
     }
+  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.
 
-/** 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 file name if found, or empty string if not found. */
 
-  @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
 {
-  if (!nm.length_i () || (nm == "-") )
-    return nm;
-  for (int i=0; i < size (); i++)
+  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++)
     {
-      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);
-
-      path += nm;
-
-
-#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_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 "";
+}
 
-      FILE *f = fopen (path.ch_C (), "r"); // ugh!
-      if (f)
+/** 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[])
+{
+  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++)
        {
-         fclose (f);
-         return path;
+         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 "";
+  return file_name.to_string ();
 }
 
-/**
-   Add a directory, return false if failed
- */
+/** Append a directory, return false if failed.  */
 bool
-File_path::try_add (String s)
+File_path::try_append (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);
+    s = ".";
+  if (is_dir (s))
+    {
+      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++)
+  for (int i = 0, n = size (); i < n; i++)
     {
       s = s + elem (i);
-      if (i < size () -1 )
-       s += ":";
+      if (i < n - 1)
+       s += ::to_string (PATHSEP);
     }
   return s;
 }