Fixes Issue 1504, allowing feather beam line breaking.
[lilypond/patrick.git] / lily / source-file.cc
blobb01676355840a1877bacc3b9439809cb195ce496
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2011 Jan Nieuwenhuizen <janneke@gnu.org>
5 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
21 #if GCC_MAJOR < 4
22 #define _GLIBCXX_HAVE_MBSTATE_T
23 #include <wchar.h>
24 #endif /* GCC_MAJOR < 4 */
26 #include "source-file.hh"
28 #include "config.hh"
30 #include <cstdio>
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;
40 #include "file-name-map.hh"
41 #include "international.hh"
42 #include "misc.hh"
43 #include "warn.hh"
45 void
46 Source_file::load_stdin ()
48 characters_.clear ();
49 int c;
50 while ((c = fgetc (stdin)) != EOF)
51 characters_.push_back ((char)c);
55 return contents of FILENAME. *Not 0-terminated!*
57 vector<char>
58 gulp_file (string filename, int desired_size)
60 /* "b" must ensure to open literally, avoiding text (CR/LF)
61 conversions. */
62 FILE *f = fopen (filename.c_str (), "rb");
63 if (!f)
65 warning (_f ("cannot open file: `%s'", filename.c_str ()));
67 vector<char> cxx_arr;
68 return cxx_arr;
71 fseek (f, 0, SEEK_END);
72 int real_size = ftell (f);
73 int read_count = real_size;
75 if (desired_size > 0)
76 read_count = min (read_count, desired_size);
78 rewind (f);
80 char *str = new char[read_count + 1];
81 str[read_count] = 0;
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;
90 vector<char> cxx_arr;
91 cxx_arr.resize (filesize);
93 copy (str, str + filesize, cxx_arr.begin ());
95 delete[] str;
96 return cxx_arr;
99 void
100 Source_file::init ()
102 istream_ = 0;
103 line_offset_ = 0;
104 str_port_ = SCM_EOL;
105 self_scm_ = SCM_EOL;
106 smobify_self ();
109 Source_file::Source_file (string filename, string data)
111 init ();
113 name_ = filename;
115 characters_.resize (data.length ());
116 copy (data.begin (), data.end (), characters_.begin ());
118 characters_.push_back (0);
120 init_port ();
122 for (vsize i = 0; i < characters_.size (); i++)
123 if (characters_[i] == '\n')
124 newline_locations_.push_back (&characters_[0] + i);
127 Source_file::Source_file (string filename_string)
129 init ();
131 name_ = filename_string;
133 if (filename_string == "-")
134 load_stdin ();
135 else
137 characters_ = gulp_file (filename_string, -1);
140 characters_.push_back (0);
142 init_port ();
144 for (vsize i = 0; i < characters_.size (); i++)
145 if (characters_[i] == '\n')
146 newline_locations_.push_back (&characters_[0] + i);
149 void
150 Source_file::init_port ()
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_));
158 istream *
159 Source_file::get_istream ()
161 if (!istream_)
163 if (length ()) // can-t this be done without such a hack?
164 istream_ = new istringstream (c_str ());
165 else
167 istream_ = new istringstream ("");
168 istream_->setstate (ios::eofbit);
169 // istream_->set (ios::eofbit);
172 return istream_;
175 string
176 Source_file::file_line_column_string (char const *context_str0) const
178 if (!c_str ())
179 return " (" + _ ("position unknown") + ")";
180 else
182 int l, ch, col, offset;
183 get_counts (context_str0, &l, &ch, &col, &offset);
185 return name_string () + ":" + to_string (l)
186 + ":" + to_string (col);
190 string
191 Source_file::quote_input (char const *pos_str0) const
193 if (!contains (pos_str0))
194 return " (" + _ ("position unknown") + ")";
196 int l, ch, col, offset;
197 get_counts (pos_str0, &l, &ch, &col, &offset);
198 string line = line_string (pos_str0);
199 string context = line.substr (0, offset)
200 + to_string ('\n')
201 + to_string (' ', col)
202 + line.substr (offset, line.length () - offset);
203 return context;
206 string
207 Source_file::name_string () const
209 return map_file_name (name_);
212 Source_file::~Source_file ()
214 delete istream_;
217 Slice
218 Source_file::line_slice (char const *pos_str0) const
220 if (!contains (pos_str0))
221 return Slice (0, 0);
223 char const *data_str0 = c_str ();
224 char const *eof_C_ = data_str0 + length ();
226 if (pos_str0 == eof_C_)
227 pos_str0--;
228 char const *begin_str0 = pos_str0;
229 while (begin_str0 > data_str0)
230 if (*--begin_str0 == '\n')
232 begin_str0++;
233 break;
236 char const *end_str0 = pos_str0;
237 while (end_str0 < eof_C_)
238 if (*end_str0++ == '\n')
240 end_str0--;
241 break;
244 return Slice (begin_str0 - data_str0, end_str0 - data_str0);
247 string
248 Source_file::line_string (char const *pos_str0) const
250 if (!contains (pos_str0))
251 return "";
253 Slice line = line_slice (pos_str0);
254 char const *data_str0 = c_str ();
255 return string (data_str0 + line[LEFT], line.length ());
258 void
259 Source_file::get_counts (char const *pos_str0,
260 int *line_number,
261 int *line_char,
262 int *column,
263 int *byte_offset) const
265 *line_number = 0;
267 if (!contains (pos_str0))
268 return;
270 *line_number = get_line (pos_str0);
272 Slice line = line_slice (pos_str0);
273 char const *data = c_str ();
274 char const *line_start = (char const *)data + line[LEFT];
276 ssize left = (char const *) pos_str0 - line_start;
277 string line_begin (line_start, left);
278 char const *line_chars = line_begin.c_str ();
280 *line_char = 0;
281 *column = 0;
282 *byte_offset = 0;
284 while (left > 0)
286 size_t thislen = utf8_char_len (*line_chars);
288 if (thislen == 1 && line_chars[0] == '\t')
289 (*column) = (*column / 8 + 1) * 8;
290 else
291 (*column)++;
293 (*line_char)++;
296 To have decent output in UTF-8 aware terminals,
297 we must keep track of the number of bytes from
298 the left edge of the terminal.
300 *byte_offset += thislen;
302 /* Advance past this character. */
303 line_chars += thislen;
304 left -= thislen;
308 bool
309 Source_file::contains (char const *pos_str0) const
311 return (pos_str0 && (pos_str0 >= c_str ()) && (pos_str0 <= c_str () + length ()));
315 Source_file::get_line (char const *pos_str0) const
317 if (!contains (pos_str0))
318 return 0;
320 if (!newline_locations_.size ())
321 return 1;
323 /* this will find the '\n' character at the end of our line */
324 vsize lo = lower_bound (newline_locations_,
325 pos_str0,
326 less<char const*> ());
328 /* the return value will be indexed from 1 */
329 return lo + 1 + line_offset_;
332 void
333 Source_file::set_line (char const *pos_str0, int line)
335 int current_line = get_line (pos_str0);
336 line_offset_ += line - current_line;
338 assert (line == get_line (pos_str0));
342 Source_file::length () const
344 return characters_.size ();
347 char const *
348 Source_file::c_str () const
350 return &characters_[0];
354 Source_file::get_port () const
356 return str_port_;
359 /****************************************************************/
361 #include "ly-smobs.icc"
363 IMPLEMENT_SMOBS (Source_file);
364 IMPLEMENT_DEFAULT_EQUAL_P (Source_file);
365 IMPLEMENT_TYPE_P (Source_file, "ly:source-file?");
368 Source_file::mark_smob (SCM smob)
370 Source_file *sc = (Source_file *) SCM_CELL_WORD_1 (smob);
372 return sc->str_port_;
377 Source_file::print_smob (SCM smob, SCM port, scm_print_state *)
379 Source_file *sc = (Source_file *) SCM_CELL_WORD_1 (smob);
381 scm_puts ("#<Source_file ", port);
382 scm_puts (sc->name_.c_str (), port);
384 /* Do not print properties, that is too much hassle. */
385 scm_puts (" >", port);
386 return 1;