]> git.donarmstrong.com Git - lilypond.git/blob - lily/input.cc
release commit
[lilypond.git] / lily / input.cc
1 /*
2   input.cc -- implement Input
3
4   source file of the LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "input.hh"
10
11 #include <cstdio>
12 using namespace std;
13
14 #include "source.hh"
15 #include "source-file.hh"
16 #include "warn.hh"
17
18 Input::Input (Input const &i)
19 {
20   source_file_ = i.source_file_;
21   start_ = i.start_;
22   end_ = i.end_;
23 }
24
25 Input::Input ()
26 {
27   source_file_ = 0;
28   start_ = 0;
29   end_ = 0;
30 }
31
32 Input
33 Input::spot () const
34 {
35   return *this;
36 }
37
38 void
39 Input::set_spot (Input const &i)
40 {
41   *this = i;
42 }
43
44 void
45 Input::step_forward ()
46 {
47   if (end_ == start_)
48     end_++;
49   start_++;
50 }
51
52 void
53 Input::set_location (Input const &i_start, Input const &i_end)
54 {
55   source_file_ = i_start.source_file_;
56   start_ = i_start.start_;
57   end_ = i_end.end_;
58 }
59
60 /*
61   Produce GNU-compliant error message.  Correcting lilypond source is
62   such a breeze if you ('re edidor) know (s) the error column too
63
64   Format:
65
66   [file:line:column:][warning:]message
67 */
68 void
69 Input::message (String s) const
70 {
71   if (source_file_)
72     s = location_string () + ": " + s + "\n"
73       + source_file_->quote_input (start_);
74   ::message (s);
75 }
76
77 void
78 Input::warning (String s) const
79 {
80   message (_f ("warning: %s", s));
81 }
82
83 void
84 Input::error (String s) const
85 {
86   message (_f ("error: %s", s));
87   // UGH, fix naming or usage
88   // exit (1);
89 }
90
91 void
92 Input::non_fatal_error (String s) const
93 {
94   message (_f ("error: %s", s));
95 }
96
97 String
98 Input::location_string () const
99 {
100   if (source_file_)
101     return source_file_->file_line_column_string (start_);
102   return " (" + _ ("position unknown") + ")";
103 }
104
105 String
106 Input::line_number_string () const
107 {
108   if (source_file_)
109     return to_string (source_file_->get_line (start_));
110   return "?";
111 }
112
113 String
114 Input::file_string () const
115 {
116   if (source_file_)
117     return source_file_->name_string ();
118   return "";
119 }
120
121 int
122 Input::line_number () const
123 {
124   if (source_file_)
125     return source_file_->get_line (start_);
126   return 0;
127 }
128
129 int
130 Input::column_number () const
131 {
132   int line, chr, col = 0;
133   source_file_->get_counts (start_, &line, &chr, &col);
134
135   return col;
136 }
137
138 int
139 Input::end_line_number () const
140 {
141   if (source_file_)
142     return source_file_->get_line (end_);
143   return 0;
144 }
145
146 int
147 Input::end_column_number () const
148 {
149   int line, chr, col = 0;
150   source_file_->get_counts (end_, &line, &chr, &col);
151
152   return col;
153 }
154
155 void
156 Input::get_counts (int *line, int *chr, int *col) const
157 {
158   source_file_->get_counts (start_, line, chr, col);
159 }
160
161 void
162 Input::set (Source_file *sf, char const *start, char const *end)
163 {
164   source_file_ = sf;
165   start_ = start;
166   end_ = end;
167 }
168
169 Source_file *
170 Input::get_source_file () const
171 {
172   return source_file_;
173 }
174
175 char const *
176 Input::start () const
177 {
178   return start_;
179 }
180
181 char const *
182 Input::end () const
183 {
184   return end_;
185 }