5 #include <sys/types.h> // open, mmap
6 #include <sys/stat.h> // open
7 #include <sys/mman.h> // mmap
8 #include <limits.h> // INT_MAX
9 #include <fcntl.h> // open
10 #include <unistd.h> // close, stat
11 #include <stdio.h> // fdopen
12 #include <string.h> // strerror
13 #include <errno.h> // errno
15 #include <strstream.h>
24 #include "windhoos-suck-suck-suck-thank-you-cygnus.hh"
25 // #include "parseconstruct.hh" // defined_ch_c_l
26 extern char const* defined_ch_c_l;
29 // #include "main.hh" // find_file
30 String find_file(String);
32 #include "source-file.hh"
34 Source_file::Source_file( String &filename_str )
39 name_str_ = filename_str;
44 // ugh!?, should call name_str() !
45 filename_str = name_str_;
48 Source_file::~Source_file()
60 return (char const*)data_caddr_;
73 Source_file::error_str( char const* pos_ch_c_l )
76 if ( !in_b( pos_ch_c_l ) )
79 char const* begin_ch_c_l = pos_ch_c_l;
80 char const* data_ch_c_l = ch_c_l();
81 while ( begin_ch_c_l > data_ch_c_l )
82 if ( *--begin_ch_c_l == '\n' ) {
87 char const* end_ch_c_l = pos_ch_c_l;
88 while ( end_ch_c_l < data_ch_c_l + size_off_ )
89 if ( *end_ch_c_l++ == '\n' ) {
95 // String( char const* p, int length ) is missing!?
96 String line_str( (Byte const*)begin_ch_c_l, end_ch_c_l - begin_ch_c_l );
98 int length_i = end_ch_c_l - begin_ch_c_l;
99 char* ch_p = new char[ length_i + 1 ];
100 strncpy( ch_p, begin_ch_c_l, length_i );
101 ch_p[ length_i ] = 0;
102 String line_str( ch_p );
107 char const* scan_ch_c_l = begin_ch_c_l;
108 while ( scan_ch_c_l < pos_ch_c_l )
109 if ( *scan_ch_c_l++ == '\t' )
110 error_col_i = ( error_col_i / 8 + 1 ) * 8;
114 String str = line_str.left_str( pos_ch_c_l - begin_ch_c_l )
116 + String( ' ', error_col_i )
117 + line_str.mid_str( pos_ch_c_l - begin_ch_c_l, INT_MAX ); // String::mid should take 0 arg..
122 Source_file::in_b( char const* pos_ch_c_l )
124 return ( pos_ch_c_l && ( pos_ch_c_l >= ch_c_l() ) && ( pos_ch_c_l < ch_c_l() + size_off_ ) );
128 Source_file::istream_l()
132 if ( size_off_ ) // can-t this be done without such a hack?
133 istream_p_ = new istrstream( ch_c_l(), size_off_ );
135 istream_p_ = new istrstream( "", 0 );
136 istream_p_->set(ios::eofbit);
143 Source_file::length_off()
149 Source_file::line_i( char const* pos_ch_c_l )
151 if ( !in_b( pos_ch_c_l ) )
155 char const* scan_ch_c_l = ch_c_l();
156 while ( scan_ch_c_l < pos_ch_c_l )
157 if ( *scan_ch_c_l++ == '\n' )
165 if ( fildes_i_ == -1 )
168 data_caddr_ = (caddr_t)mmap( (void*)0, size_off_, PROT_READ, MAP_SHARED, fildes_i_, 0 );
170 if ( (int)data_caddr_ == -1 )
171 // ugh: defined_ch_c_l...
172 warning( String( "can't map: " ) + name_str_ + String( ": " ) + strerror( errno ), defined_ch_c_l ); //lexer->here_ch_c_l() );
176 Source_file::name_str()
184 String name_str = find_file( name_str_ );
185 if ( name_str != "" )
186 name_str_ = name_str;
188 fildes_i_ = ::open( name_str_, O_RDONLY );
190 if ( fildes_i_ == -1 ) {
191 warning( String( "can't open: " ) + name_str_ + String( ": " ) + strerror( errno ), defined_ch_c_l ); // lexer->here_ch_c_l() );
195 struct stat file_stat;
196 fstat( fildes_i_, &file_stat );
197 size_off_ = file_stat.st_size;
204 munmap( data_caddr_, size_off_ );
210 Source_file::file_line_no_str(char const *ch_c_l )
212 return name_str() + ": "
213 + String( line_i( ch_c_l ) );