]> git.donarmstrong.com Git - lilypond.git/blobdiff - src/sourcefile.cc
release: 0.0.36
[lilypond.git] / src / sourcefile.cc
index 4792483056400d12b05f9c5534e4c20f6eb35a58..eac2b1f84a6454385608c7c65be96c356e9d06f2 100644 (file)
 #include "string.hh"
 #include "proto.hh"
 #include "plist.hh"
+#include "lexer.hh"
+#include "debug.hh"
+#include "parseconstruct.hh"
 #include "main.hh"                     // find_file
 
 #include "sourcefile.hh"
 
 Source_file::Source_file( String &filename_str )
 {
-    data_caddr_m = 0;
-    fildes_i_m = 0;
-    size_off_m = 0;
-    name_str_m = filename_str;
-    istream_p_m = 0;
+    data_caddr_ = 0;
+    fildes_i_ = 0;
+    size_off_ = 0;
+    name_str_ = filename_str;
+    istream_p_ = 0;
 
     open();
     map();
-    filename_str = name_str_m;
+    // ugh!?, should call name_str() ! 
+    filename_str = name_str_;
 }
 
 Source_file::~Source_file()
 {
-    delete istream_p_m;
-    istream_p_m = 0;
+    delete istream_p_;
+    istream_p_ = 0;
     unmap();
     close();
 }
@@ -45,21 +49,23 @@ Source_file::~Source_file()
 char const*
 Source_file::ch_c_l()
 {
-    return (char const*)data_caddr_m;
+    assert( this );
+    return (char const*)data_caddr_;
 }
 
 void
 Source_file::close()
 {
-    if ( fildes_i_m ) {
-       ::close( fildes_i_m );
-       fildes_i_m = 0;
+    if ( fildes_i_ ) {
+       ::close( fildes_i_ );
+       fildes_i_ = 0;
     }
 }
 
 String
 Source_file::error_str( char const* pos_ch_c_l )
 {
+    assert( this );
     if ( !in_b( pos_ch_c_l ) )
        return "";
 
@@ -72,21 +78,23 @@ Source_file::error_str( char const* pos_ch_c_l )
        }
 
     char const* end_ch_c_l = pos_ch_c_l;
-    while ( end_ch_c_l < data_ch_c_l + size_off_m )
+    while ( end_ch_c_l < data_ch_c_l + size_off_ )
         if ( *end_ch_c_l++ == '\n' ) {
            break;
        }
     end_ch_c_l--;
 
-// String( char const* p, int size ) is missing!?
-//    String line_str( begin_ch_c_l, end_ch_c_l - begin_ch_c_l );
-
-    int size_i = end_ch_c_l - begin_ch_c_l;
-    char* ch_p = new char[ size_i ];
-    strncpy( ch_p, begin_ch_c_l, size_i );
-    ch_p[ size_i ] = 0;
+#if 1
+//    String( char const* p, int length ) is missing!?
+    String line_str( (Byte const*)begin_ch_c_l, end_ch_c_l - begin_ch_c_l );
+#else
+    int length_i = end_ch_c_l - begin_ch_c_l;
+    char* ch_p = new char[ length_i + 1 ];
+    strncpy( ch_p, begin_ch_c_l, length_i );
+    ch_p[ length_i ] = 0;
     String line_str( ch_p );
     delete ch_p;
+#endif
 
     int error_col_i = 0;
     char const* scan_ch_c_l = begin_ch_c_l;
@@ -106,16 +114,28 @@ Source_file::error_str( char const* pos_ch_c_l )
 bool
 Source_file::in_b( char const* pos_ch_c_l )
 {
-    return ( pos_ch_c_l >= ch_c_l() ) && ( pos_ch_c_l < ch_c_l() + size_off_m );
+    return ( pos_ch_c_l && ( pos_ch_c_l >= ch_c_l() ) && ( pos_ch_c_l < ch_c_l() + size_off_ ) );
 }
 
 istream*
 Source_file::istream_l()
 {
-    assert( fildes_i_m );
-    if ( !istream_p_m )
-       istream_p_m = new istrstream( ch_c_l(), size_off_m );
-    return istream_p_m;
+    assert( fildes_i_ );
+    if ( !istream_p_ ) {
+       if ( size_off_ ) // can-t this be done without such a hack?
+           istream_p_ = new istrstream( ch_c_l(), size_off_ );
+        else {
+           istream_p_ = new istrstream( "", 0 );
+           istream_p_->set(ios::eofbit);
+       }
+    }
+    return istream_p_;
+}
+
+off_t
+Source_file::length_off()
+{
+    return size_off_;
 }
 
 int
@@ -124,7 +144,7 @@ Source_file::line_i( char const* pos_ch_c_l )
     if ( !in_b( pos_ch_c_l ) )
        return 0;
 
-    int i = 0;
+    int i = 1;
     char const* scan_ch_c_l = ch_c_l();
     while ( scan_ch_c_l < pos_ch_c_l )
        if ( *scan_ch_c_l++ == '\n' )
@@ -135,42 +155,53 @@ Source_file::line_i( char const* pos_ch_c_l )
 void
 Source_file::map()
 {
-    data_caddr_m = (caddr_t)mmap( (void*)0, size_off_m, PROT_READ, MAP_FILE | MAP_SHARED, fildes_i_m, 0 );
+    if ( fildes_i_ == -1 )
+       return;
 
-    if ( (int)data_caddr_m == -1 ) {
-       cerr << "lilypond: can't map: " << name_str_m << ": " << strerror( errno ) << endl;
-       assert( 0 );
-    }
+    data_caddr_ = (caddr_t)mmap( (void*)0, size_off_, PROT_READ, MAP_SHARED, fildes_i_, 0 );
+
+    if ( (int)data_caddr_ == -1 )
+       // ugh: defined_ch_c_l...
+       warning( String( "can't map: " ) + name_str_ + String( ": " ) + strerror( errno ), defined_ch_c_l ); //lexer->here_ch_c_l() );
 }
 
 String
 Source_file::name_str()
 {
-    return name_str_m;
+    return name_str_;
 }
 
 void
 Source_file::open()
 {
-    name_str_m = find_file( name_str_m );
-    fildes_i_m = ::open( name_str_m, O_RDONLY );       
-    
-    if ( fildes_i_m == -1 ) {
-       cerr << "lilypond: can't open: " << name_str_m << ": " << strerror( errno ) << endl;
-       assert( 0 );
+    String name_str = find_file( name_str_ );
+    if ( name_str != "" ) 
+        name_str_ = name_str;
+
+    fildes_i_ = ::open( name_str_, O_RDONLY ); 
+           
+    if ( fildes_i_ == -1 ) {
+       warning( String( "can't open: " ) + name_str_ + String( ": " ) + strerror( errno ), defined_ch_c_l ); // lexer->here_ch_c_l() );
+        return;
     }
 
     struct stat file_stat;
-    fstat( fildes_i_m, &file_stat );
-    size_off_m = file_stat.st_size;
+    fstat( fildes_i_, &file_stat );
+    size_off_ = file_stat.st_size;
 }
 
 void
 Source_file::unmap()
 {
-    if ( data_caddr_m ) {
-       munmap( data_caddr_m, size_off_m );
-       data_caddr_m = 0;
-       size_off_m = 0;
+    if ( data_caddr_ ) {
+       munmap( data_caddr_, size_off_ );
+       data_caddr_ = 0;
+       size_off_ = 0;
     }
 }
+String
+Source_file::file_line_no_str(char const *ch_c_l )
+{
+    return name_str() + ": "
+       + String( line_i( ch_c_l ) );
+}