flower-1.0.25
[lilypond.git] / flower / textstr.hh
blobc89f58cf8db52c29d6b37f7e45ba980cdf152a44
1 #ifndef TEXTSTR_HH
2 #define TEXTSTR_HH
4 #include <stdio.h>
5 #include <ctype.h>
6 #include "string.hh"
7 #include "varray.hh"
9 /// line counting input stream.
10 class Text_stream
12 int line_no;
14 // could just have used streams.
15 FILE *f;
16 Array<char> pushback;
17 String name;
19 public:
20 Text_stream(String fn);
21 String get_name() { return name; }
22 bool eof() {
23 return feof(f);
25 bool eol() {
26 return (peek() == '\n');
28 char peek() {
29 char c = get();
30 unget(c);
31 return c;
33 int line(){
34 return line_no;
37 char get() {
38 char c;
40 if (pushback.empty())
41 c = getc(f);
42 else
43 c = pushback.pop();
45 if (c =='\n')
46 line_no++;
47 return c;
49 void unget(char c) {
50 if (c =='\n')
51 line_no--;
52 pushback.push(c);
54 ~Text_stream (){
55 if (!eof())
56 cerr <<__FUNCTION__<< ": closing unended file";
58 fclose(f);
61 /// GNU format message.
62 void message(String s);
64 /**
65 a stream for textfiles. linecounting. Thin interface getchar and
66 ungetchar. (ungetc is unlimited)
68 should protect get and unget against improper use
72 /// read a data file
73 class Data_file : private Text_stream
76 public:
77 bool rawmode;
79 Text_stream::line;
80 Text_stream::eof;
81 Text_stream::get_name;
83 char data_get();
84 void data_unget(char c) {
85 unget(c);
88 /// read line, eat #\n#
89 String get_line();
91 /// read a word till next space, leave space. Also does quotes
92 String get_word();
94 /// gobble horizontal white stuff.
95 void gobble_white();
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;
104 ~Data_file() {
105 // *mlog << ")"<<flush;
108 warning(String s) {
109 message("warning: " + s);
111 error(String s){
112 message(s);
113 exit(1);
116 #endif