summaryrefslogtreecommitdiff
path: root/flower/textstr.hh
blob: 41a979f27f66f450878d5e6fac9c72763b43a0e7 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <ctype.h>

//#include "globals.hh"
#include "string.hh"
#include "vray.hh"

/// line counting input stream.
class Text_stream
{
    int line_no;

    // could just have used streams. 
    FILE *f;  
    sstack<char> pushback;
    String name;
    
 public:
    Text_stream(String fn);
    String get_name() { return name; }
    bool eof() {
	return feof(f);
    }
    bool eol() {
	return (peek() == '\n');
    }
    char peek() {
	char c = get();
	unget(c);
	return c;
    }
    int line(){
	return line_no;
    }

    char    get() {
	char c;
	
	if (pushback.empty())
	    c = getc(f);	
	else 
	    c = pushback.pop();

	if (c =='\n')
	    line_no++;
	return c;	
    }
    void unget(char c) {
	if (c =='\n')
	    line_no--;
	pushback.push(c);
    }
    ~Text_stream (){
	if (!eof()) 
	    cerr <<__FUNCTION__<< ": closing unended file";
    
	fclose(f);
    }

    /// GNU format message.
    void message(String s); 
};
/**
 a stream for textfiles. linecounting. Thin interface getchar and
 ungetchar.  (ungetc is unlimited) 

 should protect get and unget against improper use
*/


/// read a data file
class Data_file : private Text_stream
{
    
 public:
    bool rawmode;

    Text_stream::line;    
    Text_stream::eof;
    Text_stream::get_name;    

    char data_get();    
    void data_unget(char c) {
	unget(c);
    }

    /// read line, eat #\n#
    String get_line();
    
    /// read a word till next space, leave space. Also does quotes
    String get_word();

    /// gobble horizontal white stuff.
    void gobble_white();

    /// gobble empty stuff before first field.
    void gobble_leading_white();
    Data_file(String s) : Text_stream(s) {
	//*mlog << "(" << s << flush;	
	rawmode=  false;	
    }

    ~Data_file()  {
	//	*mlog << ")"<<flush;	
    }    

    warning(String s) {
	message("warning: " + s);
    }
    error(String s){
	message(s);
	exit(1);    
    }
};