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