]> git.donarmstrong.com Git - lilypond.git/blob - lily/source-file.cc
* ly/performer-init.ly: add CueVoice to MIDI too.
[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--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7   Han-Wen Nienhuys <hanwen@cs.uu.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>  /* wcrtomb */
16 #else
17 #include <wchar.h> /* wcrtomb */
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
29 #include "warn.hh"
30 #include "file-name-map.hh"
31
32 void
33 Source_file::load_stdin ()
34 {
35   length_ = 0;
36
37   int c;
38   Array<char> chs;              // ugh.
39   while ((c = fgetc (stdin)) != EOF)
40     chs.push (c);
41
42   chs.push (0);
43   length_ = chs.size ();
44   contents_str0_ = chs.remove_array ();
45 }
46
47 char *
48 gulp_file (String filename, int *filesize)
49 {
50   /* "b" must ensure to open literally, avoiding text (CR/LF)
51      conversions.  */
52   FILE *f = fopen (filename.to_str0 (), "rb");
53   if (!f)
54     {
55       warning (_f ("can't open file: `%s'", filename.to_str0 ()));
56       return 0;
57     }
58
59   fseek (f, 0, SEEK_END);
60   *filesize = ftell (f);
61   rewind (f);
62
63   char *str = new char[*filesize + 1];
64   str[*filesize] = 0;
65
66   int bytes_read = fread (str, sizeof (char), *filesize, f);
67   if (bytes_read != *filesize)
68     warning (_f ("expected to read %d characters, got %d", bytes_read,
69                  *filesize));
70   fclose (f);
71
72   return str;
73 }
74
75 Source_file::Source_file (String filename, String data)
76 {
77   name_ = filename;
78   istream_ = 0;
79   contents_str0_ = data.get_copy_str0 ();
80   length_ = data.length ();
81   pos_str0_ = to_str0 ();
82   init_port ();
83
84   for (int i = 0; i < length_; i++)
85     if (contents_str0_[i] == '\n')
86       newline_locations_.push (contents_str0_ + i);
87 }
88
89 Source_file::Source_file (String filename_string)
90 {
91   name_ = filename_string;
92   istream_ = 0;
93   contents_str0_ = 0;
94
95   if (filename_string == "-")
96     load_stdin ();
97   else
98     contents_str0_ = gulp_file (filename_string, &length_);
99
100   pos_str0_ = to_str0 ();
101
102   init_port ();
103
104   for (int i = 0; i < length_; i++)
105     if (contents_str0_[i] == '\n')
106       newline_locations_.push (contents_str0_ + i);
107 }
108
109 void
110 Source_file::init_port ()
111 {
112   SCM str = scm_makfrom0str (contents_str0_);
113   str_port_ = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG, __FUNCTION__);
114   scm_set_port_filename_x (str_port_, scm_makfrom0str (name_.get_str0 ()));
115 }
116
117 int
118 Source_file::tell () const
119 {
120   return pos_str0_ - contents_str0_; 
121 }
122
123 std::istream*
124 Source_file::get_istream ()
125 {
126   if (!istream_)
127     {
128       if (length ()) // can-t this be done without such a hack?
129         istream_ = new std::istringstream (to_str0 ());
130       else
131         {
132           istream_ = new std::istringstream ("");
133           istream_->setstate (std::ios::eofbit);
134           //      istream_->set (ios::eofbit);
135         }
136     }
137   return istream_;
138 }
139
140 String
141 Source_file::file_line_column_string (char const *context_str0) const
142 {
143   if (!to_str0 ())
144     return " (" + _ ("position unknown") + ")";
145   else
146     return name_string () + ":" + to_string (get_line (context_str0))
147       + ":" + to_string (get_column (context_str0));
148 }
149
150 String
151 Source_file::name_string () const
152 {
153   return map_file_name (name_);
154 }
155
156 Source_file::~Source_file ()
157 {
158   delete istream_;
159   istream_ = 0;
160   delete[] contents_str0_;
161 }
162
163 Slice
164 Source_file::line_slice (char const *pos_str0) const
165 {
166   if (!contains (pos_str0))
167     return Slice (0, 0);
168
169   char const *data_str0 = to_str0 ();
170   char const *eof_C_ = data_str0 + length ();
171
172   if (pos_str0 == eof_C_)
173     pos_str0--;
174   char const *begin_str0 = pos_str0;
175   while (begin_str0 > data_str0)
176     if (*--begin_str0 == '\n')
177       {
178         begin_str0++;
179         break;
180       }
181
182   char const* end_str0 = pos_str0;
183   while (end_str0 < eof_C_)
184     if (*end_str0++ == '\n')
185       {
186         end_str0--;
187         break;
188       }
189
190   return Slice (begin_str0 - data_str0, end_str0 - data_str0);
191 }
192
193 String
194 Source_file::line_string (char const* pos_str0) const
195 {
196   if (!contains (pos_str0))
197     return "";
198
199   Slice line = line_slice (pos_str0);
200   char const *data_str0 = to_str0 ();
201   return String ((Byte const *)data_str0 + line[LEFT], line.length ());
202 }
203
204 int
205 Source_file::get_char_of_line (char const *pos_str0) const
206 {
207   if (!contains (pos_str0))
208     return 0;
209
210   char const *data_str0 = to_str0 ();
211   return pos_str0 - (line_slice (pos_str0)[SMALLER] + data_str0);
212 }
213
214 int
215 Source_file::get_column (char const *pos_str0) const
216 {
217   if (!contains (pos_str0))
218     return 0;
219
220   Slice line = line_slice (pos_str0);
221   char const *data = to_str0 ();
222   Byte const *line_start = (Byte const *)data + line[LEFT];
223
224   int left = (Byte const*) pos_str0 -  line_start;
225   String line_begin (line_start, left);
226   char const *line_chars = line_begin.to_str0();
227   
228   int column = 0;
229   mbstate_t state;
230
231   /* Initialize the state.  */
232   memset (&state, '\0', sizeof (state));
233
234   while (left > 0)
235     {
236       wchar_t multibyte[2];
237       size_t thislen = mbrtowc (multibyte, line_chars, left, &state);
238       
239       /* Stop converting at invalid character;
240          this can mean we have read just the first part
241          of a valid character.  */
242       if (thislen == (size_t) -1)
243         break;
244       /* We want to handle embedded NUL bytes
245          but the return value is 0.  Correct this.  */
246       if (thislen == 0)
247         thislen = 1;
248
249       if (thislen == 1 && line_chars[0] == '\t')
250         column = (column / 8 + 1) * 8;  
251       else
252         column ++;
253       
254       /* Advance past this character. */
255       line_chars += thislen;
256       left -= thislen;
257     }
258
259   return column;
260 }
261
262 String
263 Source_file::error_string (char const* pos_str0) const
264 {
265   if (!contains (pos_str0))
266     return " (" + _ ("position unknown") + ")";
267
268   int ch_i = get_char_of_line (pos_str0);
269   String line = line_string (pos_str0);
270   String context = line.left_string (ch_i)
271     + to_string ('\n')
272     + to_string (' ', get_column (pos_str0))
273     + line.cut_string (ch_i, INT_MAX);
274
275   return context;
276 }
277
278 bool
279 Source_file::contains (char const* pos_str0) const
280 {
281   return (pos_str0 && (pos_str0 >= to_str0 ()) && (pos_str0 <= to_str0 () + length ()));
282 }
283
284 int
285 Source_file::get_line (char const* pos_str0) const
286 {
287   if (!contains (pos_str0))
288     return 0;
289
290   if (!newline_locations_.size ())
291     return 1;
292   
293   int lo = 0;
294   int hi = newline_locations_.size ();
295
296   if (newline_locations_[lo] > pos_str0)
297     return 1;
298   
299   if (newline_locations_[hi-1] < pos_str0)
300     return hi;
301   
302   binary_search_bounds (newline_locations_,
303                         pos_str0, 
304                         Link_array<char>::default_compare,
305                         &lo, &hi);
306
307   if (*pos_str0 == '\n')
308     lo--;
309   return lo + 2;
310 }
311
312 int
313 Source_file::length () const
314 {
315   return length_;
316 }
317
318 char const *
319 Source_file::to_str0 () const
320 {
321   return contents_str0_;
322 }
323
324 void
325 Source_file::set_pos (char const * pos_str0)
326 {
327   if (contains (pos_str0))
328     pos_str0_ = pos_str0;
329   else
330     error (error_string (pos_str0) + "invalid pos");
331 }
332
333 char const *
334 Source_file::seek_str0 (int n)
335 {
336   char const *new_str0 = to_str0 () + n;
337   if (n < 0)
338     new_str0 += length ();
339   if (contains (new_str0))
340     pos_str0_ = new_str0;
341   else
342     error (error_string (new_str0) + "seek past eof");
343
344   return pos_str0_;
345 }
346
347 char const *
348 Source_file::forward_str0 (int n)
349 {
350   char const *old_pos = pos_str0_;
351   char const *new_str0 = pos_str0_ + n;
352   if (contains (new_str0))
353     pos_str0_ = new_str0;
354   else
355     error (error_string (new_str0) + "forward past eof");
356
357   return old_pos;
358 }
359
360 String
361 Source_file::get_string (int n)
362 {
363   String str = String ((Byte const *)forward_str0 (n), n);
364   return str;
365 }