]> git.donarmstrong.com Git - lilypond.git/blob - lily/input.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / input.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "input.hh"
21
22 #include <cstdio>
23 using namespace std;
24
25 #include "international.hh"
26 #include "lily-imports.hh"
27 #include "program-option.hh"
28 #include "source-file.hh"
29 #include "sources.hh"
30 #include "warn.hh"
31
32 using std::string;
33
34 Input::Input (Input const &i)
35 {
36   source_file_ = i.source_file_;
37   start_ = i.start_;
38   end_ = i.end_;
39 }
40
41 Input::Input ()
42 {
43   source_file_ = 0;
44   start_ = 0;
45   end_ = 0;
46 }
47
48 Input
49 Input::spot () const
50 {
51   return *this;
52 }
53
54 void
55 Input::set_spot (Input const &i)
56 {
57   *this = i;
58 }
59
60 void
61 Input::step_forward ()
62 {
63   if (end_ == start_)
64     end_++;
65   start_++;
66 }
67
68 void
69 Input::set_location (Input const &i_start, Input const &i_end)
70 {
71   source_file_ = i_start.source_file_;
72   start_ = i_start.start_;
73   end_ = i_end.end_;
74 }
75
76 /*
77   Produce GNU-compliant error message.  Correcting lilypond source is
78   such a breeze if you ('re edidor) know (s) the error column too
79
80   Format:
81
82   [file:line:column:][warning:]message
83 */
84 string
85 Input::message_string (const string &msg) const
86 {
87   if (source_file_)
88     return msg + "\n" + source_file_->quote_input (start_);
89   else
90     return msg;
91 }
92
93 string
94 Input::message_location () const
95 {
96   return (source_file_) ? location_string () : "";
97 }
98
99 void
100 Input::error (const string &s) const
101 {
102   ::error (message_string (s), message_location ());
103 }
104
105 void
106 Input::programming_error (const string &s) const
107 {
108   ::programming_error (message_string (s), message_location ());
109 }
110
111 void
112 Input::non_fatal_error (const string &s) const
113 {
114   ::non_fatal_error (message_string (s), message_location ());
115 }
116
117 void
118 Input::warning (const string &s) const
119 {
120   ::warning (message_string (s), message_location ());
121 }
122
123 void
124 Input::message (const string &s) const
125 {
126   ::message (message_string (s), true, message_location ());
127 }
128
129 void
130 Input::debug_output (const string &s) const
131 {
132   ::debug_output (message_string (s), true, message_location ());
133 }
134
135 string
136 Input::location_string () const
137 {
138   if (source_file_)
139     return source_file_->file_line_column_string (start_);
140   return " (" + _ ("position unknown") + ")";
141 }
142
143 string
144 Input::line_number_string () const
145 {
146   if (source_file_)
147     return ::to_string (source_file_->get_line (start_));
148   return "?";
149 }
150
151 string
152 Input::file_string () const
153 {
154   if (source_file_)
155     return source_file_->name_string ();
156   return "";
157 }
158
159 int
160 Input::line_number () const
161 {
162   if (source_file_)
163     return source_file_->get_line (start_);
164   return 0;
165 }
166
167 int
168 Input::column_number () const
169 {
170   int line, chr, col, offset = 0;
171   source_file_->get_counts (start_, &line, &chr, &col, &offset);
172
173   return col;
174 }
175
176 int
177 Input::end_line_number () const
178 {
179   if (source_file_)
180     return source_file_->get_line (end_);
181   return 0;
182 }
183
184 int
185 Input::end_column_number () const
186 {
187   int line, chr, col, offset = 0;
188   source_file_->get_counts (end_, &line, &chr, &col, &offset);
189
190   return col;
191 }
192
193 void
194 Input::get_counts (int *line, int *chr, int *col, int *offset) const
195 {
196   source_file_->get_counts (start_, line, chr, col, offset);
197 }
198
199 void
200 Input::set (Source_file *sf, char const *start, char const *end)
201 {
202   source_file_ = sf;
203   start_ = start;
204   end_ = end;
205 }
206
207 Source_file *
208 Input::get_source_file () const
209 {
210   return source_file_;
211 }
212
213 char const *
214 Input::start () const
215 {
216   return start_;
217 }
218
219 char const *
220 Input::end () const
221 {
222   return end_;
223 }
224
225 static SCM
226 with_location_hook_0 (void *it)
227 {
228   SCM *args = static_cast <SCM *> (it);
229   return scm_call_0 (args[0]);
230 }
231
232 SCM
233 with_location (SCM loc, SCM proc)
234 {
235   return scm_c_with_fluid (Lily::f_location,
236                            unsmob<Input> (loc) ? loc : SCM_BOOL_F,
237                            with_location_hook_0,
238                            static_cast <void *> (&proc));
239 }
240
241 static SCM
242 with_location_hook_1 (void *it)
243 {
244   SCM *args = static_cast <SCM *> (it);
245   return scm_call_1 (args[0], args[1]);
246 }
247
248 SCM
249 with_location (SCM loc, SCM proc, SCM arg1)
250 {
251   SCM args[] = { proc, arg1 };
252   return scm_c_with_fluid (Lily::f_location,
253                            unsmob<Input> (loc) ? loc : SCM_BOOL_F,
254                            with_location_hook_1,
255                            static_cast <void *> (&args));
256 }
257
258 static SCM
259 with_location_hook_2 (void *it)
260 {
261   SCM *args = static_cast <SCM *> (it);
262   return scm_call_2 (args[0], args[1], args[2]);
263 }
264
265 SCM
266 with_location (SCM loc, SCM proc, SCM arg1, SCM arg2)
267 {
268   SCM args[] = { proc, arg1, arg2 };
269   return scm_c_with_fluid (Lily::f_location,
270                            unsmob<Input> (loc) ? loc : SCM_BOOL_F,
271                            with_location_hook_2,
272                            static_cast <void *> (&args));
273 }
274
275 static SCM
276 with_location_hook_3 (void *it)
277 {
278   SCM *args = static_cast <SCM *> (it);
279   return scm_call_3 (args[0], args[1], args[2], args[3]);
280 }
281
282 SCM
283 with_location (SCM loc, SCM proc, SCM arg1, SCM arg2, SCM arg3)
284 {
285   SCM args[] = { proc, arg1, arg2, arg3 };
286   return scm_c_with_fluid (Lily::f_location,
287                            unsmob<Input> (loc) ? loc : SCM_BOOL_F,
288                            with_location_hook_3,
289                            static_cast <void *> (&args));
290 }
291
292 static SCM
293 with_location_hook_4 (void *it)
294 {
295   SCM *args = static_cast <SCM *> (it);
296   return scm_call_4 (args[0], args[1], args[2], args[3], args[4]);
297 }
298
299 SCM
300 with_location (SCM loc, SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4)
301 {
302   SCM args[] = { proc, arg1, arg2, arg3, arg4 };
303   return scm_c_with_fluid (Lily::f_location,
304                            unsmob<Input> (loc) ? loc : SCM_BOOL_F,
305                            with_location_hook_4,
306                            static_cast <void *> (&args));
307 }
308
309 static SCM
310 with_location_hook_n (void *it)
311 {
312   SCM *args = static_cast <SCM *> (it);
313   return scm_apply_0 (args[0], args[1]);
314 }
315
316 SCM
317 with_location (SCM loc, SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4, SCM arg5)
318 {
319   SCM args[] = { proc, scm_list_5 (arg1, arg2, arg3, arg4, arg5) };
320   return scm_c_with_fluid (Lily::f_location,
321                            unsmob<Input> (loc) ? loc : SCM_BOOL_F,
322                            with_location_hook_n,
323                            static_cast <void *> (&args));
324 }