]> git.donarmstrong.com Git - lilypond.git/blob - lib/source-file.cc
release: 0.1.9
[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       {
37         if ( length_i() ) // can-t this be done without such a hack?
38             istream_p_ = new istrstream( ch_C(), length_i() );
39         else 
40           {
41             istream_p_ = new istrstream( "", 0 );
42             istream_p_->set(ios::eofbit);
43           }
44       }
45     return istream_p_;
46 }
47
48 String
49 Source_file::file_line_no_str(char const *ch_C )const
50 {
51     return name_str() + ": "
52         + String( line_i( ch_C ) );
53 }
54
55 String
56 Source_file::name_str()const
57 {
58     return name_str_;
59 }
60 Source_file::~Source_file()
61 {
62     delete istream_p_;
63     istream_p_ = 0;
64     delete storage_p_;
65 }
66 String
67 Source_file::error_str( char const* pos_ch_C )const
68 {
69     char const* data_ch_C = ch_C();
70     char const * eof_C_ = data_ch_C + length_i();
71     if ( !in_b( pos_ch_C ) )
72         return "(position unknown)";
73
74     
75     if ( pos_ch_C == eof_C_)
76         pos_ch_C --;
77     char const* begin_ch_C = pos_ch_C;
78     while ( begin_ch_C > data_ch_C )
79         if ( *--begin_ch_C == '\n' ) 
80           {
81             begin_ch_C++;
82             break;
83           }
84
85     char const* end_ch_C = pos_ch_C;
86     while ( end_ch_C < eof_C_ )
87         if ( *end_ch_C++ == '\n' ) 
88           {
89           end_ch_C--;
90           break;
91           }
92   
93         //    String( char const* p, int length ) is missing!?
94     String line_str( (Byte const*)begin_ch_C, end_ch_C - begin_ch_C );
95
96     int error_col_i = 0;
97     char const* scan_ch_C = begin_ch_C;
98     while ( scan_ch_C < pos_ch_C )
99         if ( *scan_ch_C++ == '\t' )
100             error_col_i = ( error_col_i / 8 + 1 ) * 8;
101         else
102             error_col_i++;
103
104     String str = line_str.left_str( pos_ch_C - begin_ch_C ) 
105         + String( '\n' )
106         + String( ' ', error_col_i ) 
107         + line_str.mid_str( pos_ch_C - begin_ch_C, INT_MAX ); // String::mid should take 0 arg..
108     return str;
109 }
110
111 bool
112 Source_file::in_b( char const* pos_ch_C )const
113 {
114     return ( pos_ch_C && ( pos_ch_C >= ch_C() ) && ( pos_ch_C <= ch_C() + length_i() ) );
115 }
116
117
118 int
119 Source_file::line_i( char const* pos_ch_C )const
120 {
121     if ( !in_b( pos_ch_C ) )
122         return 0;
123
124     int i = 1;
125     char const* scan_ch_C = ch_C();
126     while ( scan_ch_C < pos_ch_C )
127         if ( *scan_ch_C++ == '\n' )
128                 i++;
129     return i;
130 }
131
132 int
133 Source_file::length_i()const
134 {
135     return storage_p_->length_i();
136 }
137
138 char const *
139 Source_file::ch_C()const
140 {
141     return storage_p_->ch_C();
142 }