2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 Jan Nieuwenhuizen <janneke@gnu.org>
5 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
22 #define _GLIBCXX_HAVE_MBSTATE_T
24 #endif /* GCC_MAJOR < 4 */
26 #include "source-file.hh"
36 #define istringstream(x) istrstream (x, length ())
40 #include "file-name-map.hh"
41 #include "international.hh"
46 Source_file::load_stdin ()
50 while ((c = fgetc (stdin)) != EOF)
51 characters_.push_back ((char)c);
55 return contents of FILENAME. *Not 0-terminated!*
58 gulp_file (const string &filename, int desired_size)
60 /* "b" must ensure to open literally, avoiding text (CR/LF)
62 FILE *f = fopen (filename.c_str (), "rb");
65 warning (_f ("cannot open file: `%s'", filename.c_str ()));
71 fseek (f, 0, SEEK_END);
72 int real_size = ftell (f);
73 int read_count = real_size;
76 read_count = min (read_count, desired_size);
80 char *str = new char[read_count + 1];
83 int bytes_read = fread (str, sizeof (char), read_count, f);
84 if (bytes_read != read_count)
85 warning (_f ("expected to read %d characters, got %d", bytes_read,
88 int filesize = bytes_read;
91 cxx_arr.resize (filesize);
93 copy (str, str + filesize, cxx_arr.begin ());
108 Source_file::Source_file (const string &filename, const string &data)
114 characters_.resize (data.length ());
115 copy (data.begin (), data.end (), characters_.begin ());
117 characters_.push_back (0);
121 for (vsize i = 0; i < characters_.size (); i++)
122 if (characters_[i] == '\n')
123 newline_locations_.push_back (&characters_[0] + i);
126 Source_file::Source_file (const string &filename_string)
130 name_ = filename_string;
132 if (filename_string == "-")
136 characters_ = gulp_file (filename_string, -1);
139 characters_.push_back (0);
143 for (vsize i = 0; i < characters_.size (); i++)
144 if (characters_[i] == '\n')
145 newline_locations_.push_back (&characters_[0] + i);
149 Source_file::init_port ()
151 // This is somewhat icky: the string will in general be in utf8, but
152 // we do our own utf8 encoding and verification in the parser, so we
153 // use the no-conversion equivalent of latin1
154 SCM str = scm_from_latin1_string (c_str ());
155 str_port_ = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG, __FUNCTION__);
156 scm_set_port_filename_x (str_port_, ly_string2scm (name_));
160 Source_file::get_istream ()
164 if (length ()) // can-t this be done without such a hack?
165 istream_ = new istringstream (c_str ());
168 istream_ = new istringstream ("");
169 istream_->setstate (ios::eofbit);
170 // istream_->set (ios::eofbit);
177 Source_file::file_line_column_string (char const *context_str0) const
180 return " (" + _ ("position unknown") + ")";
183 int l, ch, col, offset;
184 get_counts (context_str0, &l, &ch, &col, &offset);
186 return name_string () + ":" + ::to_string (l)
187 + ":" + ::to_string (col + 1);
192 Source_file::quote_input (char const *pos_str0) const
194 if (!contains (pos_str0))
195 return " (" + _ ("position unknown") + ")";
197 int l, ch, col, offset;
198 get_counts (pos_str0, &l, &ch, &col, &offset);
199 string line = line_string (pos_str0);
200 string context = line.substr (0, offset)
202 + ::to_string (' ', col)
203 + line.substr (offset, line.length () - offset);
208 Source_file::name_string () const
210 return map_file_name (name_);
213 Source_file::~Source_file ()
219 Source_file::line_slice (char const *pos_str0) const
221 if (!contains (pos_str0))
224 char const *data_str0 = c_str ();
225 char const *eof_C_ = data_str0 + length ();
227 if (pos_str0 == eof_C_)
229 char const *begin_str0 = pos_str0;
230 while (begin_str0 > data_str0)
231 if (*--begin_str0 == '\n')
237 char const *end_str0 = pos_str0;
238 while (end_str0 < eof_C_)
239 if (*end_str0++ == '\n')
245 return Slice (begin_str0 - data_str0, end_str0 - data_str0);
249 Source_file::line_string (char const *pos_str0) const
251 if (!contains (pos_str0))
254 Slice line = line_slice (pos_str0);
255 char const *data_str0 = c_str ();
256 return string (data_str0 + line[LEFT], line.length ());
260 Source_file::get_counts (char const *pos_str0,
264 int *byte_offset) const
266 // Initialize arguments to defaults, needed if pos_str0 is not in source
272 if (!contains (pos_str0))
275 *line_number = get_line (pos_str0);
277 Slice line = line_slice (pos_str0);
278 char const *data = c_str ();
279 char const *line_start = (char const *)data + line[LEFT];
281 ssize left = (char const *) pos_str0 - line_start;
284 string line_begin (line_start, left);
285 char const *line_chars = line_begin.c_str ();
287 for (; left > 0; --left, ++line_chars)
289 // Skip UTF-8 continuation bytes. This is simplistic but
290 // robust, and we warn against non-UTF-8 input in the lexer
291 // already. In the case of non-UTF-8 or of this function being
292 // called in mid-character, the results are somewhat arbitrary,
293 // but there is no really sane definition anyway.
294 if ((*line_chars & 0xc0) == 0x80)
297 if (*line_chars == '\t')
298 (*column) = (*column / 8 + 1) * 8;
307 Source_file::contains (char const *pos_str0) const
309 return (pos_str0 && (pos_str0 >= c_str ()) && (pos_str0 <= c_str () + length ()));
313 Source_file::get_line (char const *pos_str0) const
315 if (!contains (pos_str0))
318 if (!newline_locations_.size ())
319 return 1 + line_offset_;
321 /* this will find the '\n' character at the end of our line */
322 vsize lo = lower_bound (newline_locations_,
324 less<char const *> ());
326 /* the return value will be indexed from 1 */
327 return lo + 1 + line_offset_;
331 Source_file::set_line (char const *pos_str0, int line)
335 int current_line = get_line (pos_str0);
336 line_offset_ += line - current_line;
338 assert (line == get_line (pos_str0));
345 Source_file::length () const
347 return characters_.size ();
351 Source_file::c_str () const
353 return &characters_[0];
357 Source_file::get_port () const
362 /****************************************************************/
365 const char Source_file::type_p_name_[] = "ly:source-file?";
368 Source_file::mark_smob () const
374 Source_file::print_smob (SCM port, scm_print_state *) const
376 scm_puts ("#<Source_file ", port);
377 scm_puts (name_.c_str (), port);
379 /* Do not print properties, that is too much hassle. */
380 scm_puts (" >", port);