2 source-file.cc -- implement Source_file
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 #include "source-file.hh"
19 #define istringstream(x) istrstream(x, length ())
25 Source_file::load_stdin ()
30 Array<char> chs; // ugh.
31 while ((c = fgetc (stdin)) != EOF)
35 length_ = chs.size ();
36 contents_str0_ = chs.remove_array ();
40 gulp_file (String filename, int *filesize)
42 /* "b" must ensure to open literally, avoiding text (CR/LF)
44 FILE *f = fopen (filename.to_str0 (), "rb");
47 warning (_f ("can't open file: `%s'", filename.to_str0 ()));
51 fseek (f, 0, SEEK_END);
52 *filesize = ftell (f);
55 char *str = new char[*filesize + 1];
58 int bytes_read = fread (str, sizeof (char), *filesize, f);
59 if (bytes_read != *filesize)
60 warning (_f ("Huh? Got %d, expected %d characters", bytes_read,
67 Source_file::Source_file (String filename, String data)
71 contents_str0_ = data.get_copy_str0();
72 length_ = data.length ();
73 pos_str0_ = to_str0 ();
76 for (int i = 0; i < length_; i++)
77 if (contents_str0_[i] == '\n')
78 newline_locations_.push (contents_str0_ + i);
81 Source_file::Source_file (String filename_string)
83 name_ = filename_string;
87 if (filename_string == "-")
90 contents_str0_ = gulp_file (filename_string, &length_);
92 pos_str0_ = to_str0 ();
96 for (int i = 0; i < length_; i++)
97 if (contents_str0_[i] == '\n')
98 newline_locations_.push (contents_str0_ + i);
102 Source_file::init_port ()
104 SCM str = scm_makfrom0str (contents_str0_);
105 str_port_ = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG, __FUNCTION__);
106 scm_set_port_filename_x (str_port_, scm_makfrom0str (name_.get_str0 ()));
110 Source_file::tell () const
112 return pos_str0_ - contents_str0_;
116 Source_file::get_istream ()
120 if (length ()) // can-t this be done without such a hack?
121 istream_ = new std::istringstream (to_str0 ());
124 istream_ = new std::istringstream ("");
125 istream_->setstate (std::ios::eofbit);
126 // istream_->set (ios::eofbit);
133 Source_file::file_line_column_string (char const *context_str0) const
136 return " (" + _ ("position unknown") + ")";
138 return name_string () + ":" + to_string (get_line (context_str0))
139 + ":" + to_string (get_char (context_str0));
143 Source_file::name_string () const
148 Source_file::~Source_file ()
152 delete[] contents_str0_;
156 Source_file::line_slice (char const* pos_str0) const
158 if (!contains (pos_str0))
161 char const* data_str0 = to_str0 ();
162 char const * eof_C_ = data_str0 + length ();
164 if (pos_str0 == eof_C_)
166 char const* begin_str0 = pos_str0;
167 while (begin_str0 > data_str0)
168 if (*--begin_str0 == '\n')
174 char const* end_str0 = pos_str0;
175 while (end_str0 < eof_C_)
176 if (*end_str0++ == '\n')
182 return Slice (begin_str0 - data_str0, end_str0 - data_str0);
186 Source_file::line_string (char const* pos_str0) const
188 if (!contains (pos_str0))
191 Slice line = line_slice (pos_str0);
192 char const* data_str0 = to_str0 ();
193 return String ((Byte const*)data_str0 + line[LEFT], line.length ());
197 Source_file::get_char (char const* pos_str0) const
199 if (!contains (pos_str0))
202 char const* data_str0 = to_str0 ();
203 return pos_str0 - (line_slice (pos_str0)[SMALLER] + data_str0);
207 Source_file::get_column (char const* pos_str0) const
209 if (!contains (pos_str0))
212 int ch_i = get_char (pos_str0);
213 String line = line_string (pos_str0);
216 for (int i = 0; i < ch_i; i++)
218 col_i = (col_i / 8 + 1) * 8;
226 Source_file::error_string (char const* pos_str0) const
228 if (!contains (pos_str0))
229 return " (" + _ ("position unknown") + ")";
231 int ch_i = get_char (pos_str0);
232 String line = line_string (pos_str0);
233 String context = line.left_string (ch_i)
235 + to_string (' ', get_column (pos_str0))
236 + line.cut_string (ch_i, INT_MAX);
242 Source_file::contains (char const* pos_str0) const
244 return (pos_str0 && (pos_str0 >= to_str0 ()) && (pos_str0 <= to_str0 () + length ()));
248 Source_file::get_line (char const* pos_str0) const
250 if (!contains (pos_str0))
253 if (!newline_locations_.size ())
257 int hi = newline_locations_.size ();
259 if (newline_locations_[lo] > pos_str0)
262 if (newline_locations_[hi-1] < pos_str0)
265 binary_search_bounds (newline_locations_,
267 Link_array<char>::default_compare,
270 if (*pos_str0 == '\n')
276 Source_file::length () const
282 Source_file::to_str0 () const
284 return contents_str0_;
288 Source_file::set_pos (char const * pos_str0)
290 if (contains (pos_str0))
291 pos_str0_ = pos_str0;
293 error (error_string (pos_str0) + "invalid pos");
297 Source_file::seek_str0 (int n)
299 char const* new_str0 = to_str0 () + n;
301 new_str0 += length ();
302 if (contains (new_str0))
303 pos_str0_ = new_str0;
305 error (error_string (new_str0) + "seek past eof");
311 Source_file::forward_str0 (int n)
313 char const* old_pos = pos_str0_;
314 char const* new_str0 = pos_str0_ + n;
315 if (contains (new_str0))
316 pos_str0_ = new_str0;
318 error (error_string (new_str0) + "forward past eof");
324 Source_file::get_string (int n)
326 String str = String ((Byte const*)forward_str0 (n), n);