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