2 source-file.cc -- implement Source_file
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2000 Jan Nieuwenhuizen <janneke@gnu.org>
7 & Han-Wen Nienhuys <hanwen@cs.uu.nl>
15 #include <strstream.h>
16 #define istringstream(x) istrstream(x, length ())
20 #include "flower-proto.hh"
22 #include "source-file.hh"
23 #include "simple-file-storage.hh"
24 #include "string-storage.hh"
26 Source_file::Source_file (String filename_string)
28 name_string_ = filename_string;
30 storage_ = new Simple_file_storage (filename_string);
31 pos_str0_ = to_str0 ();
34 Source_file::Source_file (String name_string, String data_string)
36 name_string_ = name_string;
38 storage_ = new String_storage (data_string);
39 pos_str0_ = to_str0 ();
43 Source_file::get_istream ()
46 if (!name_string_.length ())
52 if (length ()) // can-t this be done without such a hack?
53 istream_ = new std::istringstream (to_str0 ());
56 istream_ = new std::istringstream ("");
57 istream_->setstate (std::ios::eofbit);
58 // istream_->set (ios::eofbit);
65 Source_file::file_line_column_string (char const *context_str0) const
68 return " (" + _ ("position unknown") + ")";
70 return name_string () + ":" + to_string (get_line (context_str0))
71 + ":" + to_string (get_char (context_str0));
75 Source_file::name_string () const
80 Source_file::~Source_file ()
88 Source_file::line_slice (char const* pos_str0) const
93 char const* data_str0 = to_str0 ();
94 char const * eof_C_ = data_str0 + length ();
96 if (pos_str0 == eof_C_)
98 char const* begin_str0 = pos_str0;
99 while (begin_str0 > data_str0)
100 if (*--begin_str0 == '\n')
106 char const* end_str0 = pos_str0;
107 while (end_str0 < eof_C_)
108 if (*end_str0++ == '\n')
114 return Slice (begin_str0 - data_str0, end_str0 - data_str0);
118 Source_file::line_string (char const* pos_str0) const
120 if (!in_b (pos_str0))
123 Slice line = line_slice (pos_str0);
124 char const* data_str0 = to_str0 ();
125 return String ((Byte const*)data_str0 + line[LEFT], line.length ());
129 Source_file::get_char (char const* pos_str0) const
131 if (!in_b (pos_str0))
134 char const* data_str0 = to_str0 ();
135 return pos_str0 - (line_slice (pos_str0)[SMALLER] + data_str0);
139 Source_file::get_column (char const* pos_str0) const
141 if (!in_b (pos_str0))
144 int ch_i = get_char (pos_str0);
145 String line = line_string (pos_str0);
148 for (int i = 0; i < ch_i; i++)
150 col_i = (col_i / 8 + 1) * 8;
158 Source_file::error_string (char const* pos_str0) const
160 if (!in_b (pos_str0))
161 return " (" + _ ("position unknown") + ")";
163 int ch_i = get_char (pos_str0);
164 String line = line_string (pos_str0);
165 String context = line.left_string (ch_i)
167 + to_string (' ', get_column (pos_str0))
168 + line.cut_string (ch_i, INT_MAX);
174 Source_file::in_b (char const* pos_str0) const
176 return (pos_str0 && (pos_str0 >= to_str0 ()) && (pos_str0 <= to_str0 () + length ()));
180 Source_file::get_line (char const* pos_str0) const
182 if (!in_b (pos_str0))
186 char const* scan_str0 = to_str0 ();
190 while (scan_str0 < pos_str0)
191 if (*scan_str0++ == '\n')
197 Source_file::length () const
199 return storage_->length ();
203 Source_file::to_str0 () const
205 return storage_->to_str0 ();
209 Source_file::set_pos (char const * pos_str0)
212 pos_str0_ = pos_str0;
214 error (error_string (pos_str0) + "invalid pos");
218 Source_file::seek_str0 (int n)
220 char const* new_str0 = to_str0 () + n;
222 new_str0 += length ();
224 pos_str0_ = new_str0;
226 error (error_string (new_str0) + "seek past eof");
232 Source_file::forward_str0 (int n)
234 char const* old_pos = pos_str0_;
235 char const* new_str0 = pos_str0_ + n;
237 pos_str0_ = new_str0;
239 error (error_string (new_str0) + "forward past eof");
245 Source_file::get_string (int n)
247 String str = String ((Byte const*)forward_str0 (n), n);