]> git.donarmstrong.com Git - lilypond.git/blob - lily/source-file.cc
* VERSION (MY_PATCH_LEVEL): make 1.7.0
[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--2000 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
88
89
90 Source_file::Source_file (String filename_string)
91 {
92   name_string_ = filename_string;
93   istream_ = 0;
94   contents_str0_ = 0;
95
96   if (filename_string == "-")
97     load_stdin ();
98   else
99     contents_str0_ = gulp_file (filename_string, &length_);
100   
101   pos_str0_ = to_str0 ();
102
103   init_port();
104
105   for (int i = 0; i < length_; i++)
106     if (contents_str0_[i] == '\n')
107       newline_locations_.push (contents_str0_ + i);
108 }
109
110 void
111 Source_file::init_port ()
112 {
113   SCM str =scm_makfrom0str (contents_str0_);
114   
115   str_port_ = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
116                              __FUNCTION__);
117   scm_set_port_filename_x (str_port_,
118                            scm_makfrom0str (name_string_.get_str0()));
119 }
120
121 int
122 Source_file::tell () const
123 {
124   return pos_str0_  - contents_str0_; 
125 }
126
127 std::istream*
128 Source_file::get_istream ()
129 {
130   if (!istream_)
131     {
132       if (length ()) // can-t this be done without such a hack?
133         istream_ = new std::istringstream (to_str0 ());
134       else
135         {
136           istream_ = new std::istringstream ("");
137           istream_->setstate (std::ios::eofbit);
138           //      istream_->set (ios::eofbit);
139         }
140     }
141   return istream_;
142 }
143
144 String
145 Source_file::file_line_column_string (char const *context_str0) const
146 {
147   if (!to_str0 ())
148     return " (" + _ ("position unknown") + ")";
149   else
150     return name_string () + ":" + to_string (get_line (context_str0))
151       + ":" + to_string (get_char (context_str0));
152 }
153
154 String
155 Source_file::name_string () const
156 {
157   return name_string_;
158 }
159
160 Source_file::~Source_file ()
161 {
162   delete istream_;
163   istream_ = 0;
164   delete[] contents_str0_;
165 }
166
167 Slice
168 Source_file::line_slice (char const* pos_str0) const
169 {
170   if (!in_b (pos_str0))
171     return Slice (0,0);
172
173   char const* data_str0 = to_str0 ();
174   char const * eof_C_ = data_str0 + length ();
175
176   if (pos_str0 == eof_C_)
177     pos_str0 --;
178   char const* begin_str0 = pos_str0;
179   while (begin_str0 > data_str0)
180     if (*--begin_str0 == '\n')
181       {
182         begin_str0++;
183         break;
184       }
185
186   char const* end_str0 = pos_str0;
187   while (end_str0 < eof_C_)
188     if (*end_str0++ == '\n')
189       {
190         end_str0--;
191         break;
192       }
193
194   return Slice (begin_str0 - data_str0, end_str0 - data_str0);
195 }
196
197 String
198 Source_file::line_string (char const* pos_str0) const
199 {
200   if (!in_b (pos_str0))
201     return "";
202
203   Slice line = line_slice (pos_str0);
204   char const* data_str0 = to_str0 ();
205   return String ((Byte const*)data_str0 + line[LEFT], line.length ());
206 }
207
208 int
209 Source_file::get_char (char const* pos_str0) const
210 {
211   if (!in_b (pos_str0))
212     return 0;
213
214   char const* data_str0 = to_str0 ();
215   return pos_str0 - (line_slice (pos_str0)[SMALLER] + data_str0);
216 }
217
218 int
219 Source_file::get_column (char const* pos_str0) const
220 {
221   if (!in_b (pos_str0))
222     return 0;
223
224   int ch_i = get_char (pos_str0);
225   String line = line_string (pos_str0);
226
227   int col_i = 0;
228   for (int i = 0; i < ch_i; i++)
229     if (line[i] == '\t')
230       col_i = (col_i / 8 + 1) * 8;
231     else
232       col_i++;
233
234   return col_i;
235 }
236
237 String
238 Source_file::error_string (char const* pos_str0) const
239 {
240   if (!in_b (pos_str0))
241     return " (" + _ ("position unknown") + ")";
242
243   int ch_i = get_char (pos_str0);
244   String line = line_string (pos_str0);
245   String context = line.left_string (ch_i)
246     + to_string ('\n')
247     + to_string (' ', get_column (pos_str0))
248     + line.cut_string (ch_i, INT_MAX);
249
250   return context;
251 }
252
253 bool
254 Source_file::in_b (char const* pos_str0) const
255 {
256   return (pos_str0 && (pos_str0 >= to_str0 ()) && (pos_str0 <= to_str0 () + length ()));
257 }
258
259 int
260 Source_file::get_line (char const* pos_str0) const
261 {
262   if (!in_b (pos_str0))
263     return 0;
264
265   int lo=0;
266   int hi = newline_locations_.size();
267   
268   binary_search_bounds (newline_locations_,
269                         pos_str0, 
270                         Link_array<char>::default_compare,
271                         &lo, &hi);
272   
273   return lo;
274 }
275
276 int
277 Source_file::length () const
278 {
279   return length_;
280 }
281
282 char const *
283 Source_file::to_str0 () const
284 {
285   return contents_str0_;
286 }
287
288 void
289 Source_file::set_pos (char const * pos_str0)
290 {
291   if (in_b (pos_str0))
292     pos_str0_ = pos_str0;
293   else
294     error (error_string (pos_str0) + "invalid pos");
295 }
296
297 char const*
298 Source_file::seek_str0 (int n)
299 {
300   char const* new_str0 = to_str0 () + n;
301   if (n < 0)
302     new_str0 += length ();
303   if (in_b (new_str0))
304     pos_str0_ = new_str0;
305   else
306     error (error_string (new_str0) + "seek past eof");
307
308   return pos_str0_;
309 }
310
311 char const*
312 Source_file::forward_str0 (int n)
313 {
314   char const* old_pos = pos_str0_;
315   char const* new_str0 = pos_str0_ + n;
316   if (in_b (new_str0))
317     pos_str0_ = new_str0;
318   else
319     error (error_string (new_str0)  + "forward past eof");
320
321   return old_pos;
322 }
323
324 String
325 Source_file::get_string (int n)
326 {
327   String str = String ((Byte const*)forward_str0 (n), n);
328   return str;
329 }