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