From d39092b97931804c559f1938f96cbadfc77bfb59 Mon Sep 17 00:00:00 2001
From: hanwen <hanwen>
Date: Mon, 22 Aug 2005 14:49:23 +0000
Subject: [PATCH] * lily/lily-guile.cc (gulp_file_to_string): take size
 argument.

* lily/general-scheme.cc (LY_DEFINE): take optional size argument.
---
 ChangeLog               |  4 ++++
 lily/general-scheme.cc  | 13 ++++++++++---
 lily/lily-guile.cc      |  4 ++--
 lily/lily-parser.cc     |  2 +-
 lily/pfb.cc             |  4 ++--
 lily/source-file.cc     | 24 ++++++++++++++++--------
 scm/backend-library.scm |  1 +
 scm/ps-to-png.scm       |  8 +++++---
 8 files changed, 41 insertions(+), 19 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 47b4c746fd..7f3e001c05 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2005-08-22  Han-Wen Nienhuys  <hanwen@xs4all.nl>
 
+	* lily/lily-guile.cc (gulp_file_to_string): take size argument.
+
+	* lily/general-scheme.cc (LY_DEFINE): take optional size argument.
+
 	* input/regression/tie-manual.ly: new file
 
 	* input/regression/tie-chord.ly: update.
diff --git a/lily/general-scheme.cc b/lily/general-scheme.cc
index d080429791..6541ab8eae 100644
--- a/lily/general-scheme.cc
+++ b/lily/general-scheme.cc
@@ -52,12 +52,19 @@ LY_DEFINE (ly_find_file, "ly:find-file",
   buffering.)
 */
 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
-	   1, 0, 0, (SCM name),
+	   1, 1, 0, (SCM name, SCM size),
 	   "Read the file @var{name}, and return its contents in a string.  "
-	   "The file is looked up using the search path.")
+	   "The file is looked up using the search path. ")
 {
   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
-  String contents = gulp_file_to_string (ly_scm2string (name), true);
+  int sz = -1;
+  if (size != SCM_UNDEFINED)
+    {
+      SCM_ASSERT_TYPE (scm_is_number (size), size, SCM_ARG2, __FUNCTION__, "number");
+      sz = scm_to_int (size);
+    }
+  
+  String contents = gulp_file_to_string (ly_scm2string (name), true, sz);
   return scm_from_locale_stringn (contents.get_str0 (), contents.length ());
 }
 
diff --git a/lily/lily-guile.cc b/lily/lily-guile.cc
index f0f90afa65..1ef5370e8e 100644
--- a/lily/lily-guile.cc
+++ b/lily/lily-guile.cc
@@ -84,7 +84,7 @@ ly_symbol2string (SCM s)
 }
 
 String
