Debian Codegraph @

terps/tads/tads3/tcunas.h

raw symbols SCIP
1 /* $Header: d:/cvsroot/tads/tads3/TCUNAS.H,v 1.3 1999/07/11 00:46:58 MJRoberts Exp $ */
2
3 /*
4 * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 tcunas.h - TADS 3 Compiler Unassembler
12 Function
13
14 Notes
15
16 Modified
17 05/10/99 MJRoberts - Creation
18 */
19
20 #ifndef TCUNAS_H
21 #define TCUNAS_H
22
23 #include <stdio.h>
24 #include <stdarg.h>
25
26 #include "t3std.h"
27 #include "tcgen.h"
28
29
30 /* ------------------------------------------------------------------------ */
31 /*
32 * byte-code source for unassembler
33 */
34 class CTcUnasSrc
35 {
36 public:
37 virtual ~CTcUnasSrc() { }
38
39 /*
40 * read the next byte; returns zero on success, non-zero at the end
41 * of the byte stream
42 */
43 virtual int next_byte(char *ch) = 0;
44
45 /* get the current offset */
46 virtual ulong get_ofs() const = 0;
47 };
48
49 /*
50 * code stream implementation of byte code source
51 */
52 class CTcUnasSrcCodeStr: public CTcUnasSrc
53 {
54 public:
55 CTcUnasSrcCodeStr(CTcCodeStream *str)
56 {
57 /* remember our underlying code stream */
58 str_ = str;
59
60 /* start at the first byte of the code stream */
61 ofs_ = 0;
62 }
63
64 /* read from the code stream */
65 int next_byte(char *ch)
66 {
67 /* if there's anything left, return the byte and bump the pointer */
68 if (ofs_ < str_->get_ofs())
69 {
70 *ch = str_->get_byte_at(ofs_);
71 ++ofs_;
72 return 0;
73 }
74
75 /* indicate end of file */
76 return 1;
77 }
78
79 /* get the current offset */
80 ulong get_ofs() const
81 {
82 return ofs_;
83 }
84
85 protected:
86 /* underlying code stream object */
87 CTcCodeStream *str_;
88
89 /* current read offset in code stream */
90 ulong ofs_;
91 };
92
93 /* ------------------------------------------------------------------------ */
94 /*
95 * output stream for unassembler
96 */
97 class CTcUnasOut
98 {
99 public:
100 virtual ~CTcUnasOut() { }
101
102 /* write a line of text to the output, printf-style */
103 virtual void print(const char *fmt, ...) = 0;
104 };
105
106 /*
107 * stdio implementation of output stream - writes data to standard
108 * output
109 */
110 class CTcUnasOutStdio: public CTcUnasOut
111 {
112 public:
113 void print(const char *fmt, ...)
114 {
115 va_list va;
116
117 /* display the data on the standard output */
118 va_start(va, fmt);
119 vprintf(fmt, va);
120 va_end(va);
121 }
122 };
123
124 /*
125 * Text file (osfildef) implementation of output stream. The file handle
126 * is managed by the caller.
127 */
128 class CTcUnasOutFile: public CTcUnasOut
129 {
130 public:
131 CTcUnasOutFile(osfildef *fp) { fp_ = fp; }
132
133 void print(const char *fmt, ...)
134 {
135 char buf[1024];
136 va_list va;
137
138 /* format the text */
139 va_start(va, fmt);
140 t3vsprintf(buf, sizeof(buf), fmt, va);
141 va_end(va);
142
143 /* write the formatted text to the file */
144 os_fprintz(fp_, buf);
145 }
146
147 protected:
148 /* our file handle */
149 osfildef *fp_;
150 };
151
152 #endif /* TCUNAS_H */
153