2 source-file.cc -- implement Source_file
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "source-file.hh"
15 #include <utf8/wchar.h> /* mbrtowc */
17 #include <cwchar> /* mbrtowc */
26 #define istringstream(x) istrstream (x, length ())
31 #include "file-name-map.hh"
34 Source_file::load_stdin ()
39 Array<char> chs; // ugh.
40 while ((c = fgetc (stdin)) != EOF)
44 length_ = chs.size ();
45 contents_str0_ = chs.remove_array ();
49 gulp_file (String filename, int *filesize)
51 /* "b" must ensure to open literally, avoiding text (CR/LF)
53 FILE *f = fopen (filename.to_str0 (), "rb");
56 warning (_f ("can't open file: `%s'", filename.to_str0 ()));
60 fseek (f, 0, SEEK_END);
61 int real_size = ftell (f);
62 int read_count = real_size;
65 read_count = min (read_count, *filesize);
69 char *str = new char[read_count + 1];
72 int bytes_read = fread (str, sizeof (char), read_count, f);
73 if (bytes_read != read_count)
74 warning (_f ("expected to read %d characters, got %d", bytes_read,
77 *filesize = bytes_read;
81 Source_file::Source_file (String filename, String data)
85 contents_str0_ = data.get_copy_str0 ();
86 length_ = data.length ();
87 pos_str0_ = to_str0 ();
90 for (int i = 0; i < length_; i++)
91 if (contents_str0_[i] == '\n')
92 newline_locations_.push (contents_str0_ + i);
95 Source_file::Source_file (String filename_string)
97 name_ = filename_string;
101 if (filename_string == "-")
106 contents_str0_ = gulp_file (filename_string, &length_);
109 pos_str0_ = to_str0 ();
113 for (int i = 0; i < length_; i++)
114 if (contents_str0_[i] == '\n')
115 newline_locations_.push (contents_str0_ + i);
119 Source_file::init_port ()
121 SCM str = scm_makfrom0str (contents_str0_);
122 str_port_ = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG, __FUNCTION__);
123 scm_set_port_filename_x (str_port_, scm_makfrom0str (name_.get_str0 ()));
127 Source_file::tell () const
129 return pos_str0_ - contents_str0_;
133 Source_file::get_istream ()
137 if (length ()) // can-t this be done without such a hack?
138 istream_ = new istringstream (to_str0 ());
141 istream_ = new istringstream ("");
142 istream_->setstate (ios::eofbit);
143 // istream_->set (ios::eofbit);
150 Source_file::file_line_column_string (char const *context_str0) const
153 return " (" + _ ("position unknown") + ")";
157 get_counts (context_str0, &l, &ch, &col);
159 return name_string () + ":" + to_string (l)
160 + ":" + to_string (col);
165 Source_file::quote_input (char const *pos_str0) const
167 if (!contains (pos_str0))
168 return " (" + _ ("position unknown") + ")";
171 get_counts (pos_str0, &l, &ch, &col);
172 String line = line_string (pos_str0);
173 String context = line.left_string (ch)
175 + to_string (' ', col)
176 + line.cut_string (ch, INT_MAX);
181 Source_file::name_string () const
183 return map_file_name (name_);
186 Source_file::~Source_file ()
190 delete[] contents_str0_;
194 Source_file::line_slice (char const *pos_str0) const
196 if (!contains (pos_str0))
199 char const *data_str0 = to_str0 ();
200 char const *eof_C_ = data_str0 + length ();
202 if (pos_str0 == eof_C_)
204 char const *begin_str0 = pos_str0;
205 while (begin_str0 > data_str0)
206 if (*--begin_str0 == '\n')
212 char const *end_str0 = pos_str0;
213 while (end_str0 < eof_C_)
214 if (*end_str0++ == '\n')
220 return Slice (begin_str0 - data_str0, end_str0 - data_str0);
224 Source_file::line_string (char const *pos_str0) const
226 if (!contains (pos_str0))
229 Slice line = line_slice (pos_str0);
230 char const *data_str0 = to_str0 ();
231 return String ((Byte const *)data_str0 + line[LEFT], line.length ());
235 Source_file::get_counts (char const *pos_str0,
240 if (!contains (pos_str0))
243 *line_number = get_line (pos_str0);
245 Slice line = line_slice (pos_str0);
246 char const *data = to_str0 ();
247 Byte const *line_start = (Byte const *)data + line[LEFT];
249 int left = (Byte const *) pos_str0 - line_start;
250 String line_begin (line_start, left);
251 char const *line_chars = line_begin.to_str0 ();
258 /* Initialize the state. */
259 memset (&state, '\0', sizeof (state));
263 wchar_t multibyte[2];
266 FIXME, this is apparently locale dependent.
268 size_t thislen = mbrtowc (multibyte, line_chars, left, &state);
270 /* Stop converting at invalid character;
271 this can mean we have read just the first part
272 of a valid character. */
273 if (thislen == (size_t) -1)
276 /* We want to handle embedded NUL bytes
277 but the return value is 0. Correct this. */
281 if (thislen == 1 && line_chars[0] == '\t')
282 (*column) = (*column / 8 + 1) * 8;
287 /* Advance past this character. */
288 line_chars += thislen;
294 Source_file::contains (char const *pos_str0) const
296 return (pos_str0 && (pos_str0 >= to_str0 ()) && (pos_str0 <= to_str0 () + length ()));
300 Source_file::get_line (char const *pos_str0) const
302 if (!contains (pos_str0))
305 if (!newline_locations_.size ())
309 int hi = newline_locations_.size ();
311 if (newline_locations_[lo] > pos_str0)
314 if (newline_locations_[hi - 1] < pos_str0)
317 binary_search_bounds (newline_locations_,
319 Link_array<char>::default_compare,
322 if (*pos_str0 == '\n')
328 Source_file::length () const
334 Source_file::to_str0 () const
336 return contents_str0_;
340 Source_file::set_pos (char const *pos_str0)
342 if (contains (pos_str0))
343 pos_str0_ = pos_str0;
345 error (quote_input (pos_str0) + "invalid pos");
349 Source_file::seek_str0 (int n)
351 char const *new_str0 = to_str0 () + n;
353 new_str0 += length ();
354 if (contains (new_str0))
355 pos_str0_ = new_str0;
357 error (quote_input (new_str0) + "seek past eof");
363 Source_file::forward_str0 (int n)
365 char const *old_pos = pos_str0_;
366 char const *new_str0 = pos_str0_ + n;
367 if (contains (new_str0))
368 pos_str0_ = new_str0;
370 error (quote_input (new_str0) + "forward past eof");
376 Source_file::get_string (int n)
378 String str = String ((Byte const *)forward_str0 (n), n);
383 Source_file::get_port () const