2 source-file.cc -- implement Source_file
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 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 ()
39 while ((c = fgetc (stdin)) != EOF)
40 characters_.push_back (c);
44 return contents of FILENAME. *Not 0-terminated!*
47 gulp_file (string filename, int desired_size)
49 /* "b" must ensure to open literally, avoiding text (CR/LF)
51 FILE *f = fopen (filename.c_str (), "rb");
54 warning (_f ("cannot 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, desired_size);
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 int filesize = bytes_read;
80 cxx_arr.resize (filesize);
82 copy (str, str + filesize, cxx_arr.begin ());
98 Source_file::Source_file (string filename, string data)
104 characters_.resize (data.length ());
105 copy (data.begin (), data.end (), characters_.begin ());
107 characters_.push_back (0);
111 for (vsize i = 0; i < characters_.size (); i++)
112 if (characters_[i] == '\n')
113 newline_locations_.push_back (&characters_[0] + i);
116 Source_file::Source_file (string filename_string)
120 name_ = filename_string;
122 if (filename_string == "-")
126 characters_ = gulp_file (filename_string, -1);
129 characters_.push_back (0);
133 for (vsize i = 0; i < characters_.size (); i++)
134 if (characters_[i] == '\n')
135 newline_locations_.push_back (&characters_[0] + i);
139 Source_file::init_port ()
141 SCM str = scm_from_locale_string (c_str ());
142 str_port_ = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG, __FUNCTION__);
143 scm_set_port_filename_x (str_port_, ly_string2scm (name_));
148 Source_file::get_istream ()
152 if (length ()) // can-t this be done without such a hack?
153 istream_ = new istringstream (c_str ());
156 istream_ = new istringstream ("");
157 istream_->setstate (ios::eofbit);
158 // istream_->set (ios::eofbit);
165 Source_file::file_line_column_string (char const *context_str0) const
168 return " (" + _ ("position unknown") + ")";
172 get_counts (context_str0, &l, &ch, &col);
174 return name_string () + ":" + to_string (l)
175 + ":" + to_string (col);
180 Source_file::quote_input (char const *pos_str0) const
182 if (!contains (pos_str0))
183 return " (" + _ ("position unknown") + ")";
186 get_counts (pos_str0, &l, &ch, &col);
187 string line = line_string (pos_str0);
188 string context = line.substr (0, ch)
190 + to_string (' ', col)
191 + line.substr (ch, line.length ()-ch);
196 Source_file::name_string () const
198 return map_file_name (name_);
201 Source_file::~Source_file ()
207 Source_file::line_slice (char const *pos_str0) const
209 if (!contains (pos_str0))
212 char const *data_str0 = c_str ();
213 char const *eof_C_ = data_str0 + length ();
215 if (pos_str0 == eof_C_)
217 char const *begin_str0 = pos_str0;
218 while (begin_str0 > data_str0)
219 if (*--begin_str0 == '\n')
225 char const *end_str0 = pos_str0;
226 while (end_str0 < eof_C_)
227 if (*end_str0++ == '\n')
233 return Slice (begin_str0 - data_str0, end_str0 - data_str0);
237 Source_file::line_string (char const *pos_str0) const
239 if (!contains (pos_str0))
242 Slice line = line_slice (pos_str0);
243 char const *data_str0 = c_str ();
244 return string (data_str0 + line[LEFT], line.length ());
248 Source_file::get_counts (char const *pos_str0,
257 if (!contains (pos_str0))
260 *line_number = get_line (pos_str0);
262 Slice line = line_slice (pos_str0);
263 char const *data = c_str ();
264 char const *line_start = (char const *)data + line[LEFT];
266 ssize left = (char const *) pos_str0 - line_start;
267 string line_begin (line_start, left);
268 char const *line_chars = line_begin.c_str ();
275 /* Initialize the state. */
276 memset (&state, '\0', sizeof (state));
281 FIXME, this is apparently locale dependent.
284 wchar_t multibyte[2];
285 size_t thislen = mbrtowc (multibyte, line_chars, left, &state);
288 #endif /* !HAVE_MBRTOWC */
290 /* Stop converting at invalid character;
291 this can mean we have read just the first part
292 of a valid character. */
293 if (thislen == (size_t) -1)
296 /* We want to handle embedded NUL bytes
297 but the return value is 0. Correct this. */
301 if (thislen == 1 && line_chars[0] == '\t')
302 (*column) = (*column / 8 + 1) * 8;
307 /* Advance past this character. */
308 line_chars += thislen;
314 Source_file::contains (char const *pos_str0) const
316 return (pos_str0 && (pos_str0 >= c_str ()) && (pos_str0 <= c_str () + length ()));
320 Source_file::get_line (char const *pos_str0) const
322 if (!contains (pos_str0))
325 if (!newline_locations_.size ())
328 /* this will find the '\n' character at the end of our line */
329 vsize lo = lower_bound (newline_locations_,
331 less<char const*> ());
333 /* the return value will be indexed from 1 */
334 return lo + 1 + line_offset_;
338 Source_file::set_line (char const *pos_str0, int line)
340 int current_line = get_line (pos_str0);
341 line_offset_ += line - current_line;
343 assert (line == get_line (pos_str0));
347 Source_file::length () const
349 return characters_.size ();
353 Source_file::c_str () const
355 return &characters_[0];
359 Source_file::get_port () const
364 /****************************************************************/
366 #include "ly-smobs.icc"
368 IMPLEMENT_SMOBS (Source_file);
369 IMPLEMENT_DEFAULT_EQUAL_P (Source_file);
370 IMPLEMENT_TYPE_P (Source_file, "ly:source-file?");
373 Source_file::mark_smob (SCM smob)
375 Source_file *sc = (Source_file *) SCM_CELL_WORD_1 (smob);
377 return sc->str_port_;
382 Source_file::print_smob (SCM smob, SCM port, scm_print_state *)
384 Source_file *sc = (Source_file *) SCM_CELL_WORD_1 (smob);
386 scm_puts ("#<Source_file ", port);
387 scm_puts (sc->name_.c_str (), port);
389 /* Do not print properties, that is too much hassle. */
390 scm_puts (" >", port);