]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/file-path.cc
Run `make grand-replace'.
[lilypond.git] / flower / file-path.cc
index cf9ad32f965a51a47d08d9c89e47ff007ed8e2ac..0177d40c9f70c3ff7f5b993b0ef3b79f084adfe8 100644 (file)
 /*
-   path.cc - manipulation of paths and filenames.
+  file-path.cc - implement File_path
+
+  source file of the Flower Library
+
+  (c) 1997--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
-#include "config.h"
-#include <stdio.h>
-#include <errno.h>
-#include <limits.h>
+#include "file-path.hh"
 
-#if HAVE_SYS_STAT_H 
+#include <cstdio>
+#include <cerrno>
+
+#include "config.hh"
+#if HAVE_SYS_STAT_H
 #include <sys/stat.h>
 #endif
 
 #ifdef __CYGWIN__
 #include <sys/cygwin.h>
-
-// URGURG
-#include "../lily/include/scm-option.hh"
 #endif
 
-#include "file-path.hh"
-
+#include "file-name.hh"
+#include "warn.hh"
 
 #ifndef PATHSEP
 #define PATHSEP ':'
 #endif
 
-/* We don't have multiple roots, set this to '\0'? */
-#ifndef ROOTSEP
-#define ROOTSEP ':'
-#endif
-
-#ifndef DIRSEP
-#define DIRSEP '/'
-#endif
-
-#ifndef EXTSEP
-#define EXTSEP '.'
-#endif
-
-
-
-#ifdef __CYGWIN__
-static String
-dos_to_posix (String path)
+vector<string>
+File_path::directories () const
 {
-  char buf[PATH_MAX];
-  char *filename = path.get_copy_str0 ();
-  /* urg, wtf? char const* argument gets modified! */
-  cygwin_conv_to_posix_path (filename, buf);
-  delete filename;
-  return buf;
+  return dirs_;
 }
 
-static String
-dos_to_posix_list (String path)
+#include <algorithm>
+void
+File_path::parse_path (string p)
 {
-  char *filename = path.get_copy_str0 ();
-  int len = cygwin_win32_to_posix_path_list_buf_size (filename);
-  if (len < PATH_MAX)
-    len = PATH_MAX;
-  char *buf = new char[len];
-  /* urg, wtf? char const* argument gets modified! */
-  cygwin_win32_to_posix_path_list (filename, buf);
-  delete filename;
-  
-  String ret = buf;
-  delete buf;
-  return ret;
+  concat (dirs_, string_split (p, PATHSEP));
 }
-#endif /* __CYGWIN__ */
 
-/* Join components to full path. */
-String
-Path::string () const
+bool
+is_file (string file_name)
 {
-  String s;
-  if (!root.empty_b ())
-    s = root + to_string (ROOTSEP);
-  if (!dir.empty_b ())
-    s += dir + to_string (DIRSEP);
-  s += base;
-  if (!ext.empty_b ())
-    s += to_string (EXTSEP) + ext;
-  return s;
-}
+#if !STAT_MACROS_BROKEN
+  struct stat sbuf;
+  if (stat (file_name.c_str (), &sbuf) != 0)
+    return false;
 
-/**
-   @param path the original full filename
-   @return 4 components of the path. They can be empty
-*/
-Path
-split_path (String path)
-{
-#ifdef __CYGWIN__
-  /* All system functions would work, even if we don't convert to
-     posix path, but we'd think that \foe\bar\baz.ly is in the cwd.
-     On by default.  */
-  if (!(testing_level_global & 1))
-    path = dos_to_posix (path);
+  return !S_ISDIR (sbuf.st_mode);
 #endif
 
-  Path p;
-  int i = path.index (ROOTSEP);
-  if (i >= 0)
-    {
-      p.root = path.left_string (i);
-      path = path.right_string (path.length () - i - 1);
-    }
-
-  i = path.index_last (DIRSEP);
-  if (i >= 0)
+  if (FILE *f = fopen (file_name.c_str (), "r"))
     {
-      p.dir = path.left_string (i);
-      path = path.right_string (path.length () - i - 1);
+      fclose (f);
+      return true;
     }
 
-  i = path.index_last ('.');
-  if (i >= 0)
-    {
-      p.base = path.left_string (i);
-      p.ext = path.right_string (path.length () - i - 1);
-    }
-  else
-    p.base = path;
-  return p;
+  return false;
 }
 
-void
-File_path::parse_path (String p)
+bool
+is_dir (string file_name)
 {
-#ifdef __CYGWIN__
-  if (testing_level_global & 4)
-    p = dos_to_posix_list (p);
+  /*
+    canonicalize; in particular, trailing slashes should disappear.
+   */
+  file_name = File_name (file_name).to_string ();
+  
+#if !STAT_MACROS_BROKEN
+  struct stat sbuf;
+  if (stat (file_name.c_str (), &sbuf) != 0)
+    return false;
+
+  return S_ISDIR (sbuf.st_mode);
 #endif
 
-  int l;
-  
-  while ((l = p.length ()) )
+  if (FILE *f = fopen (file_name.c_str (), "r"))
     {
-      int i = p.index (PATHSEP);
-      if (i <0) 
-       i = l;
-      add (p.left_string (i));
-      p = p.right_string (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.
 
+@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
+string
+File_path::find (string name) const
 {
-  if (!nm.length () || (nm == "-") )
-    return nm;
-  for (int i=0; i < size (); i++)
+  if (!name.length () || (name == "-"))
+    return name;
+
+#ifdef __MINGW32__
+  if (name.find ('\\') != NPOS)
+    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 (vsize i = 0; i < dirs_.size (); i++)
     {
-      String path  = elem (i);
-      String sep = to_string (DIRSEP);
-      String right (path.right_string (1));
-      if (path.length () && right != sep)
-       path += to_string (DIRSEP);
-
-      path += nm;
-
-
-#if 0
-      /*
-       Check if directory. TODO: encapsulate for autoconf
-       */
-      struct stat sbuf;
-      if (stat (path.to_str0 (), &sbuf) != 0)
-       continue;
-      
-      if (! (sbuf.st_mode & __S_IFREG))
-       continue;
-#endif
-#if !STAT_MACROS_BROKEN
-      
-      struct stat sbuf;
-      if (stat (path.to_str0 (), &sbuf) != 0)
-       continue;
+      File_name file_name (name);
+      File_name dir = (string) dirs_[i];
+      file_name.root_ = dir.root_;
+      dir.root_ = "";
+      if (file_name.dir_.empty ())
+       file_name.dir_ = dir.to_string ();
+      else if (!dir.to_string ().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 "";
+}
 
-      if (S_ISDIR (sbuf.st_mode))
-       continue;
-#endif
+/*
+  Try to find
 
-      FILE *f = fopen (path.to_str0 (), "r"); // ugh!
-      if (f)
-       {
-         fclose (f);
-         return path;
-       }
+  file.EXT,
+
+  where EXT is from EXTENSIONS.
+*/
+string
+File_path::find (string name, char const *extensions[])
+{
+  if (name.empty () || name == "-")
+    return name;
+  
+  File_name file_name (name);
+  string orig_ext = file_name.ext_;
+  for (int i = 0; extensions[i]; i++)
+    {
+      file_name.ext_ = orig_ext;
+      if (*extensions[i] && !file_name.ext_.empty ())
+       file_name.ext_ += ".";
+      file_name.ext_ += extensions[i];
+      string found = find (file_name.to_string ());
+      if (!found.empty ())
+       return found;
     }
+  
   return "";
 }
 
-/**
-   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.to_str0 (), "r");
-  if (!f)
-    return false;
-  fclose (f);
-    
-  add (s);
-  return true;
+    s = ".";
+  if (is_dir (s))
+    {
+      append (s);
+      return true;
+    }
+  return false;
 }
 
-void
-File_path::add (String s)
+string
+File_path::to_string () const
 {
-#ifdef __CYGWIN__
-  if (testing_level_global & 2)
-    s = dos_to_posix (s);
-#endif
+  string s;
+  for (vsize i = 0; i < dirs_.size (); i++)
+    {
+      s = s + dirs_[i];
+      if (i < dirs_.size () - 1)
+       s += ::to_string (PATHSEP);
+    }
+  return s;
+}
 
-  push (s);
+void
+File_path::append (string str)
+{
+  dirs_.push_back (str);
 }
 
-String
-File_path::string () const
+void
+File_path::prepend (string str)
 {
-  String s;
-  for (int i=0; i< size (); i++)
-    {
-      s = s + elem (i);
-      if (i < size () -1 )
-       s += ":";
-    }
-  return s;
+  dirs_.insert (dirs_.begin (), str);
 }