]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/kpath.cc
* lily/kpath.cc (kpathsea_gulp_file_to_string):
[lilypond.git] / lily / kpath.cc
index 07fcbc62b89c7ff47e952cb81885cd57bcd24a43..d00a08571d7e0bd38f8020fe4d3a1b38220a6dbd 100644 (file)
@@ -3,7 +3,7 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 2000--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c) 2000--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
 
 #include <stdio.h>
@@ -38,6 +38,7 @@ extern "C" {
 #include "string.hh"
 #include "main.hh"
 #include "kpath.hh"
+#include "source-file.hh"
 #include "warn.hh"
 
 String
@@ -46,13 +47,13 @@ kpathsea_find_afm (char const * name)
 #if (KPATHSEA && HAVE_KPSE_FIND_FILE)
   char * name_ptr =  kpse_find_file (name, kpse_afm_format, true);
 
-  if(!name_ptr)
+  if (!name_ptr)
     {
   /*
     don't mutter about afms, since we try to find them first, and lots of
     TFMs don't have AFMs. 
    */
-      //      warning (_f("kpathsea couldn't find AFM file `%s'", name));
+      //      warning (_f ("kpathsea couldn't find AFM file `%s'", name));
     }
   else
     return name_ptr;
@@ -71,10 +72,8 @@ kpathsea_find_tfm (char const * name)
   
 #if (KPATHSEA && HAVE_KPSE_FIND_FILE)
   char * name_ptr =  kpse_find_file (name, kpse_tfm_format, true);
-  if(!name_ptr)
-    {
-      warning (_f("Kpathsea couldn't find TFM file `%s'", name));
-    }
+  if (!name_ptr)
+    warning (_f ("kpathsea can not find TFM file: `%s'", name));
   else
     return name_ptr;
   
@@ -82,9 +81,77 @@ kpathsea_find_tfm (char const * name)
   return "";
 }
 
+#if KPATHSEA
+/* FIXME: this should be part of kpathsea */
+
+static kpse_file_format_type
+kpathsea_find_format (String name)
+{
+  for (int i = 0; i < kpse_last_format; i++)
+    {
+      if (!kpse_format_info[i].type)
+        kpse_init_format ((kpse_file_format_type) i);
+
+      char const **suffixes[] = { kpse_format_info[i].suffix,
+                                 kpse_format_info[i].alt_suffix };
+      for (int j = 0; j < 2; j++)
+       for (char const **p = suffixes[j]; p && *p; p++)
+         {
+           String suffix = *p;
+           if (name.right_string (suffix.length ()) == suffix)
+             return (kpse_file_format_type) i;
+         }
+    }
+  return kpse_last_format;
+}
+#endif
+
+String
+kpathsea_gulp_file_to_string (String name)
+{
+  String filename = global_path.find (name);
+
+#if (KPATHSEA && HAVE_KPSE_FIND_FILE)
+  if (filename.is_empty ())
+    {
+      char *p = kpse_find_file (name.to_str0 (), kpathsea_find_format (name),
+       true);
+      if (p)
+       filename = p;
+      else
+       warning (_f ("kpathsea can not find file: `%s'", name));
+    }
+#endif
+
+  if (filename.is_empty ())
+    error (_f ("can't find file: `%s'", name));
+
+  if (verbose_global_b)
+    progress_indication ("[" + filename);
+
+  int filesize;
+  char *str = gulp_file (filename, &filesize);
+  String string (str);
+  delete[] str;
+  
+  if (verbose_global_b)
+    progress_indication ("]");
+
+  return string;
+}
+
+LY_DEFINE (ly_kpathsea_gulp_file, "ly:kpathsea-gulp-file",
+          1, 0, 0, (SCM name),
+          "Read the file @var{name}, and return its contents in a string.  "
+          "The file is looked up using the search path and kpathsea.")
+{
+  SCM_ASSERT_TYPE (gh_string_p (name), name, SCM_ARG1, __FUNCTION__, "string");
+  return scm_makfrom0str
+    (kpathsea_gulp_file_to_string (ly_scm2string (name)).to_str0 ());
+}
 
 void
-init_kpath (char *av0)
+initialize_kpathsea (char *av0)
 {
 #if KPATHSEA && HAVE_KPATHSEA_KPATHSEA_H
   /*
@@ -129,5 +196,3 @@ init_kpath (char *av0)
   kpse_maketex_option ("tfm", TRUE);
 #endif
 }
-
-