(ly:kpathsea-gulp-file): New function.
* scm/encoding.scm (read-encoding-file): Use it.
+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.
#include "string.hh"
#include "main.hh"
#include "kpath.hh"
+#include "source-file.hh"
#include "warn.hh"
String
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)
kpse_maketex_option ("tfm", TRUE);
#endif
}
-
-
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;
}
;;;; (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
("T1" . "tex256.enc")
- ;; for testing -- almost adome
+ ;; FIXME: find full Adobe; for testing -- almost Adobe:
("adobe" . "ad.enc")
+
("latin1" . "cork.enc")
;; LilyPond.
(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