1 /*******************************************************************************
2  * textstream.h
3  *
4  * This module contains all defines, typedefs, and prototypes for the
5  * C++ interface of textstream.cpp.
6  *
7  * ---------------------------------------------------------------------------
8  * Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
9  * Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
10  *
11  * POV-Ray is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Affero General Public License as
13  * published by the Free Software Foundation, either version 3 of the
14  * License, or (at your option) any later version.
15  *
16  * POV-Ray is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Affero General Public License for more details.
20  *
21  * You should have received a copy of the GNU Affero General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  * ---------------------------------------------------------------------------
24  * POV-Ray is based on the popular DKB raytracer version 2.12.
25  * DKBTrace was originally written by David K. Buck.
26  * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
27  * ---------------------------------------------------------------------------
28  * $File: //depot/public/povray/3.x/source/base/textstream.h $
29  * $Revision: #1 $
30  * $Change: 6069 $
31  * $DateTime: 2013/11/06 11:59:40 $
32  * $Author: chrisc $
33  *******************************************************************************/
34 
35 #ifndef TEXTSTREAM_H
36 #define TEXTSTREAM_H
37 
38 #include <cstdio>
39 
40 // must nuke these since everyone's favourite monopoly's cstdio still defines
41 // them for some reason (why not just use inlines like everyone else?)
42 #undef  getc
43 #undef  putc
44 #undef  getchar
45 #undef  putchar
46 
47 #include "configbase.h"
48 
49 #include "base/fileinputoutput.h"
50 #include "base/pov_err.h"
51 #include "base/types.h"
52 
53 namespace pov_base
54 {
55 
56 const int ITEXTSTREAM_BUFFER_SIZE = DEFAULT_ITEXTSTREAM_BUFFER_SIZE;
57 
58 class ITextStream
59 {
60 	public:
61 		struct FilePos
62 		{
63 			POV_LONG offset;
64 			POV_LONG lineno;
65 		};
66 
67 		ITextStream(const UCS2 *, unsigned int);
68 		ITextStream(const UCS2 *, IStream *);
69 		virtual ~ITextStream();
70 
71 		int getchar();
72 		void ungetchar(int);
73 
74 		bool eof() const;
75 		bool seekg(FilePos);
76 		FilePos tellg() const;
77 
line()78 		POV_LONG line() const { return lineno; };
79 
name()80 		const UCS2 *name() const { return filename.c_str(); };
81 	private:
82 		IStream *stream;
83 		unsigned char buffer[ITEXTSTREAM_BUFFER_SIZE];
84 		POV_ULONG bufferoffset;
85 		POV_ULONG maxbufferoffset;
86 		POV_ULONG filelength;
87 		POV_ULONG curpos;
88 		UCS2String filename;
89 		POV_LONG lineno;
90 		int ungetbuffer;
91 
92 		void RefillBuffer();
93 };
94 
95 class OTextStream
96 {
97 	public:
98 		OTextStream(const UCS2 *, unsigned int, bool append = false);
99 		OTextStream(const UCS2 *, OStream *);
100 		virtual ~OTextStream();
101 
102 		void putchar(int);
103 		void putraw(int);
104 		void printf(const char *, ...);
105 	void flush();
106 
name()107 		const UCS2 *name() const { return filename.c_str(); };
108 	private:
109 		OStream *stream;
110 		UCS2String filename;
111 };
112 
113 }
114 
115 #endif
116