X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fsource-file.cc;h=fc5b03483d78cc85582d04bf7ad576c9bb681a64;hb=dd237d4df77489170dc5c204a9ba58819a904dfa;hp=186ea1eaf83ff752ad7ab1032eb0dc58e14a1595;hpb=5c6c8edb1c4997bd3db10d3fd1d79bca2abe3d7e;p=lilypond.git diff --git a/lily/source-file.cc b/lily/source-file.cc index 186ea1eaf8..fc5b03483d 100644 --- a/lily/source-file.cc +++ b/lily/source-file.cc @@ -3,19 +3,24 @@ source file of the GNU LilyPond music typesetter - (c) 1997--2006 Jan Nieuwenhuizen + (c) 1997--2009 Jan Nieuwenhuizen Han-Wen Nienhuys */ +#if GCC_MAJOR < 4 +#define _GLIBCXX_HAVE_MBSTATE_T +#include +#endif /* GCC_MAJOR < 4 */ + #include "source-file.hh" #include "config.hh" #if HAVE_UTF8_WCHAR_H #include /* mbrtowc */ -#else +#else /* !HAVE_UTF8_WCHAR_H */ #include /* mbrtowc */ -#endif +#endif /* HAVE_UTF8_WCHAR_H */ #include @@ -38,10 +43,11 @@ Source_file::load_stdin () int c; while ((c = fgetc (stdin)) != EOF) characters_.push_back (c); - - characters_.push_back (0); } +/* + return contents of FILENAME. *Not 0-terminated!* + */ vector gulp_file (string filename, int desired_size) { @@ -50,7 +56,7 @@ gulp_file (string filename, int desired_size) FILE *f = fopen (filename.c_str (), "rb"); if (!f) { - warning (_f ("can't open file: `%s'", filename.c_str ())); + warning (_f ("cannot open file: `%s'", filename.c_str ())); vector cxx_arr; return cxx_arr; @@ -78,10 +84,9 @@ gulp_file (string filename, int desired_size) vector cxx_arr; cxx_arr.resize (filesize); - copy (str, str + filesize, cxx_arr.begin ()); - free (str); + delete[] str; return cxx_arr; } @@ -102,9 +107,10 @@ Source_file::Source_file (string filename, string data) name_ = filename; characters_.resize (data.length ()); - copy (data.begin (), data.end (), characters_.begin ()); + characters_.push_back (0); + init_port (); for (vsize i = 0; i < characters_.size (); i++) @@ -123,9 +129,10 @@ Source_file::Source_file (string filename_string) else { characters_ = gulp_file (filename_string, -1); - characters_.push_back (0); } + characters_.push_back (0); + init_port (); for (vsize i = 0; i < characters_.size (); i++) @@ -136,9 +143,9 @@ Source_file::Source_file (string filename_string) void Source_file::init_port () { - SCM str = scm_makfrom0str (c_str ()); + SCM str = scm_from_locale_string (c_str ()); str_port_ = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG, __FUNCTION__); - scm_set_port_filename_x (str_port_, scm_makfrom0str (name_.c_str ())); + scm_set_port_filename_x (str_port_, ly_string2scm (name_)); } @@ -186,7 +193,7 @@ Source_file::quote_input (char const *pos_str0) const string context = line.substr (0, ch) + to_string ('\n') + to_string (' ', col) - + line.substr (ch, line.length()-ch); + + line.substr (ch, line.length ()-ch); return context; } @@ -199,7 +206,6 @@ Source_file::name_string () const Source_file::~Source_file () { delete istream_; - istream_ = 0; } Slice @@ -276,12 +282,11 @@ Source_file::get_counts (char const *pos_str0, while (left > 0) { - wchar_t multibyte[2]; - /* FIXME, this is apparently locale dependent. */ #if HAVE_MBRTOWC + wchar_t multibyte[2]; size_t thislen = mbrtowc (multibyte, line_chars, left, &state); #else size_t thislen = 1; @@ -325,23 +330,13 @@ Source_file::get_line (char const *pos_str0) const if (!newline_locations_.size ()) return 1; - vsize lo = 0; - vsize hi = newline_locations_.size (); - - if (newline_locations_[lo] > pos_str0) - return 1; - - if (newline_locations_[hi - 1] < pos_str0) - return hi; - - binary_search_bounds (newline_locations_, - (char const*&)pos_str0, - default_compare, - &lo, &hi); + /* this will find the '\n' character at the end of our line */ + vsize lo = lower_bound (newline_locations_, + pos_str0, + less ()); - if (*pos_str0 == '\n') - lo--; - return lo + 2 + line_offset_; + /* the return value will be indexed from 1 */ + return lo + 1 + line_offset_; } void @@ -375,9 +370,9 @@ Source_file::get_port () const #include "ly-smobs.icc" -IMPLEMENT_SMOBS(Source_file); -IMPLEMENT_DEFAULT_EQUAL_P(Source_file); -IMPLEMENT_TYPE_P(Source_file, "ly:source-file?"); +IMPLEMENT_SMOBS (Source_file); +IMPLEMENT_DEFAULT_EQUAL_P (Source_file); +IMPLEMENT_TYPE_P (Source_file, "ly:source-file?"); SCM Source_file::mark_smob (SCM smob)