lilypond-1.3.141
[lilypond.git] / flower / textstream.hh
blob437f8698c7f8262cd00e29cf46c99e4747b8faff
2 #ifndef TEXTSTR_HH
3 #define TEXTSTR_HH
5 #include <stdio.h>
6 #include <ctype.h>
7 #include "string.hh"
8 #include "varray.hh"
10 /**
11 line counting input stream.
12 a stream for textfiles. linecounting. Thin interface getchar and
13 ungetchar. (ungetc is unlimited)
15 should protect get and unget against improper use
19 class Text_stream
21 int line_no;
23 // could just have used streams.
24 FILE *f;
25 Array<char> pushback;
26 String name;
28 public:
29 Text_stream(String fn);
30 String get_name() { return name; }
31 bool eof() {
32 return feof(f);
34 bool eol() {
35 return (peek() == '\n');
37 char peek() {
38 char c = get();
39 unget(c);
40 return c;
42 int line(){
43 return line_no;
46 char get() {
47 char c;
49 if (pushback.empty())
50 c = getc(f);
51 else
52 c = pushback.pop();
54 if (c =='\n')
55 line_no++;
56 return c;
58 void unget(char c) {
59 if (c =='\n')
60 line_no--;
61 pushback.push(c);
63 ~Text_stream (){
64 if (!eof())
65 cerr <<__FUNCTION__<< ": closing unended file";
67 fclose(f);
70 /// GNU format message.
71 void message(String s);
74 #endif