2 source-file.cc -- implement Source_file
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2009 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #define _GLIBCXX_HAVE_MBSTATE_T
13 #endif /* GCC_MAJOR < 4 */
15 #include "source-file.hh"
20 #include <utf8/wchar.h> /* mbrtowc */
21 #else /* !HAVE_UTF8_WCHAR_H */
22 #include <cwchar> /* mbrtowc */
23 #endif /* HAVE_UTF8_WCHAR_H */
31 #define istringstream(x) istrstream (x, length ())
35 #include "file-name-map.hh"
36 #include "international.hh"
40 Source_file::load_stdin ()
44 while ((c
= fgetc (stdin
)) != EOF
)
45 characters_
.push_back (c
);
49 return contents of FILENAME. *Not 0-terminated!*
52 gulp_file (string filename
, int desired_size
)
54 /* "b" must ensure to open literally, avoiding text (CR/LF)
56 FILE *f
= fopen (filename
.c_str (), "rb");
59 warning (_f ("cannot open file: `%s'", filename
.c_str ()));
65 fseek (f
, 0, SEEK_END
);
66 int real_size
= ftell (f
);
67 int read_count
= real_size
;
70 read_count
= min (read_count
, desired_size
);
74 char *str
= new char[read_count
+ 1];
77 int bytes_read
= fread (str
, sizeof (char), read_count
, f
);
78 if (bytes_read
!= read_count
)
79 warning (_f ("expected to read %d characters, got %d", bytes_read
,
82 int filesize
= bytes_read
;
85 cxx_arr
.resize (filesize
);
87 copy (str
, str
+ filesize
, cxx_arr
.begin ());
103 Source_file::Source_file (string filename
, string data
)
109 characters_
.resize (data
.length ());
110 copy (data
.begin (), data
.end (), characters_
.begin ());
112 characters_
.push_back (0);
116 for (vsize i
= 0; i
< characters_
.size (); i
++)
117 if (characters_
[i
] == '\n')
118 newline_locations_
.push_back (&characters_
[0] + i
);
121 Source_file::Source_file (string filename_string
)
125 name_
= filename_string
;
127 if (filename_string
== "-")
131 characters_
= gulp_file (filename_string
, -1);
134 characters_
.push_back (0);
138 for (vsize i
= 0; i
< characters_
.size (); i
++)
139 if (characters_
[i
] == '\n')
140 newline_locations_
.push_back (&characters_
[0] + i
);
144 Source_file::init_port ()
146 SCM str
= scm_from_locale_string (c_str ());
147 str_port_
= scm_mkstrport (SCM_INUM0
, str
, SCM_OPN
| SCM_RDNG
, __FUNCTION__
);
148 scm_set_port_filename_x (str_port_
, ly_string2scm (name_
));
153 Source_file::get_istream ()
157 if (length ()) // can-t this be done without such a hack?
158 istream_
= new istringstream (c_str ());
161 istream_
= new istringstream ("");
162 istream_
->setstate (ios::eofbit
);
163 // istream_->set (ios::eofbit);
170 Source_file::file_line_column_string (char const *context_str0
) const
173 return " (" + _ ("position unknown") + ")";
177 get_counts (context_str0
, &l
, &ch
, &col
);
179 return name_string () + ":" + to_string (l
)
180 + ":" + to_string (col
);
185 Source_file::quote_input (char const *pos_str0
) const
187 if (!contains (pos_str0
))
188 return " (" + _ ("position unknown") + ")";
191 get_counts (pos_str0
, &l
, &ch
, &col
);
192 string line
= line_string (pos_str0
);
193 string context
= line
.substr (0, ch
)
195 + to_string (' ', col
)
196 + line
.substr (ch
, line
.length ()-ch
);
201 Source_file::name_string () const
203 return map_file_name (name_
);
206 Source_file::~Source_file ()
212 Source_file::line_slice (char const *pos_str0
) const
214 if (!contains (pos_str0
))
217 char const *data_str0
= c_str ();
218 char const *eof_C_
= data_str0
+ length ();
220 if (pos_str0
== eof_C_
)
222 char const *begin_str0
= pos_str0
;
223 while (begin_str0
> data_str0
)
224 if (*--begin_str0
== '\n')
230 char const *end_str0
= pos_str0
;
231 while (end_str0
< eof_C_
)
232 if (*end_str0
++ == '\n')
238 return Slice (begin_str0
- data_str0
, end_str0
- data_str0
);
242 Source_file::line_string (char const *pos_str0
) const
244 if (!contains (pos_str0
))
247 Slice line
= line_slice (pos_str0
);
248 char const *data_str0
= c_str ();
249 return string (data_str0
+ line
[LEFT
], line
.length ());
253 Source_file::get_counts (char const *pos_str0
,
262 if (!contains (pos_str0
))
265 *line_number
= get_line (pos_str0
);
267 Slice line
= line_slice (pos_str0
);
268 char const *data
= c_str ();
269 char const *line_start
= (char const *)data
+ line
[LEFT
];
271 ssize left
= (char const *) pos_str0
- line_start
;
272 string
line_begin (line_start
, left
);
273 char const *line_chars
= line_begin
.c_str ();
280 /* Initialize the state. */
281 memset (&state
, '\0', sizeof (state
));
286 FIXME, this is apparently locale dependent.
289 wchar_t multibyte
[2];
290 size_t thislen
= mbrtowc (multibyte
, line_chars
, left
, &state
);
293 #endif /* !HAVE_MBRTOWC */
295 /* Stop converting at invalid character;
296 this can mean we have read just the first part
297 of a valid character. */
298 if (thislen
== (size_t) -1)
301 /* We want to handle embedded NUL bytes
302 but the return value is 0. Correct this. */
306 if (thislen
== 1 && line_chars
[0] == '\t')
307 (*column
) = (*column
/ 8 + 1) * 8;
312 /* Advance past this character. */
313 line_chars
+= thislen
;
319 Source_file::contains (char const *pos_str0
) const
321 return (pos_str0
&& (pos_str0
>= c_str ()) && (pos_str0
<= c_str () + length ()));
325 Source_file::get_line (char const *pos_str0
) const
327 if (!contains (pos_str0
))
330 if (!newline_locations_
.size ())
333 /* this will find the '\n' character at the end of our line */
334 vsize lo
= lower_bound (newline_locations_
,
336 less
<char const*> ());
338 /* the return value will be indexed from 1 */
339 return lo
+ 1 + line_offset_
;
343 Source_file::set_line (char const *pos_str0
, int line
)
345 int current_line
= get_line (pos_str0
);
346 line_offset_
+= line
- current_line
;
348 assert (line
== get_line (pos_str0
));
352 Source_file::length () const
354 return characters_
.size ();
358 Source_file::c_str () const
360 return &characters_
[0];
364 Source_file::get_port () const
369 /****************************************************************/
371 #include "ly-smobs.icc"
373 IMPLEMENT_SMOBS (Source_file
);
374 IMPLEMENT_DEFAULT_EQUAL_P (Source_file
);
375 IMPLEMENT_TYPE_P (Source_file
, "ly:source-file?");
378 Source_file::mark_smob (SCM smob
)
380 Source_file
*sc
= (Source_file
*) SCM_CELL_WORD_1 (smob
);
382 return sc
->str_port_
;
387 Source_file::print_smob (SCM smob
, SCM port
, scm_print_state
*)
389 Source_file
*sc
= (Source_file
*) SCM_CELL_WORD_1 (smob
);
391 scm_puts ("#<Source_file ", port
);
392 scm_puts (sc
->name_
.c_str (), port
);
394 /* Do not print properties, that is too much hassle. */
395 scm_puts (" >", port
);