]> git.donarmstrong.com Git - lilypond.git/blob - lily/source-file.cc
Fix some bugs in the dynamic engraver and PostScript backend
[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   length_ = data.length ();
86   contents_str0_ = string_copy (data);
87   pos_str0_ = c_str ();
88   init_port ();
89
90   for (int i = 0; i < length_; i++)
91     if (contents_str0_[i] == '\n')
92       newline_locations_.push_back (contents_str0_ + i);
93 }
94
95 Source_file::Source_file (string filename_string)
96 {
97   name_ = filename_string;
98   istream_ = 0;
99   contents_str0_ = 0;
100
101   if (filename_string == "-")
102     load_stdin ();
103   else
104     {
105       length_ = -1;
106       contents_str0_ = gulp_file (filename_string, &length_);
107     }
108   
109   pos_str0_ = c_str ();
110
111   init_port ();
112
113   for (int i = 0; i < length_; i++)
114     if (contents_str0_[i] == '\n')
115       newline_locations_.push_back (contents_str0_ + i);
116 }
117
118 void
119 Source_file::init_port ()
120 {
121   SCM str = scm_makfrom0str (contents_str0_);
122   str_port_ = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG, __FUNCTION__);
123   scm_set_port_filename_x (str_port_, scm_makfrom0str (name_.c_str ()));
124 }
125
126 int
127 Source_file::tell () const
128 {
129   return pos_str0_ - contents_str0_;
130 }
131
132 istream *
133 Source_file::get_istream ()
134 {
135   if (!istream_)
136     {
137       if (length ()) // can-t this be done without such a hack?
138         istream_ = new istringstream (c_str ());
139       else
140         {
141           istream_ = new istringstream ("");
142           istream_->setstate (ios::eofbit);
143           //      istream_->set (ios::eofbit);
144         }
145     }
146   return istream_;
147 }
148
149 string
150 Source_file::file_line_column_string (char const *context_str0) const
151 {
152   if (!c_str ())
153     return " (" + _ ("position unknown") + ")";
154   else
155     {
156       int l, ch, col;
157       get_counts (context_str0, &l, &ch, &col);
158
159       return name_string () + ":" + to_string (l)
160         + ":" + to_string (col);
161     }
162 }
163
164 string
165 Source_file::quote_input (char const *pos_str0) const
166 {
167   if (!contains (pos_str0))
168     return " (" + _ ("position unknown") + ")";
169
170   int l, ch, col;
171   get_counts (pos_str0, &l, &ch, &col);
172   string line = line_string (pos_str0);
173   string context = line.substr (0, ch)
174     + to_string ('\n')
175     + to_string (' ', col)
176     + line.substr (ch, line.length()-ch);
177   return context;
178 }
179
180 string
181 Source_file::name_string () const
182 {
183   return map_file_name (name_);
184 }
185
186 Source_file::~Source_file ()
187 {
188   delete istream_;
189   istream_ = 0;
190   delete[] contents_str0_;
191 }
192
193 Slice
194 Source_file::line_slice (char const *pos_str0) const
195 {
196   if (!contains (pos_str0))
197     return Slice (0, 0);
198
199   char const *data_str0 = c_str ();
200   char const *eof_C_ = data_str0 + length ();
201
202   if (pos_str0 == eof_C_)
203     pos_str0--;
204   char const *begin_str0 = pos_str0;
205   while (begin_str0 > data_str0)
206     if (*--begin_str0 == '\n')
207       {
208         begin_str0++;
209         break;
210       }
211
212   char const *end_str0 = pos_str0;
213   while (end_str0 < eof_C_)
214     if (*end_str0++ == '\n')
215       {
216         end_str0--;
217         break;
218       }
219
220   return Slice (begin_str0 - data_str0, end_str0 - data_str0);
221 }
222
223 string
224 Source_file::line_string (char const *pos_str0) const
225 {
226   if (!contains (pos_str0))
227     return "";
228
229   Slice line = line_slice (pos_str0);
230   char const *data_str0 = c_str ();
231   return string (data_str0 + line[LEFT], line.length ());
232 }
233
234 void
235 Source_file::get_counts (char const *pos_str0,
236                          int *line_number,
237                          int *line_char,
238                          int *column) const
239 {
240   *line_number = 0;
241   *line_char = 0;
242   *column = 0;
243     
244   if (!contains (pos_str0))
245     return;
246
247   *line_number = get_line (pos_str0);
248
249   Slice line = line_slice (pos_str0);
250   char const *data = c_str ();
251   char const *line_start = (char const *)data + line[LEFT];
252
253   ssize left = (char const *) pos_str0 - line_start;
254   string line_begin (line_start, left);
255   char const *line_chars = line_begin.c_str ();
256
257   *column = 0;
258   *line_char = 0;
259
260   mbstate_t state;
261
262   /* Initialize the state.  */
263   memset (&state, '\0', sizeof (state));
264
265   while (left > 0)
266     {
267       wchar_t multibyte[2];
268
269       /*
270         FIXME, this is apparently locale dependent.
271       */
272 #if HAVE_MBRTOWC
273       size_t thislen = mbrtowc (multibyte, line_chars, left, &state);
274 #else
275       size_t thislen = 1;
276 #endif /* !HAVE_MBRTOWC */
277
278       /* Stop converting at invalid character;
279          this can mean we have read just the first part
280          of a valid character.  */
281       if (thislen == (size_t) -1)
282         break;
283
284       /* We want to handle embedded NUL bytes
285          but the return value is 0.  Correct this.  */
286       if (thislen == 0)
287         thislen = 1;
288
289       if (thislen == 1 && line_chars[0] == '\t')
290         (*column) = (*column / 8 + 1) * 8;
291       else
292         (*column)++;
293
294       (*line_char)++;
295       /* Advance past this character. */
296       line_chars += thislen;
297       left -= thislen;
298     }
299 }
300
301 bool
302 Source_file::contains (char const *pos_str0) const
303 {
304   return (pos_str0 && (pos_str0 >= c_str ()) && (pos_str0 <= c_str () + length ()));
305 }
306
307 int
308 Source_file::get_line (char const *pos_str0) const
309 {
310   if (!contains (pos_str0))
311     return 0;
312
313   if (!newline_locations_.size ())
314     return 1;
315
316   vsize lo = 0;
317   vsize hi = newline_locations_.size ();
318
319   if (newline_locations_[lo] > pos_str0)
320     return 1;
321
322   if (newline_locations_[hi - 1] < pos_str0)
323     return hi;
324
325   binary_search_bounds (newline_locations_,
326                         (char const*&)pos_str0,
327                         default_compare,
328                         &lo, &hi);
329
330   if (*pos_str0 == '\n')
331     lo--;
332   return lo + 2;
333 }
334
335 int
336 Source_file::length () const
337 {
338   return length_;
339 }
340
341 char const *
342 Source_file::c_str () const
343 {
344   return contents_str0_;
345 }
346
347 void
348 Source_file::set_pos (char const *pos_str0)
349 {
350   if (contains (pos_str0))
351     pos_str0_ = pos_str0;
352   else
353     error (quote_input (pos_str0) + "invalid pos");
354 }
355
356 char const *
357 Source_file::seek_str0 (int n)
358 {
359   char const *new_str0 = c_str () + n;
360   if (n < 0)
361     new_str0 += length ();
362   if (contains (new_str0))
363     pos_str0_ = new_str0;
364   else
365     error (quote_input (new_str0) + "seek past eof");
366
367   return pos_str0_;
368 }
369
370 char const *
371 Source_file::forward_str0 (int n)
372 {
373   char const *old_pos = pos_str0_;
374   char const *new_str0 = pos_str0_ + n;
375   if (contains (new_str0))
376     pos_str0_ = new_str0;
377   else
378     error (quote_input (new_str0) + "forward past eof");
379
380   return old_pos;
381 }
382
383 string
384 Source_file::get_string (int n)
385 {
386   string str = string ((char const *)forward_str0 (n), n);
387   return str;
388 }
389
390 SCM
391 Source_file::get_port () const
392 {
393   return str_port_;
394 }