Use scalar instead of embedded_scm for context mod overrides.
[lilypond/mpolesky.git] / lily / source-file.cc
blob7687b4749a4ea12e941e675472ded1e07a386b3a
1 /*
2 source-file.cc -- implement Source_file
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2009 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
10 #if GCC_MAJOR < 4
11 #define _GLIBCXX_HAVE_MBSTATE_T
12 #include <wchar.h>
13 #endif /* GCC_MAJOR < 4 */
15 #include "source-file.hh"
17 #include "config.hh"
19 #include <cstdio>
21 #if HAVE_SSTREAM
22 #include <sstream>
23 #else
24 #include <strstream>
25 #define istringstream(x) istrstream (x, length ())
26 #endif
27 using namespace std;
29 #include "file-name-map.hh"
30 #include "international.hh"
31 #include "misc.hh"
32 #include "warn.hh"
34 void
35 Source_file::load_stdin ()
37 characters_.clear ();
38 int c;
39 while ((c = fgetc (stdin)) != EOF)
40 characters_.push_back (c);
44 return contents of FILENAME. *Not 0-terminated!*
46 vector<char>
47 gulp_file (string filename, int desired_size)
49 /* "b" must ensure to open literally, avoiding text (CR/LF)
50 conversions. */
51 FILE *f = fopen (filename.c_str (), "rb");
52 if (!f)
54 warning (_f ("cannot open file: `%s'", filename.c_str ()));
56 vector<char> cxx_arr;
57 return cxx_arr;
60 fseek (f, 0, SEEK_END);
61 int real_size = ftell (f);
62 int read_count = real_size;
64 if (desired_size > 0)
65 read_count = min (read_count, desired_size);
67 rewind (f);
69 char *str = new char[read_count + 1];
70 str[read_count] = 0;
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,
75 read_count));
76 fclose (f);
77 int filesize = bytes_read;
79 vector<char> cxx_arr;
80 cxx_arr.resize (filesize);
82 copy (str, str + filesize, cxx_arr.begin ());
84 delete[] str;
85 return cxx_arr;
88 void
89 Source_file::init ()
91 istream_ = 0;
92 line_offset_ = 0;
93 str_port_ = SCM_EOL;
94 self_scm_ = SCM_EOL;
95 smobify_self ();
98 Source_file::Source_file (string filename, string data)
100 init ();
102 name_ = filename;
104 characters_.resize (data.length ());
105 copy (data.begin (), data.end (), characters_.begin ());
107 characters_.push_back (0);
109 init_port ();
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)
118 init ();
120 name_ = filename_string;
122 if (filename_string == "-")
123 load_stdin ();
124 else
126 characters_ = gulp_file (filename_string, -1);
129 characters_.push_back (0);
131 init_port ();
133 for (vsize i = 0; i < characters_.size (); i++)
134 if (characters_[i] == '\n')
135 newline_locations_.push_back (&characters_[0] + i);
138 void
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_));
147 istream *
148 Source_file::get_istream ()
150 if (!istream_)
152 if (length ()) // can-t this be done without such a hack?
153 istream_ = new istringstream (c_str ());
154 else
156 istream_ = new istringstream ("");
157 istream_->setstate (ios::eofbit);
158 // istream_->set (ios::eofbit);
161 return istream_;
164 string
165 Source_file::file_line_column_string (char const *context_str0) const
167 if (!c_str ())
168 return " (" + _ ("position unknown") + ")";
169 else
171 int l, ch, col, offset;
172 get_counts (context_str0, &l, &ch, &col, &offset);
174 return name_string () + ":" + to_string (l)
175 + ":" + to_string (col);
179 string
180 Source_file::quote_input (char const *pos_str0) const
182 if (!contains (pos_str0))
183 return " (" + _ ("position unknown") + ")";
185 int l, ch, col, offset;
186 get_counts (pos_str0, &l, &ch, &col, &offset);
187 string line = line_string (pos_str0);
188 string context = line.substr (0, offset)
189 + to_string ('\n')
190 + to_string (' ', col)
191 + line.substr (offset, line.length () - offset);
192 return context;
195 string
196 Source_file::name_string () const
198 return map_file_name (name_);
201 Source_file::~Source_file ()
203 delete istream_;
206 Slice
207 Source_file::line_slice (char const *pos_str0) const
209 if (!contains (pos_str0))
210 return Slice (0, 0);
212 char const *data_str0 = c_str ();
213 char const *eof_C_ = data_str0 + length ();
215 if (pos_str0 == eof_C_)
216 pos_str0--;
217 char const *begin_str0 = pos_str0;
218 while (begin_str0 > data_str0)
219 if (*--begin_str0 == '\n')
221 begin_str0++;
222 break;
225 char const *end_str0 = pos_str0;
226 while (end_str0 < eof_C_)
227 if (*end_str0++ == '\n')
229 end_str0--;
230 break;
233 return Slice (begin_str0 - data_str0, end_str0 - data_str0);
236 string
237 Source_file::line_string (char const *pos_str0) const
239 if (!contains (pos_str0))
240 return "";
242 Slice line = line_slice (pos_str0);
243 char const *data_str0 = c_str ();
244 return string (data_str0 + line[LEFT], line.length ());
247 void
248 Source_file::get_counts (char const *pos_str0,
249 int *line_number,
250 int *line_char,
251 int *column,
252 int *byte_offset) const
254 *line_number = 0;
256 if (!contains (pos_str0))
257 return;
259 *line_number = get_line (pos_str0);
261 Slice line = line_slice (pos_str0);
262 char const *data = c_str ();
263 char const *line_start = (char const *)data + line[LEFT];
265 ssize left = (char const *) pos_str0 - line_start;
266 string line_begin (line_start, left);
267 char const *line_chars = line_begin.c_str ();
269 *line_char = 0;
270 *column = 0;
271 *byte_offset = 0;
273 while (left > 0)
275 size_t thislen = utf8_char_len (*line_chars);
277 if (thislen == 1 && line_chars[0] == '\t')
278 (*column) = (*column / 8 + 1) * 8;
279 else
280 (*column)++;
282 (*line_char)++;
285 To have decent output in UTF-8 aware terminals,
286 we must keep track of the number of bytes from
287 the left edge of the terminal.
289 *byte_offset += thislen;
291 /* Advance past this character. */
292 line_chars += thislen;
293 left -= thislen;
297 bool
298 Source_file::contains (char const *pos_str0) const
300 return (pos_str0 && (pos_str0 >= c_str ()) && (pos_str0 <= c_str () + length ()));
304 Source_file::get_line (char const *pos_str0) const
306 if (!contains (pos_str0))
307 return 0;
309 if (!newline_locations_.size ())
310 return 1;
312 /* this will find the '\n' character at the end of our line */
313 vsize lo = lower_bound (newline_locations_,
314 pos_str0,
315 less<char const*> ());
317 /* the return value will be indexed from 1 */
318 return lo + 1 + line_offset_;
321 void
322 Source_file::set_line (char const *pos_str0, int line)
324 int current_line = get_line (pos_str0);
325 line_offset_ += line - current_line;
327 assert (line == get_line (pos_str0));
331 Source_file::length () const
333 return characters_.size ();
336 char const *
337 Source_file::c_str () const
339 return &characters_[0];
343 Source_file::get_port () const
345 return str_port_;
348 /****************************************************************/
350 #include "ly-smobs.icc"
352 IMPLEMENT_SMOBS (Source_file);
353 IMPLEMENT_DEFAULT_EQUAL_P (Source_file);
354 IMPLEMENT_TYPE_P (Source_file, "ly:source-file?");
357 Source_file::mark_smob (SCM smob)
359 Source_file *sc = (Source_file *) SCM_CELL_WORD_1 (smob);
361 return sc->str_port_;
366 Source_file::print_smob (SCM smob, SCM port, scm_print_state *)
368 Source_file *sc = (Source_file *) SCM_CELL_WORD_1 (smob);
370 scm_puts ("#<Source_file ", port);
371 scm_puts (sc->name_.c_str (), port);
373 /* Do not print properties, that is too much hassle. */
374 scm_puts (" >", port);
375 return 1;