]> git.donarmstrong.com Git - lilypond.git/blob - lily/source-file.cc
release commit
[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@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 "warn.hh"
31 #include "file-name-map.hh"
32
33 void
34 Source_file::load_stdin ()
35 {
36   length_ = 0;
37
38   int c;
39   Array<char> chs;              // ugh.
40   while ((c = fgetc (stdin)) != EOF)
41     chs.push (c);
42
43   chs.push (0);
44   length_ = chs.size ();
45   contents_str0_ = chs.remove_array ();
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.to_str0 (), "rb");
54   if (!f)
55     {
56       warning (_f ("can't open file: `%s'", filename.to_str0 ()));
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   contents_str0_ = data.get_copy_str0 ();
86   length_ = data.length ();
87   pos_str0_ = to_str0 ();
88   init_port ();
89
90   for (int i = 0; i < length_; i++)
91     if (contents_str0_[i] == '\n')
92       newline_locations_.push (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_ = to_str0 ();
110
111   init_port ();
112
113   for (int i = 0; i < length_; i++)
114     if (contents_str0_[i] == '\n')
115       newline_locations_.push (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_.get_str0 ()));
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 (to_str0 ());
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 (!to_str0 ())
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.left_string (ch)
174     + to_string ('\n')
175     + to_string (' ', col)
176     + line.cut_string (ch, INT_MAX);
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 = to_str0 ();
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 = to_str0 ();
231   return String ((Byte const *)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   if (!contains (pos_str0))
241     return;
242
243   *line_number = get_line (pos_str0);
244
245   Slice line = line_slice (pos_str0);
246   char const *data = to_str0 ();
247   Byte const *line_start = (Byte const *)data + line[LEFT];
248
249   int left = (Byte const *) pos_str0 - line_start;
250   String line_begin (line_start, left);
251   char const *line_chars = line_begin.to_str0 ();
252
253   *column = 0;
254   *line_char = 0;
255
256   mbstate_t state;
257
258   /* Initialize the state.  */
259   memset (&state, '\0', sizeof (state));
260
261   while (left > 0)
262     {
263       wchar_t multibyte[2];
264
265       /*
266         FIXME, this is apparently locale dependent.
267       */
268       size_t thislen = mbrtowc (multibyte, line_chars, left, &state);
269
270       /* Stop converting at invalid character;
271          this can mean we have read just the first part
272          of a valid character.  */
273       if (thislen == (size_t) -1)
274         break;
275
276       /* We want to handle embedded NUL bytes
277          but the return value is 0.  Correct this.  */
278       if (thislen == 0)
279         thislen = 1;
280
281       if (thislen == 1 && line_chars[0] == '\t')
282         (*column) = (*column / 8 + 1) * 8;
283       else
284         (*column)++;
285
286       (*line_char)++;
287       /* Advance past this character. */
288       line_chars += thislen;
289       left -= thislen;
290     }
291 }
292
293 bool
294 Source_file::contains (char const *pos_str0) const
295 {
296   return (pos_str0 && (pos_str0 >= to_str0 ()) && (pos_str0 <= to_str0 () + length ()));
297 }
298
299 int
300 Source_file::get_line (char const *pos_str0) const
301 {
302   if (!contains (pos_str0))
303     return 0;
304
305   if (!newline_locations_.size ())
306     return 1;
307
308   int lo = 0;
309   int hi = newline_locations_.size ();
310
311   if (newline_locations_[lo] > pos_str0)
312     return 1;
313
314   if (newline_locations_[hi - 1] < pos_str0)
315     return hi;
316
317   binary_search_bounds (newline_locations_,
318                         pos_str0,
319                         Link_array<char>::default_compare,
320                         &lo, &hi);
321
322   if (*pos_str0 == '\n')
323     lo--;
324   return lo + 2;
325 }
326
327 int
328 Source_file::length () const
329 {
330   return length_;
331 }
332
333 char const *
334 Source_file::to_str0 () const
335 {
336   return contents_str0_;
337 }
338
339 void
340 Source_file::set_pos (char const *pos_str0)
341 {
342   if (contains (pos_str0))
343     pos_str0_ = pos_str0;
344   else
345     error (quote_input (pos_str0) + "invalid pos");
346 }
347
348 char const *
349 Source_file::seek_str0 (int n)
350 {
351   char const *new_str0 = to_str0 () + n;
352   if (n < 0)
353     new_str0 += length ();
354   if (contains (new_str0))
355     pos_str0_ = new_str0;
356   else
357     error (quote_input (new_str0) + "seek past eof");
358
359   return pos_str0_;
360 }
361
362 char const *
363 Source_file::forward_str0 (int n)
364 {
365   char const *old_pos = pos_str0_;
366   char const *new_str0 = pos_str0_ + n;
367   if (contains (new_str0))
368     pos_str0_ = new_str0;
369   else
370     error (quote_input (new_str0) + "forward past eof");
371
372   return old_pos;
373 }
374
375 String
376 Source_file::get_string (int n)
377 {
378   String str = String ((Byte const *)forward_str0 (n), n);
379   return str;
380 }
381
382 SCM
383 Source_file::get_port () const
384 {
385   return str_port_;
386 }