]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/text-stream.hh
release: 0.1.59
[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   char    get() {
35     char c;
36         
37     if (pushback.empty())
38       c = getc (f);     
39     else 
40       c = pushback.pop();
41
42     if (c =='\n')
43       line_no++;
44     return c;   
45   }
46   void unget (char c) {
47     if (c =='\n')
48       line_no--;
49     pushback.push (c);
50   }
51   char peek() {
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()) 
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