]> git.donarmstrong.com Git - lilypond.git/blob - flower/textstr.hh
release: 0.0.20
[lilypond.git] / flower / textstr.hh
1 #ifndef TEXTSTR_HH
2 #define TEXTSTR_HH
3
4 #include <stdio.h>
5 #include <ctype.h>
6 #include "string.hh"
7 #include "sstack.hh"
8
9 /// line counting input stream.
10 class Text_stream
11 {
12     int line_no;
13
14     // could just have used streams. 
15     FILE *f;  
16     sstack<char> pushback;
17     String name;
18     
19  public:
20     Text_stream(String fn);
21     String get_name() { return name; }
22     bool eof() {
23         return feof(f);
24     }
25     bool eol() {
26         return (peek() == '\n');
27     }
28     char peek() {
29         char c = get();
30         unget(c);
31         return c;
32     }
33     int line(){
34         return line_no;
35     }
36
37     char    get() {
38         char c;
39         
40         if (pushback.empty())
41             c = getc(f);        
42         else 
43             c = pushback.pop();
44
45         if (c =='\n')
46             line_no++;
47         return c;       
48     }
49     void unget(char c) {
50         if (c =='\n')
51             line_no--;
52         pushback.push(c);
53     }
54     ~Text_stream (){
55         if (!eof()) 
56             cerr <<__FUNCTION__<< ": closing unended file";
57     
58         fclose(f);
59     }
60
61     /// GNU format message.
62     void message(String s); 
63 };
64 /**
65  a stream for textfiles. linecounting. Thin interface getchar and
66  ungetchar.  (ungetc is unlimited) 
67
68  should protect get and unget against improper use
69 */
70
71
72 /// read a data file
73 class Data_file : private Text_stream
74 {
75     
76  public:
77     bool rawmode;
78
79     Text_stream::line;    
80     Text_stream::eof;
81     Text_stream::get_name;    
82
83     char data_get();    
84     void data_unget(char c) {
85         unget(c);
86     }
87
88     /// read line, eat #\n#
89     String get_line();
90     
91     /// read a word till next space, leave space. Also does quotes
92     String get_word();
93
94     /// gobble horizontal white stuff.
95     void gobble_white();
96
97     /// gobble empty stuff before first field.
98     void gobble_leading_white();
99     Data_file(String s) : Text_stream(s) {
100         //*mlog << "(" << s << flush;   
101         rawmode=  false;        
102     }
103
104     ~Data_file()  {
105         //      *mlog << ")"<<flush;    
106     }    
107
108     warning(String s) {
109         message("warning: " + s);
110     }
111     error(String s){
112         message(s);
113         exit(1);    
114     }
115 };
116 #endif