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>
22 #include "parseconstruct.hh"
23 #include "main.hh" // find_file
25 #include "sourcefile.hh"
27 Source_file::Source_file( String &filename_str )
32 name_str_ = filename_str;
37 filename_str = name_str_;
40 Source_file::~Source_file()
52 return (char const*)data_caddr_;
65 Source_file::error_str( char const* pos_ch_c_l )
68 if ( !in_b( pos_ch_c_l ) )
71 char const* begin_ch_c_l = pos_ch_c_l;
72 char const* data_ch_c_l = ch_c_l();
73 while ( begin_ch_c_l > data_ch_c_l )
74 if ( *--begin_ch_c_l == '\n' ) {
79 char const* end_ch_c_l = pos_ch_c_l;
80 while ( end_ch_c_l < data_ch_c_l + size_off_ )
81 if ( *end_ch_c_l++ == '\n' ) {
86 // String( char const* p, int length ) is missing!?
87 // String line_str( begin_ch_c_l, end_ch_c_l - begin_ch_c_l );
89 int length_i = end_ch_c_l - begin_ch_c_l;
90 char* ch_p = new char[ length_i + 1 ];
91 strncpy( ch_p, begin_ch_c_l, length_i );
93 String line_str( ch_p );
97 char const* scan_ch_c_l = begin_ch_c_l;
98 while ( scan_ch_c_l < pos_ch_c_l )
99 if ( *scan_ch_c_l++ == '\t' )
100 error_col_i = ( error_col_i / 8 + 1 ) * 8;
104 String str = line_str.left( pos_ch_c_l - begin_ch_c_l )
106 + String( ' ', error_col_i )
107 + line_str.mid( pos_ch_c_l - begin_ch_c_l + 1, INT_MAX ); // String::mid should take 0 arg..
112 Source_file::in_b( char const* pos_ch_c_l )
114 return ( pos_ch_c_l && ( pos_ch_c_l >= ch_c_l() ) && ( pos_ch_c_l < ch_c_l() + size_off_ ) );
118 Source_file::istream_l()
122 if ( size_off_ ) // can-t this be done without such a hack?
123 istream_p_ = new istrstream( ch_c_l(), size_off_ );
125 istream_p_ = new istrstream( "", 0 );
126 istream_p_->set(ios::eofbit);
133 Source_file::line_i( char const* pos_ch_c_l )
135 if ( !in_b( pos_ch_c_l ) )
139 char const* scan_ch_c_l = ch_c_l();
140 while ( scan_ch_c_l < pos_ch_c_l )
141 if ( *scan_ch_c_l++ == '\n' )
149 if ( fildes_i_ == -1 )
152 data_caddr_ = (caddr_t)mmap( (void*)0, size_off_, PROT_READ, MAP_SHARED, fildes_i_, 0 );
154 if ( (int)data_caddr_ == -1 )
155 // ugh: defined_ch_c_l...
156 warning( String( "can't map: " ) + name_str_ + String( ": " ) + strerror( errno ), defined_ch_c_l ); //lexer->here_ch_c_l() );
160 Source_file::name_str()
168 String name_str = find_file( name_str_ );
169 if ( name_str != "" )
170 name_str_ = name_str;
172 fildes_i_ = ::open( name_str_, O_RDONLY );
174 if ( fildes_i_ == -1 ) {
175 warning( String( "can't open: " ) + name_str_ + String( ": " ) + strerror( errno ), defined_ch_c_l ); // lexer->here_ch_c_l() );
179 struct stat file_stat;
180 fstat( fildes_i_, &file_stat );
181 size_off_ = file_stat.st_size;
188 munmap( data_caddr_, size_off_ );
194 Source_file::file_line_no_str(const char *cch_c_l )
196 return name_str() + ": "
197 + String( line_i( cch_c_l ) );