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