File: nam_stream.h

package info (click to toggle)
nam 1.15-5
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 29,240 kB
  • sloc: cpp: 17,338; tcl: 10,655; sh: 2,997; ansic: 1,252; makefile: 139; perl: 66
file content (149 lines) | stat: -rw-r--r-- 4,181 bytes parent folder | download | duplicates (5)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

/*
 * Copyright (c) 1998 University of Southern California.
 * All rights reserved.                                            
 *                                                                
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation, advertising
 * materials, and other materials related to such distribution and use
 * acknowledge that the software was developed by the University of
 * Southern California, Information Sciences Institute.  The name of the
 * University may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

#ifndef nam_stream_h
#define nam_stream_h

#include <stdio.h>
#include <sys/types.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#endif

#include "nam.h"  // for die

class NamStream : public TclObject {
 protected:
	int is_open_;
	
 public:
	NamStream() : is_open_(0) {};
	// NamStream(int fd) : is_open_(0) {};
	NamStream(const char *fn) : is_open_(0) {}
	virtual ~NamStream() { if (is_open_) close(); };

	int command(int argc, const char*const* argv);
	static NamStream *open(const char *fn);

	int is_ok() { return is_open_; }
	virtual int seekable() { return 1; }

	virtual char *gets(char *buf, int len) { return NULL; };
	virtual char get_char() { return EOF; }
	virtual char *rgets(char *buf, int len);
	virtual off_t seek(off_t offset, int whence) { return -1; };
	virtual off_t tell() { return -1; };
	virtual int close() { is_open_ = 0; return 0; }
	virtual int eof() { return 1; };
	virtual int read(char *buf, int size) { return 0; }
};

class NamStreamFile : public NamStream {
	FILE *file_;

 public:
	// NamStreamFile(int fd);
	NamStreamFile(const char *fn);

	virtual int seekable() { return 1; }

	virtual char *gets(char *buf, int len);
	virtual char get_char();
	virtual off_t seek(off_t offset, int whence);
	virtual off_t tell();
	virtual int close();
	virtual int eof();
	virtual int read(char *buf, int size);
};

#ifdef HAVE_ZLIB_H
#include <zlib.h>

class NamStreamCompressedFile : public NamStream {
	gzFile file_;

 public:
	NamStreamCompressedFile(const char *fn);

	virtual int seekable() { return 0; }

	virtual char *gets(char *buf, int len);
	virtual char get_char();
	virtual off_t seek(off_t offset, int whence);
	virtual off_t tell();
	virtual int close();
	virtual int eof();
	virtual int read(char *buf, int size);
};
#endif

/*
 * Make front_ seem like a good, seekable file
 * when really it's a low-down pipe.
 * We do this by saving the output in back_.
 */
class NamStreamPipe : public NamStream {
 protected:
	int    front_;	// file descriptor of the pipe
	FILE  *back_;	// temporary backup file
	off_t  back_len_;	// file size of the backup file
	char  *pipename_;	// the pipe name

	// double linked list of all NamStreamPipe instances
	NamStreamPipe *prev_;
	NamStreamPipe *next_;
	static NamStreamPipe *head_;
	static int instances_;	// number of NamStreamPipe instances

	// timer to check pipe input periodically
	static Tcl_TimerToken timer_;
	static void timer_handler(ClientData data);
	static int  read_pipe();

	void insure_backing(off_t lim);

 public:
	NamStreamPipe(const char *fn);
	~NamStreamPipe();

	virtual int seekable() { return 0; }

	virtual char* gets(char *buf, int len);
	virtual char  get_char();
	virtual off_t seek(off_t offset, int whence);
	virtual off_t tell();
	virtual int   close();
	virtual int   eof();
	virtual int   read(char *buf, int size);

	/*
	 * The Tcl command to open a pipe stream is:
	 *     set stream [new NamStream $tracefile]
	 * that is, the instance construction command.
	 * Since this command can be executed several times on the same
	 * pipe, we need to make sure not to create duplicate instance.
	 */
	 static NamStreamPipe *open_pipe(const char *fn);
};

#endif /* nam_stream_h */