From 7f223f9827a8ace82f1d065fb8cd2f8ed3a143a2 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Tue, 6 Apr 2004 10:02:44 +0000 Subject: [PATCH] * lily/kpath.cc (kpathsea_gulp_file_to_string): (ly:kpathsea-gulp-file): New function. * scm/encoding.scm (read-encoding-file): Use it. --- ChangeLog | 7 +++++ lily/kpath.cc | 71 +++++++++++++++++++++++++++++++++++++++++++-- lily/source-file.cc | 34 +++++++++------------- scm/encoding.scm | 28 +++++++----------- 4 files changed, 100 insertions(+), 40 deletions(-) diff --git a/ChangeLog b/ChangeLog index 32c56202f2..a3e6d0ffad 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2004-04-06 Jan Nieuwenhuizen + + * 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 * scm/encoding.scm (coding-alist): Fix typo. diff --git a/lily/kpath.cc b/lily/kpath.cc index 50cb5c3545..d00a08571d 100644 --- a/lily/kpath.cc +++ b/lily/kpath.cc @@ -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 } - - diff --git a/lily/source-file.cc b/lily/source-file.cc index 108c68b6a2..eb83b0e1de 100644 --- a/lily/source-file.cc +++ b/lily/source-file.cc @@ -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; } diff --git a/scm/encoding.scm b/scm/encoding.scm index 09ed12a368..9856776fde 100644 --- a/scm/encoding.scm +++ b/scm/encoding.scm @@ -5,24 +5,20 @@ ;;;; (c) 2004 Jan Nieuwenhuizen ;; 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 -- 2.39.2