-gulp_file_to_string (String fn, bool must_exist)
+gulp_file_to_string (String fn, bool must_exist, int size)
 {
   String s = global_path.find (fn);
   if (s == "")
@@ -103,7 +103,7 @@ gulp_file_to_string (String fn, bool must_exist)
   if (be_verbose_global)
     progress_indication ("[" + s);
 
-  int n;
+  int n = sz;
   char *str = gulp_file (s, &n);
   String result ((Byte *) str, n);
   delete[] str;
diff --git a/lily/lily-parser.cc b/lily/lily-parser.cc
index 6987c052e8..feae02f645 100644
--- a/lily/lily-parser.cc
+++ b/lily/lily-parser.cc
@@ -103,7 +103,7 @@ Lily_parser::parse_file (String init, String name, String out_name)
 
   File_name f (name);
   String s = global_path.find (f.base_ + ".twy");
-  s = gulp_file_to_string (s, false);
+  s = gulp_file_to_string (s, false, -1);
   scm_eval_string (scm_makfrom0str (s.to_str0 ()));
 
   /* Read .ly IN_FILE, lex, parse, write \score blocks from IN_FILE to
diff --git a/lily/pfb.cc b/lily/pfb.cc
index d5961f12f5..f255cb846e 100644
--- a/lily/pfb.cc
+++ b/lily/pfb.cc
@@ -77,11 +77,11 @@ LY_DEFINE (ly_pfb_to_pfa, "ly:pfb->pfa",
 		   SCM_ARG1, __FUNCTION__, "string");
 
   String file_name = ly_scm2string (pfb_file_name);
-  int len;
+  int len = -1;
 
   if (be_verbose_global)
     progress_indication ("[" + file_name);
-
+  
   char *str = gulp_file (file_name, &len);
   char *pfa = pfb2pfa ((Byte *)str, len);
 
diff --git a/lily/source-file.cc b/lily/source-file.cc
index af9005e9e7..fdd628fbfb 100644
--- a/lily/source-file.cc
+++ b/lily/source-file.cc
@@ -57,16 +57,21 @@ gulp_file (String filename, int *filesize)
     }
 
   fseek (f, 0, SEEK_END);
-  *filesize = ftell (f);
+  int real_size = ftell (f);
+  int read_count = real_size;
+
+  if (*filesize >= 0)
+    read_count = min (read_count, *filesize);
+  
   rewind (f);
 
-  char *str = new char[*filesize + 1];
-  str[*filesize] = 0;
+  char *str = new char[read_count + 1];
+  str[read_count] = 0;
 
-  int bytes_read = fread (str, sizeof (char), *filesize, f);
-  if (bytes_read != *filesize)
+  int bytes_read = fread (str, sizeof (char), read_count, f);
+  if (bytes_read != read_count)
     warning (_f ("expected to read %d characters, got %d", bytes_read,
-		 *filesize));
+		 read_count));
   fclose (f);
 
   return str;
@@ -95,8 +100,11 @@ Source_file::Source_file (String filename_string)
   if (filename_string == "-")
     load_stdin ();
   else
-    contents_str0_ = gulp_file (filename_string, &length_);
-
+    {
+      length_ = -1;
+      contents_str0_ = gulp_file (filename_string, &length_);
+    }
+  
   pos_str0_ = to_str0 ();
 
   init_port ();
diff --git a/scm/backend-library.scm b/scm/backend-library.scm
index 6943b97610..18c9c0d2bc 100644
--- a/scm/backend-library.scm
+++ b/scm/backend-library.scm
@@ -94,6 +94,7 @@
   (let ((paper-size (sanitize-command-option paper-size-name))
 	(verbose (ly:get-option 'verbose))
 	(rename-page-1 #f))
+
     (ly:message (_ "Converting to ~a...") "PNG")
     (make-ps-images name resolution paper-size rename-page-1 verbose
 		    (ly:get-option 'anti-alias-factor))
diff --git a/scm/ps-to-png.scm b/scm/ps-to-png.scm
index dfcb9551b0..ddaa3bc18a 100644
--- a/scm/ps-to-png.scm
+++ b/scm/ps-to-png.scm
@@ -42,8 +42,8 @@
 
 (define (gulp-port port max-length)
   (let ((str (make-string max-length)))
-    (read-string!/partial str port)
-    str))
+    (read-string!/partial str port 0 max-length)
+   str))
 
 (define (dir-listing dir-name)
   (define (dir-helper dir lst)
@@ -121,7 +121,8 @@
 	 )
    
    (let* ((base (basename (re-sub "[.]e?ps" "" ps-name)))
-	  (header (gulp-port (open-file ps-name "r") 10240))
+	  (header (ly:gulp-file ps-name))
+;	  (header (gulp-port (open-file ps-name "r") 10240))
 	  (png1 (string-append base ".png"))
 	  (pngn (string-append base "-page%d.png"))
 	  (pngn-re (re-sub "%d" "[0-9]*" pngn))
@@ -152,6 +153,7 @@
 			   (* aa-factor resolution) ps-name))
 	  (status 0)
 	  (files '()))
+
      
      (for-each delete-file (append (dir-re "." png1)
 				   (dir-re "." pngn-re)))
-- 
2.39.5