]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/text-stream.hh
release: 1.0.1
[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 "array.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_b();
32   char get() {
33     char c;
34         
35     if (pushback.empty())
36       c = getc (f);     
37     else 
38       c = pushback.pop();
39
40     if (c =='\n')
41       line_no++;
42     return c;   
43   }
44   void unget (char c) {
45     if (c =='\n')
46       line_no--;
47     pushback.push (c);
48   }
49   char peek() {
50     if (eof_b ())
51       return -1;
52     char c = get();
53     unget (c);
54     return c;
55   }
56   bool eol() {
57     return (peek() == '\n');
58   }
59   int line(){
60     return line_no;
61   }
62
63   ~Text_stream(){
64     if (!eof_b()) 
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