]> git.donarmstrong.com Git - lilypond.git/commitdiff
* lily/kpath.cc (kpathsea_gulp_file_to_string):
authorJan Nieuwenhuizen <janneke@gnu.org>
Tue, 6 Apr 2004 10:02:44 +0000 (10:02 +0000)
committerJan Nieuwenhuizen <janneke@gnu.org>
Tue, 6 Apr 2004 10:02:44 +0000 (10:02 +0000)
(ly:kpathsea-gulp-file): New function.

* scm/encoding.scm (read-encoding-file): Use it.

ChangeLog
lily/kpath.cc
lily/source-file.cc
scm/encoding.scm

index 32c56202f2397a148dfa5333c20f87a960ec77ff..a3e6d0ffad37beb3fc79db6716d2974db534b2bc 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2004-04-06  Jan Nieuwenhuizen  <janneke@gnu.org>
+
+       * lily/kpath.cc (kpathsea_gulp_file_to_string): 
+       (ly:kpathsea-gulp-file): New function.
+
+       * scm/encoding.scm (read-encoding-file): Use it.
+
 2004-04-06  Werner Lemberg  <wl@gnu.org>
 
        * scm/encoding.scm (coding-alist): Fix typo.
index 50cb5c3545485e303d689b6b4f1734f68858c228..d00a08571d7e0bd38f8020fe4d3a1b38220a6dbd 100644 (file)
@@ -38,6 +38,7 @@ extern "C" {
 #include "string.hh"
 #include "main.hh"
 #include "kpath.hh"
+#include "source-file.hh"
 #include "warn.hh"
 
 String
@@ -80,6 +81,74 @@ 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
 initialize_kpathsea (char *av0)
@@ -127,5 +196,3 @@ initialize_kpathsea (char *av0)
   kpse_maketex_option ("tfm", TRUE);
 #endif
 }
-
-
index 108c68b6a23e9b3b2a21bb9dc50860ce3a210d5d..eb83b0e1de40308ac598ac1312a0d5232e8f6dd0 100644 (file)
@@ -39,37 +39,31 @@ Source_file::load_stdin ()
   contents_str0_ = chs.remove_array ();
 }
 
-
-
-char *
-gulp_file (String fn, int* len)
+char*
+gulp_file (String filename, int *filesize)
 {
-  /*
-    let's hope that "b" opens anything binary, and does not apply
-    CR/LF translation
-    */
-  FILE * f =  fopen (fn.to_str0 (), "rb");
-
+  /* "b" must ensure to open literally, avoiding text (CR/LF)
+     conversions.  */
+  FILE *f = fopen (filename.to_str0 (), "rb");
   if (!f)
     {
-      warning (_f ("can't open file: `%s'", fn.to_str0 ()));
+      warning (_f ("can't open file: `%s'", filename.to_str0 ()));
       return 0;
     }
 
-  int ret = fseek (f, 0, SEEK_END);
-
-  *len = ftell (f);
+  fseek (f, 0, SEEK_END);
+  *filesize = ftell (f);
   rewind (f);
-  char *  str = new char[*len+1];
-  str[*len] = 0;
-  ret = fread (str, sizeof (char), *len, f);
 
-  if (ret!=*len)
-    warning (_f ("Huh?  Got %d, expected %d characters", ret, *len));
+  char *str = new char[*filesize + 1];
+  str[*filesize] = 0;
 
+  int bytes_read = fread (str, sizeof (char), *filesize, f);
+  if (bytes_read != *filesize)
+    warning (_f ("Huh?  Got %d, expected %d characters", bytes_read,
+                *filesize));
   fclose (f);
 
-
   return str;
 }
 
index 09ed12a368f16e5dc1ec42ce4dde9e93545fb942..9856776fdeb3b6ee780d920edf306722042bd5eb 100644 (file)
@@ -5,24 +5,20 @@
 ;;;; (c) 2004 Jan Nieuwenhuizen <janneke@gnu.org>
 
 ;; WIP
-;; cp /usr/share/texmf/dvips/base/*.enc mf/out
-;; cp /usr/share/texmf/dvips/tetex/*.enc mf/out
 ;; encoding.ly:
 ;; #(display (reencode-string "adobe" "latin1" "hellö fóebär"))
 ;;
 
 
-(define (read-encoding-file filename)
-  "Read .enc file, returning a vector of symbols."
-  (let* ((raw (ly:gulp-file filename))
+(define-public (read-encoding-file filename)
+  "Read .enc file, return as a vector of symbols."
+  (let* ((raw (ly:kpathsea-gulp-file filename))
         (string (regexp-substitute/global #f "%[^\n]*" raw 'pre "" 'post))
         (start (string-index string #\[))
         (end (string-index string #\]))
         (ps-lst (string-tokenize (substring string (+ start 1) end)))
-        (lst (map (lambda (x) (substring x 1)) ps-lst))
-        (vector (list->vector lst)))
-
-    vector))
+        (lst (map (lambda (x) (substring x 1)) ps-lst)))
+    (list->vector lst)))
 
 (define (make-encoding-table encoding-vector)
   "Return a hash table mapping names to chars. ENCODING-VECTOR is a
@@ -90,8 +86,9 @@ vector of symbols."
         
         ("T1" . "tex256.enc")
 
-        ;; for testing -- almost adome
+        ;; FIXME: find full Adobe; for testing -- almost Adobe:
         ("adobe" . "ad.enc")
+
         ("latin1" . "cork.enc")
         
         ;; LilyPond.
@@ -111,12 +108,7 @@ vector of symbols."
   (cadr (get-coding coding-name)))
 
 
+;;; JUNKME
 ;;; what's this for? --hwn
-(define-public (encoded-index font-coding input-coding code)
-  (format (current-error-port) "CODE: ~S\n" code)
-  (let* ((font (get-coding-table font-coding))
-        (in (get-coding-vector input-coding))
-        (char (vector-ref in code)))
-    (format (current-error-port) "CHAR: ~S\n" char)
-    (hash-ref font char)))
-
+;; (define-public (encoded-index font-coding input-coding code)
+;; This was used by simplistic first incarnation of reencode-string --jcn