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>
23 #include "windhoos-suck-suck-suck-thank-you-cygnus.hh"
25 #include "source-file.hh"
27 Source_file::Source_file( String filename_str )
32 name_str_ = filename_str;
40 Source_file::istream_l()
44 if ( size_off_ ) // can-t this be done without such a hack?
45 istream_p_ = new istrstream( ch_C(), size_off_ );
47 istream_p_ = new istrstream( "", 0 );
48 istream_p_->set(ios::eofbit);
54 Source_file::~Source_file()
66 return (char const*)data_caddr_;
79 Source_file::error_str( char const* pos_ch_C )
81 char const* data_ch_C = ch_C();
82 char const * eof_C_ = data_ch_C + size_off_;
83 if ( !in_b( pos_ch_C ) )
84 return "(position unknown)";
87 if ( pos_ch_C == eof_C_)
89 char const* begin_ch_C = pos_ch_C;
90 while ( begin_ch_C > data_ch_C )
91 if ( *--begin_ch_C == '\n' ) {
96 char const* end_ch_C = pos_ch_C;
97 while ( end_ch_C < eof_C_ )
98 if ( *end_ch_C++ == '\n' ) {
103 // String( char const* p, int length ) is missing!?
104 String line_str( (Byte const*)begin_ch_C, end_ch_C - begin_ch_C );
107 char const* scan_ch_C = begin_ch_C;
108 while ( scan_ch_C < pos_ch_C )
109 if ( *scan_ch_C++ == '\t' )
110 error_col_i = ( error_col_i / 8 + 1 ) * 8;
114 String str = line_str.left_str( pos_ch_C - begin_ch_C )
116 + String( ' ', error_col_i )
117 + line_str.mid_str( pos_ch_C - begin_ch_C, INT_MAX ); // String::mid should take 0 arg..
122 Source_file::in_b( char const* pos_ch_C )
124 return ( pos_ch_C && ( pos_ch_C >= ch_C() ) && ( pos_ch_C <= ch_C() + size_off_ ) );
128 Source_file::length_off()
134 Source_file::line_i( char const* pos_ch_C )
136 if ( !in_b( pos_ch_C ) )
140 char const* scan_ch_C = ch_C();
141 while ( scan_ch_C < pos_ch_C )
142 if ( *scan_ch_C++ == '\n' )
150 if ( fildes_i_ == -1 )
153 data_caddr_ = (caddr_t)mmap( (void*)0, size_off_, PROT_READ, MAP_SHARED, fildes_i_, 0 );
155 if ( (int)data_caddr_ == -1 )
156 warning( String( "can't map: " ) + name_str_ + String( ": " ) + strerror( errno ));
160 Source_file::name_str()
168 fildes_i_ = ::open( name_str_, O_RDONLY );
170 if ( fildes_i_ == -1 ) {
171 warning( String( "can't open: " ) + name_str_ + String( ": " ) + strerror( errno ));
175 struct stat file_stat;
176 fstat( fildes_i_, &file_stat );
177 size_off_ = file_stat.st_size;
184 munmap( data_caddr_, size_off_ );
190 Source_file::file_line_no_str(char const *ch_C )
192 return name_str() + ": "
193 + String( line_i( ch_C ) );