]> git.donarmstrong.com Git - lilypond.git/blob - lily/source-file.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / source-file.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 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   self_scm_ = SCM_EOL;
106   smobify_self ();
107 }
108
109 Source_file::Source_file (const string &filename, const string &data)
110 {
111   init ();
112
113   name_ = filename;
114
115   characters_.resize (data.length ());
116   copy (data.begin (), data.end (), characters_.begin ());
117
118   characters_.push_back (0);
119
120   init_port ();
121
122   for (vsize i = 0; i < characters_.size (); i++)
123     if (characters_[i] == '\n')
124       newline_locations_.push_back (&characters_[0] + i);
125 }
126
127 Source_file::Source_file (const string &filename_string)
128 {
129   init ();
130
131   name_ = filename_string;
132
133   if (filename_string == "-")
134     load_stdin ();
135   else
136     {
137       characters_ = gulp_file (filename_string, -1);
138     }
139
140   characters_.push_back (0);
141
142   init_port ();
143
144   for (vsize i = 0; i < characters_.size (); i++)
145     if (characters_[i] == '\n')
146       newline_locations_.push_back (&characters_[0] + i);
147 }
148
149 void
150 Source_file::init_port ()
151 {
152   SCM str = scm_from_locale_string (c_str ());
153   str_port_ = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG, __FUNCTION__);
154   scm_set_port_filename_x (str_port_, ly_string2scm (name_));
155 }
156
157 istream *
158 Source_file::get_istream ()
159 {
160   if (!istream_)
161     {
162       if (length ()) // can-t this be done without such a hack?
163         istream_ = new istringstream (c_str ());
164       else
165         {
166           istream_ = new istringstream ("");
167           istream_->setstate (ios::eofbit);
168           //      istream_->set (ios::eofbit);
169         }
170     }
171   return istream_;
172 }
173
174 string
175 Source_file::file_line_column_string (char const *context_str0) const
176 {
177   if (!c_str ())
178     return " (" + _ ("position unknown") + ")";
179   else
180     {
181       int l, ch, col, offset;
182       get_counts (context_str0, &l, &ch, &col, &offset);
183
184       return name_string () + ":" + ::to_string (l)
185              + ":" + ::to_string (col + 1);
186     }
187 }
188
189 string
190 Source_file::quote_input (char const *pos_str0) const
191 {
192   if (!contains (pos_str0))
193     return " (" + _ ("position unknown") + ")";
194
195   int l, ch, col, offset;
196   get_counts (pos_str0, &l, &ch, &col, &offset);
197   string line = line_string (pos_str0);
198   string context = line.substr (0, offset)
199                    + ::to_string ('\n')
200                    + ::to_string (' ', col)
201                    + line.substr (offset, line.length () - offset);
202   return context;
203 }
204
205 string
206 Source_file::name_string () const
207 {
208   return map_file_name (name_);
209 }
210
211 Source_file::~Source_file ()
212 {
213   delete istream_;
214 }
215
216 Slice
217 Source_file::line_slice (char const *pos_str0) const
218 {
219   if (!contains (pos_str0))
220     return Slice (0, 0);
221
222   char const *data_str0 = c_str ();
223   char const *eof_C_ = data_str0 + length ();
224
225   if (pos_str0 == eof_C_)
226     pos_str0--;
227   char const *begin_str0 = pos_str0;
228   while (begin_str0 > data_str0)
229     if (*--begin_str0 == '\n')
230       {
231         begin_str0++;
232         break;
233       }
234
235   char const *end_str0 = pos_str0;
236   while (end_str0 < eof_C_)
237     if (*end_str0++ == '\n')
238       {
239         end_str0--;
240         break;
241       }
242
243   return Slice (begin_str0 - data_str0, end_str0 - data_str0);
244 }
245
246 string
247 Source_file::line_string (char const *pos_str0) const
248 {
249   if (!contains (pos_str0))
250     return "";
251
252   Slice line = line_slice (pos_str0);
253   char const *data_str0 = c_str ();
254   return string (data_str0 + line[LEFT], line.length ());
255 }
256
257 void
258 Source_file::get_counts (char const *pos_str0,
259                          int *line_number,
260                          int *line_char,
261                          int *column,
262                          int *byte_offset) const
263 {
264   // Initialize arguments to defaults, needed if pos_str0 is not in source
265   *line_number = 0;
266   *line_char = 0;
267   *column = 0;
268   *byte_offset = 0;
269
270   if (!contains (pos_str0))
271     return;
272
273   *line_number = get_line (pos_str0);
274
275   Slice line = line_slice (pos_str0);
276   char const *data = c_str ();
277   char const *line_start = (char const *)data + line[LEFT];
278
279   ssize left = (char const *) pos_str0 - line_start;
280   *byte_offset = left;
281
282   string line_begin (line_start, left);
283   char const *line_chars = line_begin.c_str ();
284
285   for (; left > 0; --left, ++line_chars)
286     {
287       // Skip UTF-8 continuation bytes.  This is simplistic but
288       // robust, and we warn against non-UTF-8 input in the lexer
289       // already.  In the case of non-UTF-8 or of this function being
290       // called in mid-character, the results are somewhat arbitrary,
291       // but there is no really sane definition anyway.
292       if ((*line_chars & 0xc0) == 0x80)
293         continue;
294
295       if (*line_chars == '\t')
296         (*column) = (*column / 8 + 1) * 8;
297       else
298         (*column)++;
299
300       (*line_char)++;
301     }
302 }
303
304 bool
305 Source_file::contains (char const *pos_str0) const
306 {
307   return (pos_str0 && (pos_str0 >= c_str ()) && (pos_str0 <= c_str () + length ()));
308 }
309
310 int
311 Source_file::get_line (char const *pos_str0) const
312 {
313   if (!contains (pos_str0))
314     return 0;
315
316   if (!newline_locations_.size ())
317     return 1 + line_offset_;
318
319   /* this will find the '\n' character at the end of our line */
320   vsize lo = lower_bound (newline_locations_,
321                           pos_str0,
322                           less<char const *> ());
323
324   /* the return value will be indexed from 1 */
325   return lo + 1 + line_offset_;
326 }
327
328 void
329 Source_file::set_line (char const *pos_str0, int line)
330 {
331   if (pos_str0)
332     {
333       int current_line = get_line (pos_str0);
334       line_offset_ += line - current_line;
335
336       assert (line == get_line (pos_str0));
337     }
338   else
339     line_offset_ = line;
340 }
341
342 int
343 Source_file::length () const
344 {
345   return characters_.size ();
346 }
347
348 char const *
349 Source_file::c_str () const
350 {
351   return &characters_[0];
352 }
353
354 SCM
355 Source_file::get_port () const
356 {
357   return str_port_;
358 }
359
360 /****************************************************************/
361
362 #include "ly-smobs.icc"
363
364 IMPLEMENT_SMOBS (Source_file);
365 IMPLEMENT_DEFAULT_EQUAL_P (Source_file);
366 IMPLEMENT_TYPE_P (Source_file, "ly:source-file?");
367
368 SCM
369 Source_file::mark_smob (SCM smob)
370 {
371   Source_file *sc = (Source_file *) SCM_CELL_WORD_1 (smob);
372
373   return sc->str_port_;
374 }
375
376 int
377 Source_file::print_smob (SCM smob, SCM port, scm_print_state *)
378 {
379   Source_file *sc = (Source_file *) SCM_CELL_WORD_1 (smob);
380
381   scm_puts ("#<Source_file ", port);
382   scm_puts (sc->name_.c_str (), port);
383
384   /* Do not print properties, that is too much hassle.  */
385   scm_puts (" >", port);
386   return 1;
387 }
388