]> git.donarmstrong.com Git - lilypond.git/blob - lily/input.cc
6cc8a182a540bdc29ecfe96d763af0dcf70f42b3
[lilypond.git] / lily / input.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 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 "program-option.hh"
27 #include "source-file.hh"
28 #include "sources.hh"
29 #include "warn.hh"
30
31 Input::Input (Input const &i)
32 {
33   source_file_ = i.source_file_;
34   start_ = i.start_;
35   end_ = i.end_;
36 }
37
38 Input::Input ()
39 {
40   source_file_ = 0;
41   start_ = 0;
42   end_ = 0;
43 }
44
45 Input
46 Input::spot () const
47 {
48   return *this;
49 }
50
51 void
52 Input::set_spot (Input const &i)
53 {
54   *this = i;
55 }
56
57 void
58 Input::step_forward ()
59 {
60   if (end_ == start_)
61     end_++;
62   start_++;
63 }
64
65 void
66 Input::set_location (Input const &i_start, Input const &i_end)
67 {
68   source_file_ = i_start.source_file_;
69   start_ = i_start.start_;
70   end_ = i_end.end_;
71 }
72
73 /*
74   Produce GNU-compliant error message.  Correcting lilypond source is
75   such a breeze if you ('re edidor) know (s) the error column too
76
77   Format:
78
79   [file:line:column:][warning:]message
80 */
81 void
82 Input::message (string s) const
83 {
84   if (source_file_)
85     s = location_string () + ": " + s + "\n"
86         + source_file_->quote_input (start_) + "\n";
87   ::message (s);
88 }
89
90 void
91 Input::programming_error (string s) const
92 {
93   if (get_program_option ("warning-as-error"))
94     ::error (s);
95   else
96     {
97       message (_f ("programming error: %s", s.c_str ()));
98       message (_ ("continuing, cross fingers") + "\n");
99     }
100 }
101
102 void
103 Input::warning (string s) const
104 {
105   if (get_program_option ("warning-as-error"))
106     ::error (s);
107   else
108     message (_f ("warning: %s", s));
109 }
110
111 void
112 Input::error (string s) const
113 {
114   message (_f ("error: %s", s));
115   // UGH, fix naming or usage
116   // exit (1);
117 }
118
119 void
120 Input::non_fatal_error (string s) const
121 {
122   message (_f ("error: %s", s));
123 }
124
125 string
126 Input::location_string () const
127 {
128   if (source_file_)
129     return source_file_->file_line_column_string (start_);
130   return " (" + _ ("position unknown") + ")";
131 }
132
133 string
134 Input::line_number_string () const
135 {
136   if (source_file_)
137     return to_string (source_file_->get_line (start_));
138   return "?";
139 }
140
141 string
142 Input::file_string () const
143 {
144   if (source_file_)
145     return source_file_->name_string ();
146   return "";
147 }
148
149 int
150 Input::line_number () const
151 {
152   if (source_file_)
153     return source_file_->get_line (start_);
154   return 0;
155 }
156
157 int
158 Input::column_number () const
159 {
160   int line, chr, col, offset = 0;
161   source_file_->get_counts (start_, &line, &chr, &col, &offset);
162
163   return col;
164 }
165
166 int
167 Input::end_line_number () const
168 {
169   if (source_file_)
170     return source_file_->get_line (end_);
171   return 0;
172 }
173
174 int
175 Input::end_column_number () const
176 {
177   int line, chr, col, offset = 0;
178   source_file_->get_counts (end_, &line, &chr, &col, &offset);
179
180   return col;
181 }
182
183 void
184 Input::get_counts (int *line, int *chr, int *col, int *offset) const
185 {
186   source_file_->get_counts (start_, line, chr, col, offset);
187 }
188
189 void
190 Input::set (Source_file *sf, char const *start, char const *end)
191 {
192   source_file_ = sf;
193   start_ = start;
194   end_ = end;
195 }
196
197 Source_file *
198 Input::get_source_file () const
199 {
200   return source_file_;
201 }
202
203 char const *
204 Input::start () const
205 {
206   return start_;
207 }
208
209 char const *
210 Input::end () const
211 {
212   return end_;
213 }