]> git.donarmstrong.com Git - lilypond.git/blob - lily/source-file.cc
* lily/lexer.ll: Add \sourcefileline command
[lilypond.git] / lily / source-file.cc
1 /*
2   source-file.cc -- implement Source_file
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2006 Jan Nieuwenhuizen <janneke@gnu.org>
7   Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
9
10 #include "source-file.hh"
11
12 #include "config.hh"
13
14 #if HAVE_UTF8_WCHAR_H
15 #include <utf8/wchar.h>  /* mbrtowc */
16 #else
17 #include <cwchar> /* mbrtowc */
18 #endif
19
20 #include <cstdio>
21
22 #if HAVE_SSTREAM
23 #include <sstream>
24 #else
25 #include <strstream>
26 #define istringstream(x) istrstream (x, length ())
27 #endif
28 using namespace std;
29
30 #include "file-name-map.hh"
31 #include "international.hh"
32 #include "warn.hh"
33
34 void
35 Source_file::load_stdin ()
36 {
37   length_ = 0;
38   chs_.clear ();
39   int c;
40   while ((c = fgetc (stdin)) != EOF)
41     chs_.push_back (c);
42
43   chs_.push_back (0);
44   length_ = chs_.size ();
45   contents_str0_ = &chs_[0];
46 }
47
48 char *
49 gulp_file (string filename, int *filesize)
50 {
51   /* "b" must ensure to open literally, avoiding text (CR/LF)
52      conversions.  */
53   FILE *f = fopen (filename.c_str (), "rb");
54   if (!f)
55     {
56       warning (_f ("can't open file: `%s'", filename.c_str ()));
57       return 0;
58     }
59
60   fseek (f, 0, SEEK_END);
61   int real_size = ftell (f);
62   int read_count = real_size;
63
64   if (*filesize >= 0)
65     read_count = min (read_count, *filesize);
66   
67   rewind (f);
68
69   char *str = new char[read_count + 1];
70   str[read_count] = 0;
71
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   *filesize = bytes_read;
78   return str;
79 }
80
81 Source_file::Source_file (string filename, string data)
82 {
83   name_ = filename;
84   istream_ = 0;
85   line_offset_ = 0;
86   length_ = data.length ();
87   contents_str0_ = string_copy (data);
88   pos_str0_ = c_str ();
89   init_port ();
90
91   for (int i = 0; i < length_; i++)
92     if (contents_str0_[i] == '\n')
93       newline_locations_.push_back (contents_str0_ + i);
94 }
95
96 Source_file::Source_file (string filename_string)
97 {
98   name_ = filename_string;
99   istream_ = 0;
100   line_offset_ = 0;
101   contents_str0_ = 0;
102
103   if (filename_string == "-")
104     load_stdin ();
105   else
106     {
107       length_ = -1;
108       contents_str0_ = gulp_file (filename_string, &length_);
109     }
110   
111   pos_str0_ = c_str ();
112
113   init_port ();
114
115   for (int i = 0; i < length_; i++)
116     if (contents_str0_[i] == '\n')
117       newline_locations_.push_back (contents_str0_ + i);
118 }
119
120 void
121 Source_file::init_port ()
122 {
123   SCM str = scm_makfrom0str (contents_str0_);
124   str_port_ = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG, __FUNCTION__);
125   scm_set_port_filename_x (str_port_, scm_makfrom0str (name_.c_str ()));
126 }
127
128 int
129 Source_file::tell () const
130 {
131   return pos_str0_ - contents_str0_;
132 }
133
134 istream *
135 Source_file::get_istream ()
136 {
137   if (!istream_)
138     {
139       if (length ()) // can-t this be done without such a hack?
140         istream_ = new istringstream (c_str ());
141       else
142         {
143           istream_ = new istringstream ("");
144           istream_->setstate (ios::eofbit);
145           //      istream_->set (ios::eofbit);
146         }
147     }
148   return istream_;
149 }
150
151 string
152 Source_file::file_line_column_string (char const *context_str0) const
153 {
154   if (!c_str ())
155     return " (" + _ ("position unknown") + ")";
156   else
157     {
158       int l, ch, col;
159       get_counts (context_str0, &l, &ch, &col);
160
161       return name_string () + ":" + to_string (l)
162         + ":" + to_string (col);
163     }
164 }
165
166 string
167 Source_file::quote_input (char const *pos_str0) const
168 {
169   if (!contains (pos_str0))
170     return " (" + _ ("position unknown") + ")";
171
172   int l, ch, col;
173   get_counts (pos_str0, &l, &ch, &col);
174   string line = line_string (pos_str0);
175   string context = line.substr (0, ch)
176     + to_string ('\n')
177     + to_string (' ', col)
178     + line.substr (ch, line.length()-ch);
179   return context;
180 }
181
182 string
183 Source_file::name_string () const
184 {
185   return map_file_name (name_);
186 }
187
188 Source_file::~Source_file ()
189 {
190   delete istream_;
191   istream_ = 0;
192   delete[] contents_str0_;
193 }
194
195 Slice
196 Source_file::line_slice (char const *pos_str0) const
197 {
198   if (!contains (pos_str0))
199     return Slice (0, 0);
200
201   char const *data_str0 = c_str ();
202   char const *eof_C_ = data_str0 + length ();
203
204   if (pos_str0 == eof_C_)
205     pos_str0--;
206   char const *begin_str0 = pos_str0;
207   while (begin_str0 > data_str0)
208     if (*--begin_str0 == '\n')
209       {
210         begin_str0++;
211         break;
212       }
213
214   char const *end_str0 = pos_str0;
215   while (end_str0 < eof_C_)
216     if (*end_str0++ == '\n')
217       {
218         end_str0--;
219         break;
220       }
221
222   return Slice (begin_str0 - data_str0, end_str0 - data_str0);
223 }
224
225 string
226 Source_file::line_string (char const *pos_str0) const
227 {
228   if (!contains (pos_str0))
229     return "";
230
231   Slice line = line_slice (pos_str0);
232   char const *data_str0 = c_str ();
233   return string (data_str0 + line[LEFT], line.length ());
234 }
235
236 void
237 Source_file::get_counts (char const *pos_str0,
238                          int *line_number,
239                          int *line_char,
240                          int *column) const
241 {
242   *line_number = 0;
243   *line_char = 0;
244   *column = 0;
245     
246   if (!contains (pos_str0))
247     return;
248
249   *line_number = get_line (pos_str0);
250
251   Slice line = line_slice (pos_str0);
252   char const *data = c_str ();
253   char const *line_start = (char const *)data + line[LEFT];
254
255   ssize left = (char const *) pos_str0 - line_start;
256   string line_begin (line_start, left);
257   char const *line_chars = line_begin.c_str ();
258
259   *column = 0;
260   *line_char = 0;
261
262   mbstate_t state;
263
264   /* Initialize the state.  */
265   memset (&state, '\0', sizeof (state));
266
267   while (left > 0)
268     {
269       wchar_t multibyte[2];
270
271       /*
272         FIXME, this is apparently locale dependent.
273       */
274 #if HAVE_MBRTOWC
275       size_t thislen = mbrtowc (multibyte, line_chars, left, &state);
276 #else
277       size_t thislen = 1;
278 #endif /* !HAVE_MBRTOWC */
279
280       /* Stop converting at invalid character;
281          this can mean we have read just the first part
282          of a valid character.  */
283       if (thislen == (size_t) -1)
284         break;
285
286       /* We want to handle embedded NUL bytes
287          but the return value is 0.  Correct this.  */
288       if (thislen == 0)
289         thislen = 1;
290
291       if (thislen == 1 && line_chars[0] == '\t')
292         (*column) = (*column / 8 + 1) * 8;
293       else
294         (*column)++;
295
296       (*line_char)++;
297       /* Advance past this character. */
298       line_chars += thislen;
299       left -= thislen;
300     }
301 }
302
303 bool
304 Source_file::contains (char const *pos_str0) const
305 {
306   return (pos_str0 && (pos_str0 >= c_str ()) && (pos_str0 <= c_str () + length ()));
307 }
308
309 int
310 Source_file::get_line (char const *pos_str0) const
311 {
312   if (!contains (pos_str0))
313     return 0;
314
315   if (!newline_locations_.size ())
316     return 1;
317
318   vsize lo = 0;
319   vsize hi = newline_locations_.size ();
320
321   if (newline_locations_[lo] > pos_str0)
322     return 1;
323
324   if (newline_locations_[hi - 1] < pos_str0)
325     return hi;
326
327   binary_search_bounds (newline_locations_,
328                         (char const*&)pos_str0,
329                         default_compare,
330                         &lo, &hi);
331
332   if (*pos_str0 == '\n')
333     lo--;
334   return lo + 2 + line_offset_;
335 }
336
337 void
338 Source_file::set_line (char const *pos_str0, int line)
339 {
340   int current_line = get_line (pos_str0);
341   line_offset_ += line - current_line;
342
343   assert (line == get_line (pos_str0));
344 }
345
346 int
347 Source_file::length () const
348 {
349   return length_;
350 }
351
352 char const *
353 Source_file::c_str () const
354 {
355   return contents_str0_;
356 }
357
358 void
359 Source_file::set_pos (char const *pos_str0)
360 {
361   if (contains (pos_str0))
362     pos_str0_ = pos_str0;
363   else
364     error (quote_input (pos_str0) + "invalid pos");
365 }
366
367 char const *
368 Source_file::seek_str0 (int n)
369 {
370   char const *new_str0 = c_str () + n;
371   if (n < 0)
372     new_str0 += length ();
373   if (contains (new_str0))
374     pos_str0_ = new_str0;
375   else
376     error (quote_input (new_str0) + "seek past eof");
377
378   return pos_str0_;
379 }
380
381 char const *
382 Source_file::forward_str0 (int n)
383 {
384   char const *old_pos = pos_str0_;
385   char const *new_str0 = pos_str0_ + n;
386   if (contains (new_str0))
387     pos_str0_ = new_str0;
388   else
389     error (quote_input (new_str0) + "forward past eof");
390
391   return old_pos;
392 }
393
394 string
395 Source_file::get_string (int n)
396 {
397   string str = string ((char const *)forward_str0 (n), n);
398   return str;
399 }
400
401 SCM
402 Source_file::get_port () const
403 {
404   return str_port_;
405 }