2 source-file.cc -- implement Source_file
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2002 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@cs.uu.nl>
17 #include <strstream.h>
18 #define istringstream(x) istrstream(x, length ())
22 #include "flower-proto.hh"
24 #include "source-file.hh"
28 Source_file::load_stdin ()
33 Array<char> chs; // ugh.
34 while ((c = fgetc (stdin)) != EOF)
37 length_ = chs.size ();
38 contents_str0_ = chs.remove_array ();
44 gulp_file (String fn, int* len)
47 let's hope that "b" opens anything binary, and does not apply
50 FILE * f = fopen (fn.to_str0 (), "rb");
54 warning (_f ("can't open file: `%s'", fn.to_str0 ()));
58 int ret = fseek (f, 0, SEEK_END);
62 char * str = new char[*len+1];
64 ret = fread (str, sizeof (char), *len, f);
67 warning (_f ("Huh? Got %d, expected %d characters", ret, *len));
77 Source_file::Source_file (String filename, String data)
81 contents_str0_ = data.get_copy_str0();
82 length_ = data.length();
83 pos_str0_ = to_str0 ();
87 Source_file::Source_file (String filename_string)
89 name_string_ = filename_string;
93 if (filename_string == "-")
96 contents_str0_ = gulp_file (filename_string, &length_);
98 pos_str0_ = to_str0 ();
102 for (int i = 0; i < length_; i++)
103 if (contents_str0_[i] == '\n')
104 newline_locations_.push (contents_str0_ + i);
108 Source_file::init_port ()
110 SCM str =scm_makfrom0str (contents_str0_);
112 str_port_ = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
114 scm_set_port_filename_x (str_port_,
115 scm_makfrom0str (name_string_.get_str0()));
119 Source_file::tell () const
121 return pos_str0_ - contents_str0_;
125 Source_file::get_istream ()
129 if (length ()) // can-t this be done without such a hack?
130 istream_ = new std::istringstream (to_str0 ());
133 istream_ = new std::istringstream ("");
134 istream_->setstate (std::ios::eofbit);
135 // istream_->set (ios::eofbit);
142 Source_file::file_line_column_string (char const *context_str0) const
145 return " (" + _ ("position unknown") + ")";
147 return name_string () + ":" + to_string (get_line (context_str0))
148 + ":" + to_string (get_char (context_str0));
152 Source_file::name_string () const
157 Source_file::~Source_file ()
161 delete[] contents_str0_;
165 Source_file::line_slice (char const* pos_str0) const
167 if (!in_b (pos_str0))
170 char const* data_str0 = to_str0 ();
171 char const * eof_C_ = data_str0 + length ();
173 if (pos_str0 == eof_C_)
175 char const* begin_str0 = pos_str0;
176 while (begin_str0 > data_str0)
177 if (*--begin_str0 == '\n')
183 char const* end_str0 = pos_str0;
184 while (end_str0 < eof_C_)
185 if (*end_str0++ == '\n')
191 return Slice (begin_str0 - data_str0, end_str0 - data_str0);
195 Source_file::line_string (char const* pos_str0) const
197 if (!in_b (pos_str0))
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 ());
206 Source_file::get_char (char const* pos_str0) const
208 if (!in_b (pos_str0))
211 char const* data_str0 = to_str0 ();
212 return pos_str0 - (line_slice (pos_str0)[SMALLER] + data_str0);
216 Source_file::get_column (char const* pos_str0) const
218 if (!in_b (pos_str0))
221 int ch_i = get_char (pos_str0);
222 String line = line_string (pos_str0);
225 for (int i = 0; i < ch_i; i++)
227 col_i = (col_i / 8 + 1) * 8;
235 Source_file::error_string (char const* pos_str0) const
237 if (!in_b (pos_str0))
238 return " (" + _ ("position unknown") + ")";
240 int ch_i = get_char (pos_str0);
241 String line = line_string (pos_str0);
242 String context = line.left_string (ch_i)
244 + to_string (' ', get_column (pos_str0))
245 + line.cut_string (ch_i, INT_MAX);
251 Source_file::in_b (char const* pos_str0) const
253 return (pos_str0 && (pos_str0 >= to_str0 ()) && (pos_str0 <= to_str0 () + length ()));
257 Source_file::get_line (char const* pos_str0) const
259 if (!in_b (pos_str0))
262 if (!newline_locations_.size())
266 int hi = newline_locations_.size();
268 if (newline_locations_[lo] > pos_str0)
271 if (newline_locations_[hi-1] < pos_str0)
274 binary_search_bounds (newline_locations_,
276 Link_array<char>::default_compare,
283 Source_file::length () const
289 Source_file::to_str0 () const
291 return contents_str0_;
295 Source_file::set_pos (char const * pos_str0)
298 pos_str0_ = pos_str0;
300 error (error_string (pos_str0) + "invalid pos");
304 Source_file::seek_str0 (int n)
306 char const* new_str0 = to_str0 () + n;
308 new_str0 += length ();
310 pos_str0_ = new_str0;
312 error (error_string (new_str0) + "seek past eof");
318 Source_file::forward_str0 (int n)
320 char const* old_pos = pos_str0_;
321 char const* new_str0 = pos_str0_ + n;
323 pos_str0_ = new_str0;
325 error (error_string (new_str0) + "forward past eof");
331 Source_file::get_string (int n)
333 String str = String ((Byte const*)forward_str0 (n), n);