]> git.donarmstrong.com Git - lilypond.git/blob - lib/source-file.cc
release: 0.1.7
[lilypond.git] / lib / source-file.cc
1 /*
2   source-file.cc -- implement 
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997 Jan Nieuwenhuizen <jan@digicash.com> 
7   & Han-Wen Nienhuys <hanwen@stack.nl>
8 */
9
10
11 #include <assert.h>
12 #include <strstream.h>
13
14 #include "string.hh"
15 #include "proto.hh"
16 #include "plist.hh"
17 #include "warn.hh"
18 #include "windhoos-suck-suck-suck-thank-you-cygnus.hh"
19 #include "source-file.hh"
20 #include "file-storage.hh"
21
22 Source_file::Source_file( String filename_str )
23 {
24     name_str_ = filename_str;
25     istream_p_ = 0;
26     storage_p_ = new Simple_file_storage(filename_str);
27 }
28
29 istream*
30 Source_file::istream_l()
31 {
32     if ( !name_str_.length_i())
33         return &cin;
34     
35     if ( !istream_p_ ) {
36         if ( length_i() ) // can-t this be done without such a hack?
37             istream_p_ = new istrstream( ch_C(), length_i() );
38         else {
39             istream_p_ = new istrstream( "", 0 );
40             istream_p_->set(ios::eofbit);
41         }
42     }
43     return istream_p_;
44 }
45
46 String
47 Source_file::file_line_no_str(char const *ch_C )const
48 {
49     return name_str() + ": "
50         + String( line_i( ch_C ) );
51 }
52
53 String
54 Source_file::name_str()const
55 {
56     return name_str_;
57 }
58 Source_file::~Source_file()
59 {
60     delete istream_p_;
61     istream_p_ = 0;
62     delete storage_p_;
63 }
64 String
65 Source_file::error_str( char const* pos_ch_C )const
66 {
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)";
71
72     
73     if ( pos_ch_C == eof_C_)
74         pos_ch_C --;
75     char const* begin_ch_C = pos_ch_C;
76     while ( begin_ch_C > data_ch_C )
77         if ( *--begin_ch_C == '\n' ) {
78             begin_ch_C++;
79             break;
80         }
81
82     char const* end_ch_C = pos_ch_C;
83     while ( end_ch_C < eof_C_ )
84         if ( *end_ch_C++ == '\n' ) {
85           end_ch_C--;
86           break;
87         }
88   
89         //    String( char const* p, int length ) is missing!?
90     String line_str( (Byte const*)begin_ch_C, end_ch_C - begin_ch_C );
91
92     int error_col_i = 0;
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;
97         else
98             error_col_i++;
99
100     String str = line_str.left_str( pos_ch_C - begin_ch_C ) 
101         + String( '\n' )
102         + String( ' ', error_col_i ) 
103         + line_str.mid_str( pos_ch_C - begin_ch_C, INT_MAX ); // String::mid should take 0 arg..
104     return str;
105 }
106
107 bool
108 Source_file::in_b( char const* pos_ch_C )const
109 {
110     return ( pos_ch_C && ( pos_ch_C >= ch_C() ) && ( pos_ch_C <= ch_C() + length_i() ) );
111 }
112
113
114 int
115 Source_file::line_i( char const* pos_ch_C )const
116 {
117     if ( !in_b( pos_ch_C ) )
118         return 0;
119
120     int i = 1;
121     char const* scan_ch_C = ch_C();
122     while ( scan_ch_C < pos_ch_C )
123         if ( *scan_ch_C++ == '\n' )
124                 i++;
125     return i;
126 }
127
128 int
129 Source_file::length_i()const
130 {
131     return storage_p_->length_i();
132 }
133
134 char const *
135 Source_file::ch_C()const
136 {
137     return storage_p_->ch_C();
138 }