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@cs.uu.nl>
10 #include "source-file.hh"
15 #include <utf8/wchar.h> /* mbrtowc */
17 #include <wchar.h> /* mbrtowc */
26 #define istringstream(x) istrstream (x, length ())
30 #include "file-name-map.hh"
33 Source_file::load_stdin ()
38 Array<char> chs; // ugh.
39 while ((c = fgetc (stdin)) != EOF)
43 length_ = chs.size ();
44 contents_str0_ = chs.remove_array ();
48 gulp_file (String filename, int *filesize)
50 /* "b" must ensure to open literally, avoiding text (CR/LF)
52 FILE *f = fopen (filename.to_str0 (), "rb");
55 warning (_f ("can't open file: `%s'", filename.to_str0 ()));
59 fseek (f, 0, SEEK_END);
60 int real_size = ftell (f);
61 int read_count = real_size;
64 read_count = min (read_count, *filesize);
68 char *str = new char[read_count + 1];
71 int bytes_read = fread (str, sizeof (char), read_count, f);
72 if (bytes_read != read_count)
73 warning (_f ("expected to read %d characters, got %d", bytes_read,
80 Source_file::Source_file (String filename, String data)
84 contents_str0_ = data.get_copy_str0 ();
85 length_ = data.length ();
86 pos_str0_ = to_str0 ();
89 for (int i = 0; i < length_; i++)
90 if (contents_str0_[i] == '\n')
91 newline_locations_.push (contents_str0_ + i);
94 Source_file::Source_file (String filename_string)
96 name_ = filename_string;
100 if (filename_string == "-")
105 contents_str0_ = gulp_file (filename_string, &length_);
108 pos_str0_ = to_str0 ();
112 for (int i = 0; i < length_; i++)
113 if (contents_str0_[i] == '\n')
114 newline_locations_.push (contents_str0_ + i);
118 Source_file::init_port ()
120 SCM str = scm_makfrom0str (contents_str0_);
121 str_port_ = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG, __FUNCTION__);
122 scm_set_port_filename_x (str_port_, scm_makfrom0str (name_.get_str0 ()));
126 Source_file::tell () const
128 return pos_str0_ - contents_str0_;
132 Source_file::get_istream ()
136 if (length ()) // can-t this be done without such a hack?
137 istream_ = new std::istringstream (to_str0 ());
140 istream_ = new std::istringstream ("");
141 istream_->setstate (std::ios::eofbit);
142 // istream_->set (ios::eofbit);
149 Source_file::file_line_column_string (char const *context_str0) const
152 return " (" + _ ("position unknown") + ")";
156 get_counts (context_str0, &l, &ch, &col);
158 return name_string () + ":" + to_string (l)
159 + ":" + to_string (col);
164 Source_file::quote_input (char const *pos_str0) const
166 if (!contains (pos_str0))
167 return " (" + _ ("position unknown") + ")";
170 get_counts (pos_str0, &l, &ch, &col);
171 String line = line_string (pos_str0);
172 String context = line.left_string (ch)
174 + to_string (' ', col)
175 + line.cut_string (ch, INT_MAX);
180 Source_file::name_string () const
182 return map_file_name (name_);
185 Source_file::~Source_file ()
189 delete[] contents_str0_;
193 Source_file::line_slice (char const *pos_str0) const
195 if (!contains (pos_str0))
198 char const *data_str0 = to_str0 ();
199 char const *eof_C_ = data_str0 + length ();
201 if (pos_str0 == eof_C_)
203 char const *begin_str0 = pos_str0;
204 while (begin_str0 > data_str0)
205 if (*--begin_str0 == '\n')
211 char const *end_str0 = pos_str0;
212 while (end_str0 < eof_C_)
213 if (*end_str0++ == '\n')
219 return Slice (begin_str0 - data_str0, end_str0 - data_str0);
223 Source_file::line_string (char const *pos_str0) const
225 if (!contains (pos_str0))
228 Slice line = line_slice (pos_str0);
229 char const *data_str0 = to_str0 ();
230 return String ((Byte const *)data_str0 + line[LEFT], line.length ());
234 Source_file::get_counts (char const *pos_str0,
239 if (!contains (pos_str0))
242 *line_number = get_line (pos_str0);
244 Slice line = line_slice (pos_str0);
245 char const *data = to_str0 ();
246 Byte const *line_start = (Byte const *)data + line[LEFT];
248 int left = (Byte const *) pos_str0 - line_start;
249 String line_begin (line_start, left);
250 char const *line_chars = line_begin.to_str0 ();
257 /* Initialize the state. */
258 memset (&state, '\0', sizeof (state));
262 wchar_t multibyte[2];
265 FIXME, this is apparently locale dependent.
267 size_t thislen = mbrtowc (multibyte, line_chars, left, &state);
269 /* Stop converting at invalid character;
270 this can mean we have read just the first part
271 of a valid character. */
272 if (thislen == (size_t) -1)
275 /* We want to handle embedded NUL bytes
276 but the return value is 0. Correct this. */
280 if (thislen == 1 && line_chars[0] == '\t')
281 (*column) = (*column / 8 + 1) * 8;
286 /* Advance past this character. */
287 line_chars += thislen;
293 Source_file::contains (char const *pos_str0) const
295 return (pos_str0 && (pos_str0 >= to_str0 ()) && (pos_str0 <= to_str0 () + length ()));
299 Source_file::get_line (char const *pos_str0) const
301 if (!contains (pos_str0))
304 if (!newline_locations_.size ())
308 int hi = newline_locations_.size ();
310 if (newline_locations_[lo] > pos_str0)
313 if (newline_locations_[hi - 1] < pos_str0)
316 binary_search_bounds (newline_locations_,
318 Link_array<char>::default_compare,
321 if (*pos_str0 == '\n')
327 Source_file::length () const
333 Source_file::to_str0 () const
335 return contents_str0_;
339 Source_file::set_pos (char const *pos_str0)
341 if (contains (pos_str0))
342 pos_str0_ = pos_str0;
344 error (quote_input (pos_str0) + "invalid pos");
348 Source_file::seek_str0 (int n)
350 char const *new_str0 = to_str0 () + n;
352 new_str0 += length ();
353 if (contains (new_str0))
354 pos_str0_ = new_str0;
356 error (quote_input (new_str0) + "seek past eof");
362 Source_file::forward_str0 (int n)
364 char const *old_pos = pos_str0_;
365 char const *new_str0 = pos_str0_ + n;
366 if (contains (new_str0))
367 pos_str0_ = new_str0;
369 error (quote_input (new_str0) + "forward past eof");
375 Source_file::get_string (int n)
377 String str = String ((Byte const *)forward_str0 (n), n);
382 Source_file::get_port () const