2 source-file.cc -- implement
4 source file of the GNU LilyPond music typesetter
6 (c) 1997 Jan Nieuwenhuizen <jan@digicash.com>
7 & Han-Wen Nienhuys <hanwen@stack.nl>
12 #include <strstream.h>
18 #include "windhoos-suck-suck-suck-thank-you-cygnus.hh"
19 #include "source-file.hh"
20 #include "file-storage.hh"
22 Source_file::Source_file( String filename_str )
24 name_str_ = filename_str;
26 storage_p_ = new Simple_file_storage(filename_str);
30 Source_file::istream_l()
32 if ( !name_str_.length_i())
36 if ( length_i() ) // can-t this be done without such a hack?
37 istream_p_ = new istrstream( ch_C(), length_i() );
39 istream_p_ = new istrstream( "", 0 );
40 istream_p_->set(ios::eofbit);
47 Source_file::file_line_no_str(char const *ch_C )const
49 return name_str() + ": "
50 + String( line_i( ch_C ) );
54 Source_file::name_str()const
58 Source_file::~Source_file()
65 Source_file::error_str( char const* pos_ch_C )const
67 char const* data_ch_C = ch_C();
68 char const * eof_C_ = data_ch_C + length_i();
69 if ( !in_b( pos_ch_C ) )
70 return "(position unknown)";
73 if ( pos_ch_C == eof_C_)
75 char const* begin_ch_C = pos_ch_C;
76 while ( begin_ch_C > data_ch_C )
77 if ( *--begin_ch_C == '\n' ) {
82 char const* end_ch_C = pos_ch_C;
83 while ( end_ch_C < eof_C_ )
84 if ( *end_ch_C++ == '\n' ) {
89 // String( char const* p, int length ) is missing!?
90 String line_str( (Byte const*)begin_ch_C, end_ch_C - begin_ch_C );
93 char const* scan_ch_C = begin_ch_C;
94 while ( scan_ch_C < pos_ch_C )
95 if ( *scan_ch_C++ == '\t' )
96 error_col_i = ( error_col_i / 8 + 1 ) * 8;
100 String str = line_str.left_str( pos_ch_C - begin_ch_C )
102 + String( ' ', error_col_i )
103 + line_str.mid_str( pos_ch_C - begin_ch_C, INT_MAX ); // String::mid should take 0 arg..
108 Source_file::in_b( char const* pos_ch_C )const
110 return ( pos_ch_C && ( pos_ch_C >= ch_C() ) && ( pos_ch_C <= ch_C() + length_i() ) );
115 Source_file::line_i( char const* pos_ch_C )const
117 if ( !in_b( pos_ch_C ) )
121 char const* scan_ch_C = ch_C();
122 while ( scan_ch_C < pos_ch_C )
123 if ( *scan_ch_C++ == '\n' )
129 Source_file::length_i()const
131 return storage_p_->length_i();
135 Source_file::ch_C()const
137 return storage_p_->ch_C();