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