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 Array<char> chs; // ugh.
41 while ((c = fgetc (stdin)) != EOF)
45 length_ = chs.size ();
46 contents_str0_ = chs.remove_array ();
50 gulp_file (std::string filename, int *filesize)
52 /* "b" must ensure to open literally, avoiding text (CR/LF)
54 FILE *f = fopen (filename.c_str (), "rb");
57 warning (_f ("can't open file: `%s'", filename.c_str ()));
61 fseek (f, 0, SEEK_END);
62 int real_size = ftell (f);
63 int read_count = real_size;
66 read_count = min (read_count, *filesize);
70 char *str = new char[read_count + 1];
73 int bytes_read = fread (str, sizeof (char), read_count, f);
74 if (bytes_read != read_count)
75 warning (_f ("expected to read %d characters, got %d", bytes_read,
78 *filesize = bytes_read;
82 Source_file::Source_file (std::string filename, std::string data)
86 length_ = data.length ();
87 contents_str0_ = string_copy (data);
91 for (int i = 0; i < length_; i++)
92 if (contents_str0_[i] == '\n')
93 newline_locations_.push (contents_str0_ + i);
96 Source_file::Source_file (std::string filename_string)
98 name_ = filename_string;
102 if (filename_string == "-")
107 contents_str0_ = gulp_file (filename_string, &length_);
110 pos_str0_ = c_str ();
114 for (int i = 0; i < length_; i++)
115 if (contents_str0_[i] == '\n')
116 newline_locations_.push (contents_str0_ + i);
120 Source_file::init_port ()
122 SCM str = scm_makfrom0str (contents_str0_);
123 str_port_ = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG, __FUNCTION__);
124 scm_set_port_filename_x (str_port_, scm_makfrom0str (name_.c_str ()));
128 Source_file::tell () const
130 return pos_str0_ - contents_str0_;
134 Source_file::get_istream ()
138 if (length ()) // can-t this be done without such a hack?
139 istream_ = new istringstream (c_str ());
142 istream_ = new istringstream ("");
143 istream_->setstate (ios::eofbit);
144 // istream_->set (ios::eofbit);
151 Source_file::file_line_column_string (char const *context_str0) const
154 return " (" + _ ("position unknown") + ")";
158 get_counts (context_str0, &l, &ch, &col);
160 return name_string () + ":" + to_string (l)
161 + ":" + to_string (col);
166 Source_file::quote_input (char const *pos_str0) const
168 if (!contains (pos_str0))
169 return " (" + _ ("position unknown") + ")";
172 get_counts (pos_str0, &l, &ch, &col);
173 std::string line = line_string (pos_str0);
174 std::string context = line.substr (0, ch)
176 + to_string (' ', col)
182 Source_file::name_string () const
184 return map_file_name (name_);
187 Source_file::~Source_file ()
191 delete[] contents_str0_;
195 Source_file::line_slice (char const *pos_str0) const
197 if (!contains (pos_str0))
200 char const *data_str0 = c_str ();
201 char const *eof_C_ = data_str0 + length ();
203 if (pos_str0 == eof_C_)
205 char const *begin_str0 = pos_str0;
206 while (begin_str0 > data_str0)
207 if (*--begin_str0 == '\n')
213 char const *end_str0 = pos_str0;
214 while (end_str0 < eof_C_)
215 if (*end_str0++ == '\n')
221 return Slice (begin_str0 - data_str0, end_str0 - data_str0);
225 Source_file::line_string (char const *pos_str0) const
227 if (!contains (pos_str0))
230 Slice line = line_slice (pos_str0);
231 char const *data_str0 = c_str ();
232 return std::string (data_str0 + line[LEFT], line.length ());
236 Source_file::get_counts (char const *pos_str0,
245 if (!contains (pos_str0))
248 *line_number = get_line (pos_str0);
250 Slice line = line_slice (pos_str0);
251 char const *data = c_str ();
252 char const *line_start = (char const *)data + line[LEFT];
254 ssize left = (char const *) pos_str0 - line_start;
255 std::string line_begin (line_start, left);
256 char const *line_chars = line_begin.c_str ();
263 /* Initialize the state. */
264 memset (&state, '\0', sizeof (state));
268 wchar_t multibyte[2];
271 FIXME, this is apparently locale dependent.
274 size_t thislen = mbrtowc (multibyte, line_chars, left, &state);
277 #endif /* !HAVE_MBRTOWC */
279 /* Stop converting at invalid character;
280 this can mean we have read just the first part
281 of a valid character. */
282 if (thislen == (size_t) -1)
285 /* We want to handle embedded NUL bytes
286 but the return value is 0. Correct this. */
290 if (thislen == 1 && line_chars[0] == '\t')
291 (*column) = (*column / 8 + 1) * 8;
296 /* Advance past this character. */
297 line_chars += thislen;
303 Source_file::contains (char const *pos_str0) const
305 return (pos_str0 && (pos_str0 >= c_str ()) && (pos_str0 <= c_str () + length ()));
309 Source_file::get_line (char const *pos_str0) const
311 if (!contains (pos_str0))
314 if (!newline_locations_.size ())
318 int hi = newline_locations_.size ();
320 if (newline_locations_[lo] > pos_str0)
323 if (newline_locations_[hi - 1] < pos_str0)
326 binary_search_bounds (newline_locations_,
328 Link_array<char>::default_compare,
331 if (*pos_str0 == '\n')
337 Source_file::length () const
343 Source_file::c_str () const
345 return contents_str0_;
349 Source_file::set_pos (char const *pos_str0)
351 if (contains (pos_str0))
352 pos_str0_ = pos_str0;
354 error (quote_input (pos_str0) + "invalid pos");
358 Source_file::seek_str0 (int n)
360 char const *new_str0 = c_str () + n;
362 new_str0 += length ();
363 if (contains (new_str0))
364 pos_str0_ = new_str0;
366 error (quote_input (new_str0) + "seek past eof");
372 Source_file::forward_str0 (int n)
374 char const *old_pos = pos_str0_;
375 char const *new_str0 = pos_str0_ + n;
376 if (contains (new_str0))
377 pos_str0_ = new_str0;
379 error (quote_input (new_str0) + "forward past eof");
385 Source_file::get_string (int n)
387 std::string str = std::string ((char const *)forward_str0 (n), n);
392 Source_file::get_port () const