]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/file-path.cc
Imported sources
[lilypond.git] / flower / file-path.cc
index d7d6d27c37195f9cb8b154047f6012fa867b8f7c..c59d218cd1f2dce2f4f071cb647005bea0527d7d 100644 (file)
@@ -5,20 +5,29 @@
 #include "config.h"
 #include <stdio.h>
 #include <errno.h>
+#include <limits.h>
 
 #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 "flower-debug.hh"
+
 
 #ifndef PATHSEP
 #define PATHSEP ':'
 #endif
 
+/* We don't have multiple roots, set this to '\0'? */
 #ifndef ROOTSEP
-#define ROOTSEP '/'
+#define ROOTSEP ':'
 #endif
 
 #ifndef DIRSEP
 #define EXTSEP '.'
 #endif
 
+
+
+#ifdef __CYGWIN__
+static String
+dos_to_posix (String path)
+{
+  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;
+}
+
+static String
+dos_to_posix_list (String path)
+{
+  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;
+}
+#endif /* __CYGWIN__ */
+
 /* Join components to full path. */
 String
-Path::str () const
+Path::to_string () const
 {
   String s;
-  if (!root.empty_b ())
-    s = root + to_str (ROOTSEP);
-  if (!dir.empty_b ())
-    s += dir + to_str (DIRSEP);
+  if (!root.is_empty ())
+    s = root + ::to_string (ROOTSEP);
+  if (!dir.is_empty ())
+    s += dir + ::to_string (DIRSEP);
   s += base;
-  if (!ext.empty_b ())
-    s += to_str (EXTSEP) + ext;
+  if (!ext.is_empty ())
+    s += ::to_string (EXTSEP) + ext;
   return s;
 }
 
@@ -51,26 +92,34 @@ Path::str () const
 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);
+#endif
+
   Path p;
-  int i = path.index_i (ROOTSEP);
+  int i = path.index (ROOTSEP);
   if (i >= 0)
     {
-      p.root = path.left_str (i);
-      path = path.right_str (path.length_i () - i); // - 1);
+      p.root = path.left_string (i);
+      path = path.right_string (path.length () - i - 1);
     }
 
-  i = path.index_last_i (DIRSEP);
+  i = path.index_last (DIRSEP);
   if (i >= 0)
     {
-      p.dir = path.left_str (i);
-      path = path.right_str (path.length_i () - i - 1);
+      p.dir = path.left_string (i);
+      path = path.right_string (path.length () - i - 1);
     }
 
-  i = path.index_last_i ('.');
+  i = path.index_last ('.');
   if (i >= 0)
     {
-      p.base = path.left_str (i);
-      p.ext = path.right_str (path.length_i () - i - 1);
+      p.base = path.left_string (i);
+      p.ext = path.right_string (path.length () - i - 1);
     }
   else
     p.base = path;
@@ -80,15 +129,20 @@ split_path (String path)
 void
 File_path::parse_path (String p)
 {
+#ifdef __CYGWIN__
+  if (testing_level_global & 4)
+    p = dos_to_posix_list (p);
+#endif
+
   int l;
   
-  while ( (l = p.length_i ()) )
+  while ((l = 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);
+      add (p.left_string (i));
+      p = p.right_string (l- i - 1);
     }
 }
 
@@ -105,50 +159,47 @@ File_path::parse_path (String p)
 String
 File_path::find (String nm) const
 {
-  DEBUG_OUT << "looking for" << nm << ": ";
-  if (!nm.length_i() || (nm == "-") )
+  if (!nm.length () || (nm == "-") )
     return nm;
-  for (int i=0; i < size(); i++)
+  for (int i=0; i < size (); 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);
+      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;
 
-      DEBUG_OUT << path << "? ";
 
 #if 0
       /*
        Check if directory. TODO: encapsulate for autoconf
        */
       struct stat sbuf;
-      if (stat (path.ch_C(), &sbuf) == ENOENT)
+      if (stat (path.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 (path.to_str0 (), &sbuf) != 0)
        continue;
-      
+
       if (S_ISDIR (sbuf.st_mode))
        continue;
 #endif
 
-      FILE *f = fopen (path.ch_C(), "r"); // ugh!
+      FILE *f = fopen (path.to_str0 (), "r"); // ugh!
       if (f)
        {
-         DEBUG_OUT << "found\n";
          fclose (f);
          return path;
        }
     }
-  DEBUG_OUT << '\n';
   return "";
 }
 
@@ -160,23 +211,28 @@ File_path::try_add (String s)
 {
   if (s == "")
     s =  ".";
-  FILE  * f = fopen (s.ch_C(), "r");
+  FILE  * f = fopen (s.to_str0 (), "r");
   if (!f)
     return false;
   fclose (f);
     
-  push (s);
+  add (s);
   return true;
 }
 
 void
 File_path::add (String s)
 {
+#ifdef __CYGWIN__
+  if (testing_level_global & 2)
+    s = dos_to_posix (s);
+#endif
+
   push (s);
 }
 
 String
-File_path::str () const
+File_path::to_string () const
 {
   String s;
   for (int i=0; i< size (); i++)