2 source-file.cc -- implement Source_file
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 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 ())
30 #include "file-name-map.hh"
31 #include "international.hh"
35 Source_file::load_stdin ()
40 while ((c = fgetc (stdin)) != EOF)
44 length_ = chs_.size ();
45 contents_str0_ = &chs_[0];
49 gulp_file (std::string filename, int *filesize)
51 /* "b" must ensure to open literally, avoiding text (CR/LF)
53 FILE *f = fopen (filename.c_str (), "rb");
56 warning (_f ("can't open file: `%s'", filename.c_str ()));
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 (std::string filename, std::string data)
85 length_ = data.length ();
86 contents_str0_ = string_copy (data);
90 for (int i = 0; i < length_; i++)
91 if (contents_str0_[i] == '\n')
92 newline_locations_.push_back (contents_str0_ + i);
95 Source_file::Source_file (std::string filename_string)
97 name_ = filename_string;
101 if (filename_string == "-")
106 contents_str0_ = gulp_file (filename_string, &length_);
109 pos_str0_ = c_str ();
113 for (int i = 0; i < length_; i++)
114 if (contents_str0_[i] == '\n')
115 newline_locations_.push_back (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_.c_str ()));
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 (c_str ());
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 std::string line = line_string (pos_str0);
173 std::string context = line.substr (0, ch)
175 + to_string (' ', col)
176 + line.substr (ch, line.length()-ch);
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 = c_str ();
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 = c_str ();
231 return std::string (data_str0 + line[LEFT], line.length ());
235 Source_file::get_counts (char const *pos_str0,
244 if (!contains (pos_str0))
247 *line_number = get_line (pos_str0);
249 Slice line = line_slice (pos_str0);
250 char const *data = c_str ();
251 char const *line_start = (char const *)data + line[LEFT];
253 ssize left = (char const *) pos_str0 - line_start;
254 std::string line_begin (line_start, left);
255 char const *line_chars = line_begin.c_str ();
262 /* Initialize the state. */
263 memset (&state, '\0', sizeof (state));
267 wchar_t multibyte[2];
270 FIXME, this is apparently locale dependent.
273 size_t thislen = mbrtowc (multibyte, line_chars, left, &state);
276 #endif /* !HAVE_MBRTOWC */
278 /* Stop converting at invalid character;
279 this can mean we have read just the first part
280 of a valid character. */
281 if (thislen == (size_t) -1)
284 /* We want to handle embedded NUL bytes
285 but the return value is 0. Correct this. */
289 if (thislen == 1 && line_chars[0] == '\t')
290 (*column) = (*column / 8 + 1) * 8;
295 /* Advance past this character. */
296 line_chars += thislen;
302 Source_file::contains (char const *pos_str0) const
304 return (pos_str0 && (pos_str0 >= c_str ()) && (pos_str0 <= c_str () + length ()));
308 Source_file::get_line (char const *pos_str0) const
310 if (!contains (pos_str0))
313 if (!newline_locations_.size ())
317 int hi = newline_locations_.size ();
319 if (newline_locations_[lo] > pos_str0)
322 if (newline_locations_[hi - 1] < pos_str0)
325 binary_search_bounds (newline_locations_,
330 if (*pos_str0 == '\n')
336 Source_file::length () const
342 Source_file::c_str () const
344 return contents_str0_;
348 Source_file::set_pos (char const *pos_str0)
350 if (contains (pos_str0))
351 pos_str0_ = pos_str0;
353 error (quote_input (pos_str0) + "invalid pos");
357 Source_file::seek_str0 (int n)
359 char const *new_str0 = c_str () + n;
361 new_str0 += length ();
362 if (contains (new_str0))
363 pos_str0_ = new_str0;
365 error (quote_input (new_str0) + "seek past eof");
371 Source_file::forward_str0 (int n)
373 char const *old_pos = pos_str0_;
374 char const *new_str0 = pos_str0_ + n;
375 if (contains (new_str0))
376 pos_str0_ = new_str0;
378 error (quote_input (new_str0) + "forward past eof");
384 Source_file::get_string (int n)
386 std::string str = std::string ((char const *)forward_str0 (n), n);
391 Source_file::get_port () const