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>
12 #include <strstream.h>
17 #include "source-file.hh"
18 #include "simple-file-storage.hh"
19 #include "string-storage.hh"
21 Source_file::Source_file (String filename_str
)
23 name_str_
= filename_str
;
25 storage_p_
= new Simple_file_storage (filename_str
);
29 Source_file::Source_file (String name_str
, String data_str
)
33 storage_p_
= new String_storage (data_str
);
38 Source_file::istream_l ()
41 if (!name_str_.length_i ())
47 if (length_i ()) // can-t this be done without such a hack?
48 istream_p_
= new istrstream (ch_C (), length_i ());
51 istream_p_
= new istrstream ("", 0);
52 istream_p_
->set (ios::eofbit
);
59 Source_file::file_line_column_str (char const *context_ch_C
) const
62 return "(" + _ ("position unknown") + ")";
64 return name_str () + ":" + to_str (line_i (context_ch_C
))
65 + ":" + to_str (char_i (context_ch_C
));
69 Source_file::name_str () const
74 Source_file::~Source_file ()
82 Source_file::line_slice (char const* pos_ch_C
) const
87 char const* data_ch_C
= ch_C ();
88 char const * eof_C_
= data_ch_C
+ length_i ();
90 if (pos_ch_C
== eof_C_
)
92 char const* begin_ch_C
= pos_ch_C
;
93 while (begin_ch_C
> data_ch_C
)
94 if (*--begin_ch_C
== '\n')
100 char const* end_ch_C
= pos_ch_C
;
101 while (end_ch_C
< eof_C_
)
102 if (*end_ch_C
++ == '\n')
108 return Slice (begin_ch_C
- data_ch_C
, end_ch_C
- data_ch_C
);
112 Source_file::line_str (char const* pos_ch_C
) const
114 if (!in_b (pos_ch_C
))
117 Slice line
= line_slice (pos_ch_C
);
118 char const* data_ch_C
= ch_C ();
119 return String ((Byte
const*)data_ch_C
+ line
[LEFT
], line
.length ());
123 Source_file::char_i (char const* pos_ch_C
) const
125 if (!in_b (pos_ch_C
))
128 char const* data_ch_C
= ch_C ();
129 return pos_ch_C
- (line_slice (pos_ch_C
)[SMALLER
] + data_ch_C
);
133 Source_file::column_i (char const* pos_ch_C
) const
135 if (!in_b (pos_ch_C
))
138 int ch_i
= char_i (pos_ch_C
);
139 String line
= line_str (pos_ch_C
);
142 for (int i
= 0; i
< ch_i
; i
++)
144 col_i
= (col_i
/ 8 + 1) * 8;
152 Source_file::error_str (char const* pos_ch_C
) const
154 if (!in_b (pos_ch_C
))
155 return "(" + _ ("position unknown") + ")";
157 int ch_i
= char_i (pos_ch_C
);
158 String line
= line_str (pos_ch_C
);
159 String context
= line
.left_str (ch_i
)
161 + to_str (' ', column_i (pos_ch_C
))
162 + line
.cut_str (ch_i
, INT_MAX
);
168 Source_file::in_b (char const* pos_ch_C
) const
170 return (pos_ch_C
&& (pos_ch_C
>= ch_C ()) && (pos_ch_C
<= ch_C () + length_i ()));
174 Source_file::line_i (char const* pos_ch_C
) const
176 if (!in_b (pos_ch_C
))
180 char const* scan_ch_C
= ch_C ();
184 while (scan_ch_C
< pos_ch_C
)
185 if (*scan_ch_C
++ == '\n')
191 Source_file::length_i () const
193 return storage_p_
->length_i ();
197 Source_file::ch_C () const
199 return storage_p_
->ch_C ();
203 Source_file::set_pos (char const * pos_ch_C
)
206 pos_ch_C_
= pos_ch_C
;
208 error (error_str (pos_ch_C
) + "invalid pos");
212 Source_file::seek_ch_C (int n
)
214 char const* new_ch_C
= ch_C () + n
;
216 new_ch_C
+= length_i ();
218 pos_ch_C_
= new_ch_C
;
220 error (error_str (new_ch_C
) + "seek past eof");
226 Source_file::forward_ch_C (int n
)
228 char const* old_pos_C
= pos_ch_C_
;
229 char const* new_ch_C
= pos_ch_C_
+ n
;
231 pos_ch_C_
= new_ch_C
;
233 error (error_str (new_ch_C
) + "forward past eof");
239 Source_file::get_str (int n
)
241 String
str ((Byte
const*)forward_ch_C (n
), n
);