2 source-file.cc -- implement Source_file
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1998 Jan Nieuwenhuizen <janneke@gnu.org>
7 & Han-Wen Nienhuys <hanwen@cs.uu.nl>
12 #include <strstream.h>
18 #include "thank-you-cygnus.hh"
19 #include "source-file.hh"
20 #include "simple-file-storage.hh"
21 #include "string-storage.hh"
23 Source_file::Source_file (String filename_str)
25 name_str_ = filename_str;
27 storage_p_ = new Simple_file_storage (filename_str);
30 Source_file::Source_file (String name_str, String data_str)
34 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.min (), 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).min () + 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 ();