]> git.donarmstrong.com Git - lilypond.git/blob - lily/source-file.cc
* lily/source-file.cc (get_line): oops. All line numbers were off
[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--2002 Jan Nieuwenhuizen <janneke@gnu.org>
7   Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9
10 #include "config.h"
11
12 #include <stdio.h>
13 #include <assert.h>
14 #if HAVE_SSTREAM
15 #include <sstream>
16 #else
17 #include <strstream.h>
18 #define istringstream(x) istrstream(x, length ()) 
19 #endif
20
21 #include "string.hh"
22 #include "flower-proto.hh"
23 #include "warn.hh"
24 #include "source-file.hh"
25 #include "array.hh"
26
27 void
28 Source_file::load_stdin ()
29 {
30   length_ = 0;
31
32   int c;
33   Array<char> chs;              // ugh.
34   while ((c = fgetc (stdin)) != EOF)
35     chs.push (c);
36
37   length_ = chs.size ();
38   contents_str0_ = chs.remove_array ();
39 }
40
41
42
43 char *
44 gulp_file (String fn, int* len)
45 {
46   /*
47     let's hope that "b" opens anything binary, and does not apply
48     CR/LF translation
49     */
50   FILE * f =  fopen (fn.to_str0 (), "rb");
51
52   if (!f)
53     {
54       warning (_f ("can't open file: `%s'", fn.to_str0 ()));
55       return 0;
56     }
57
58   int ret = fseek (f, 0, SEEK_END);
59
60   *len = ftell (f);
61   rewind (f);
62   char *  str = new char[*len+1];
63   str[*len] = 0;
64   ret = fread (str, sizeof (char), *len, f);
65
66   if (ret!=*len)
67     warning (_f ("Huh?  Got %d, expected %d characters", ret, *len));
68
69   fclose (f);
70
71
72   return str;
73 }
74
75
76
77 Source_file::Source_file (String filename, String data)
78 {
79   name_string_ = "";
80   istream_ = 0;
81   contents_str0_ = data.get_copy_str0();
82   length_ = data.length();
83   pos_str0_ = to_str0 ();
84   init_port();
85 }
86
87 Source_file::Source_file (String filename_string)
88 {
89   name_string_ = filename_string;
90   istream_ = 0;
91   contents_str0_ = 0;
92
93   if (filename_string == "-")
94     load_stdin ();
95   else
96     contents_str0_ = gulp_file (filename_string, &length_);
97   
98   pos_str0_ = to_str0 ();
99
100   init_port();
101
102   for (int i = 0; i < length_; i++)
103     if (contents_str0_[i] == '\n')
104       newline_locations_.push (contents_str0_ + i);
105 }
106
107 void
108 Source_file::init_port ()
109 {
110   SCM str =scm_makfrom0str (contents_str0_);
111   
112   str_port_ = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
113                              __FUNCTION__);
114   scm_set_port_filename_x (str_port_,
115                            scm_makfrom0str (name_string_.get_str0()));
116 }
117
118 int
119 Source_file::tell () const
120 {
121   return pos_str0_  - contents_str0_; 
122 }
123
124 std::istream*
125 Source_file::get_istream ()
126 {
127   if (!istream_)
128     {
129       if (length ()) // can-t this be done without such a hack?
130         istream_ = new std::istringstream (to_str0 ());
131       else
132         {
133           istream_ = new std::istringstream ("");
134           istream_->setstate (std::ios::eofbit);
135           //      istream_->set (ios::eofbit);
136         }
137     }
138   return istream_;
139 }
140
141 String
142 Source_file::file_line_column_string (char const *context_str0) const
143 {
144   if (!to_str0 ())
145     return " (" + _ ("position unknown") + ")";
146   else
147     return name_string () + ":" + to_string (get_line (context_str0))
148       + ":" + to_string (get_char (context_str0));
149 }
150
151 String
152 Source_file::name_string () const
153 {
154   return name_string_;
155 }
156
157 Source_file::~Source_file ()
158 {
159   delete istream_;
160   istream_ = 0;
161   delete[] contents_str0_;
162 }
163
164 Slice
165 Source_file::line_slice (char const* pos_str0) const
166 {
167   if (!in_b (pos_str0))
168     return Slice (0,0);
169
170   char const* data_str0 = to_str0 ();
171   char const * eof_C_ = data_str0 + length ();
172
173   if (pos_str0 == eof_C_)
174     pos_str0 --;
175   char const* begin_str0 = pos_str0;
176   while (begin_str0 > data_str0)
177     if (*--begin_str0 == '\n')
178       {
179         begin_str0++;
180         break;
181       }
182
183   char const* end_str0 = pos_str0;
184   while (end_str0 < eof_C_)
185     if (*end_str0++ == '\n')
186       {
187         end_str0--;
188         break;
189       }
190
191   return Slice (begin_str0 - data_str0, end_str0 - data_str0);
192 }
193
194 String
195 Source_file::line_string (char const* pos_str0) const
196 {
197   if (!in_b (pos_str0))
198     return "";
199
200   Slice line = line_slice (pos_str0);
201   char const* data_str0 = to_str0 ();
202   return String ((Byte const*)data_str0 + line[LEFT], line.length ());
203 }
204
205 int
206 Source_file::get_char (char const* pos_str0) const
207 {
208   if (!in_b (pos_str0))
209     return 0;
210
211   char const* data_str0 = to_str0 ();
212   return pos_str0 - (line_slice (pos_str0)[SMALLER] + data_str0);
213 }
214
215 int
216 Source_file::get_column (char const* pos_str0) const
217 {
218   if (!in_b (pos_str0))
219     return 0;
220
221   int ch_i = get_char (pos_str0);
222   String line = line_string (pos_str0);
223
224   int col_i = 0;
225   for (int i = 0; i < ch_i; i++)
226     if (line[i] == '\t')
227       col_i = (col_i / 8 + 1) * 8;
228     else
229       col_i++;
230
231   return col_i;
232 }
233
234 String
235 Source_file::error_string (char const* pos_str0) const
236 {
237   if (!in_b (pos_str0))
238     return " (" + _ ("position unknown") + ")";
239
240   int ch_i = get_char (pos_str0);
241   String line = line_string (pos_str0);
242   String context = line.left_string (ch_i)
243     + to_string ('\n')
244     + to_string (' ', get_column (pos_str0))
245     + line.cut_string (ch_i, INT_MAX);
246
247   return context;
248 }
249
250 bool
251 Source_file::in_b (char const* pos_str0) const
252 {
253   return (pos_str0 && (pos_str0 >= to_str0 ()) && (pos_str0 <= to_str0 () + length ()));
254 }
255
256 int
257 Source_file::get_line (char const* pos_str0) const
258 {
259   if (!in_b (pos_str0))
260     return 0;
261
262   if (!newline_locations_.size())
263     return 1;
264   
265   int lo=0;
266   int hi = newline_locations_.size();
267
268   if (newline_locations_[lo] > pos_str0)
269     return 1;
270   
271   if (newline_locations_[hi-1] < pos_str0)
272     return hi;
273   
274   binary_search_bounds (newline_locations_,
275                         pos_str0, 
276                         Link_array<char>::default_compare,
277                         &lo, &hi);
278   
279   return lo + 2;
280 }
281
282 int
283 Source_file::length () const
284 {
285   return length_;
286 }
287
288 char const *
289 Source_file::to_str0 () const
290 {
291   return contents_str0_;
292 }
293
294 void
295 Source_file::set_pos (char const * pos_str0)
296 {
297   if (in_b (pos_str0))
298     pos_str0_ = pos_str0;
299   else
300     error (error_string (pos_str0) + "invalid pos");
301 }
302
303 char const*
304 Source_file::seek_str0 (int n)
305 {
306   char const* new_str0 = to_str0 () + n;
307   if (n < 0)
308     new_str0 += length ();
309   if (in_b (new_str0))
310     pos_str0_ = new_str0;
311   else
312     error (error_string (new_str0) + "seek past eof");
313
314   return pos_str0_;
315 }
316
317 char const*
318 Source_file::forward_str0 (int n)
319 {
320   char const* old_pos = pos_str0_;
321   char const* new_str0 = pos_str0_ + n;
322   if (in_b (new_str0))
323     pos_str0_ = new_str0;
324   else
325     error (error_string (new_str0)  + "forward past eof");
326
327   return old_pos;
328 }
329
330 String
331 Source_file::get_string (int n)
332 {
333   String str = String ((Byte const*)forward_str0 (n), n);
334   return str;
335 }