]> git.donarmstrong.com Git - lilypond.git/blob - lily/source-file.cc
Run grand replace for 2015.
[lilypond.git] / lily / source-file.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Jan Nieuwenhuizen <janneke@gnu.org>
5   Han-Wen Nienhuys <hanwen@xs4all.nl>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #if GCC_MAJOR < 4
22 #define _GLIBCXX_HAVE_MBSTATE_T
23 #include <wchar.h>
24 #endif /* GCC_MAJOR < 4 */
25
26 #include "source-file.hh"
27
28 #include "config.hh"
29
30 #include <cstdio>
31
32 #if HAVE_SSTREAM
33 #include <sstream>
34 #else
35 #include <strstream>
36 #define istringstream(x) istrstream (x, length ())
37 #endif
38 using namespace std;
39
40 #include "file-name-map.hh"
41 #include "international.hh"
42 #include "misc.hh"
43 #include "warn.hh"
44
45 void
46 Source_file::load_stdin ()
47 {
48   characters_.clear ();
49   int c;
50   while ((c = fgetc (stdin)) != EOF)
51     characters_.push_back ((char)c);
52 }
53
54 /*
55   return contents of FILENAME. *Not 0-terminated!*
56  */
57 vector<char>
58 gulp_file (const string &filename, int desired_size)
59 {
60   /* "b" must ensure to open literally, avoiding text (CR/LF)
61      conversions.  */
62   FILE *f = fopen (filename.c_str (), "rb");
63   if (!f)
64     {
65       warning (_f ("cannot open file: `%s'", filename.c_str ()));
66
67       vector<char> cxx_arr;
68       return cxx_arr;
69     }
70
71   fseek (f, 0, SEEK_END);
72   int real_size = ftell (f);
73   int read_count = real_size;
74
75   if (desired_size > 0)
76     read_count = min (read_count, desired_size);
77
78   rewind (f);
79
80   char *str = new char[read_count + 1];
81   str[read_count] = 0;
82
83   int bytes_read = fread (str, sizeof (char), read_count, f);
84   if (bytes_read != read_count)
85     warning (_f ("expected to read %d characters, got %d", bytes_read,
86                  read_count));
87   fclose (f);
88   int filesize = bytes_read;
89
90   vector<char> cxx_arr;
91   cxx_arr.resize (filesize);
92
93   copy (str, str + filesize, cxx_arr.begin ());
94
95   delete[] str;
96   return cxx_arr;
97 }
98
99 void
100 Source_file::init ()
101 {
102   istream_ = 0;
103   line_offset_ = 0;
104   str_port_ = SCM_EOL;
105   smobify_self ();
106 }
107
108 Source_file::Source_file (const string &filename, const string &data)
109 {
110   init ();
111
112   name_ = filename;
113
114   characters_.resize (data.length ());
115   copy (data.begin (), data.end (), characters_.begin ());
116
117   characters_.push_back (0);
118
119   init_port ();
120
121   for (vsize i = 0; i < characters_.size (); i++)
122     if (characters_[i] == '\n')
123       newline_locations_.push_back (&characters_[0] + i);
124 }
125
126 Source_file::Source_file (const string &filename_string)
127 {
128   init ();
129
130   name_ = filename_string;
131
132   if (filename_string == "-")
133     load_stdin ();
134   else
135     {
136       characters_ = gulp_file (filename_string, -1);
137     }
138
139   characters_.push_back (0);
140
141   init_port ();
142
143   for (vsize i = 0; i < characters_.size (); i++)
144     if (characters_[i] == '\n')
145       newline_locations_.push_back (&characters_[0] + i);
146 }
147
148 void
149 Source_file::init_port ()
150 {
151   SCM str = scm_from_locale_string (c_str ());
152   str_port_ = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG, __FUNCTION__);
153   scm_set_port_filename_x (str_port_, ly_string2scm (name_));
154 }
155
156 istream *
157 Source_file::get_istream ()
158 {
159   if (!istream_)
160     {
161       if (length ()) // can-t this be done without such a hack?
162         istream_ = new istringstream (c_str ());
163       else
164         {
165           istream_ = new istringstream ("");
166           istream_->setstate (ios::eofbit);
167           //      istream_->set (ios::eofbit);
168         }
169     }
170   return istream_;
171 }
172
173 string
174 Source_file::file_line_column_string (char const *context_str0) const
175 {
176   if (!c_str ())
177     return " (" + _ ("position unknown") + ")";
178   else
179     {
180       int l, ch, col, offset;
181       get_counts (context_str0, &l, &ch, &col, &offset);
182
183       return name_string () + ":" + ::to_string (l)
184              + ":" + ::to_string (col + 1);
185     }
186 }
187
188 string
189 Source_file::quote_input (char const *pos_str0) const
190 {
191   if (!contains (pos_str0))
192     return " (" + _ ("position unknown") + ")";
193
194   int l, ch, col, offset;
195   get_counts (pos_str0, &l, &ch, &col, &offset);
196   string line = line_string (pos_str0);
197   string context = line.substr (0, offset)
198                    + ::to_string ('\n')
199                    + ::to_string (' ', col)
200                    + line.substr (offset, line.length () - offset);
201   return context;
202 }
203
204 string
205 Source_file::name_string () const
206 {
207   return map_file_name (name_);
208 }
209
210 Source_file::~Source_file ()
211 {
212   delete istream_;
213 }
214
215 Slice
216 Source_file::line_slice (char const *pos_str0) const
217 {
218   if (!contains (pos_str0))
219     return Slice (0, 0);
220
221   char const *data_str0 = c_str ();
222   char const *eof_C_ = data_str0 + length ();
223
224   if (pos_str0 == eof_C_)
225     pos_str0--;
226   char const *begin_str0 = pos_str0;
227   while (begin_str0 > data_str0)
228     if (*--begin_str0 == '\n')
229       {
230         begin_str0++;
231         break;
232       }
233
234   char const *end_str0 = pos_str0;
235   while (end_str0 < eof_C_)
236     if (*end_str0++ == '\n')
237       {
238         end_str0--;
239         break;
240       }
241
242   return Slice (begin_str0 - data_str0, end_str0 - data_str0);
243 }
244
245 string
246 Source_file::line_string (char const *pos_str0) const
247 {
248   if (!contains (pos_str0))
249     return "";
250
251   Slice line = line_slice (pos_str0);
252   char const *data_str0 = c_str ();
253   return string (data_str0 + line[LEFT], line.length ());
254 }
255
256 void
257 Source_file::get_counts (char const *pos_str0,
258                          int *line_number,
259                          int *line_char,
260                          int *column,
261                          int *byte_offset) const
262 {
263   // Initialize arguments to defaults, needed if pos_str0 is not in source
264   *line_number = 0;
265   *line_char = 0;
266   *column = 0;
267   *byte_offset = 0;
268
269   if (!contains (pos_str0))
270     return;
271
272   *line_number = get_line (pos_str0);
273
274   Slice line = line_slice (pos_str0);
275   char const *data = c_str ();
276   char const *line_start = (char const *)data + line[LEFT];
277
278   ssize left = (char const *) pos_str0 - line_start;
279   *byte_offset = left;
280
281   string line_begin (line_start, left);
282   char const *line_chars = line_begin.c_str ();
283
284   for (; left > 0; --left, ++line_chars)
285     {
286       // Skip UTF-8 continuation bytes.  This is simplistic but
287       // robust, and we warn against non-UTF-8 input in the lexer
288       // already.  In the case of non-UTF-8 or of this function being
289       // called in mid-character, the results are somewhat arbitrary,
290       // but there is no really sane definition anyway.
291       if ((*line_chars & 0xc0) == 0x80)
292         continue;
293
294       if (*line_chars == '\t')
295         (*column) = (*column / 8 + 1) * 8;
296       else
297         (*column)++;
298
299       (*line_char)++;
300     }
301 }
302
303 bool
304 Source_file::contains (char const *pos_str0) const
305 {
306   return (pos_str0 && (pos_str0 >= c_str ()) && (pos_str0 <= c_str () + length ()));
307 }
308
309 int
310 Source_file::get_line (char const *pos_str0) const
311 {
312   if (!contains (pos_str0))
313     return 0;
314
315   if (!newline_locations_.size ())
316     return 1 + line_offset_;
317
318   /* this will find the '\n' character at the end of our line */
319   vsize lo = lower_bound (newline_locations_,
320                           pos_str0,
321                           less<char const *> ());
322
323   /* the return value will be indexed from 1 */
324   return lo + 1 + line_offset_;
325 }
326
327 void
328 Source_file::set_line (char const *pos_str0, int line)
329 {
330   if (pos_str0)
331     {
332       int current_line = get_line (pos_str0);
333       line_offset_ += line - current_line;
334
335       assert (line == get_line (pos_str0));
336     }
337   else
338     line_offset_ = line;
339 }
340
341 int
342 Source_file::length () const
343 {
344   return characters_.size ();
345 }
346
347 char const *
348 Source_file::c_str () const
349 {
350   return &characters_[0];
351 }
352
353 SCM
354 Source_file::get_port () const
355 {
356   return str_port_;
357 }
358
359 /****************************************************************/
360
361
362 const char Source_file::type_p_name_[] = "ly:source-file?";
363
364 SCM
365 Source_file::mark_smob ()
366 {
367   return str_port_;
368 }
369
370 int
371 Source_file::print_smob (SCM port, scm_print_state *)
372 {
373   scm_puts ("#<Source_file ", port);
374   scm_puts (name_.c_str (), port);
375
376   /* Do not print properties, that is too much hassle.  */
377   scm_puts (" >", port);
378   return 1;
379 }