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 /* 38 * read the next byte; returns zero on success, non-zero at the end 39 * of the byte stream 40 */ 41 virtual int next_byte(char *ch) = 0; 42 43 /* get the current offset */ 44 virtual ulong get_ofs() const = 0; 45 }; 46 47 /* 48 * code stream implementation of byte code source 49 */ 50 class CTcUnasSrcCodeStr: public CTcUnasSrc 51 { 52 public: CTcUnasSrcCodeStr(CTcCodeStream * str)53 CTcUnasSrcCodeStr(CTcCodeStream *str) 54 { 55 /* remember our underlying code stream */ 56 str_ = str; 57 58 /* start at the first byte of the code stream */ 59 ofs_ = 0; 60 } 61 62 /* read from the code stream */ next_byte(char * ch)63 int next_byte(char *ch) 64 { 65 /* if there's anything left, return the byte and bump the pointer */ 66 if (ofs_ < str_->get_ofs()) 67 { 68 *ch = str_->get_byte_at(ofs_); 69 ++ofs_; 70 return 0; 71 } 72 73 /* indicate end of file */ 74 return 1; 75 } 76 77 /* get the current offset */ get_ofs()78 ulong get_ofs() const 79 { 80 return ofs_; 81 } 82 83 protected: 84 /* underlying code stream object */ 85 CTcCodeStream *str_; 86 87 /* current read offset in code stream */ 88 ulong ofs_; 89 }; 90 91 /* ------------------------------------------------------------------------ */ 92 /* 93 * output stream for unassembler 94 */ 95 class CTcUnasOut 96 { 97 public: ~CTcUnasOut()98 virtual ~CTcUnasOut() { } 99 100 /* write a line of text to the output, printf-style */ 101 virtual void print(const char *fmt, ...) = 0; 102 }; 103 104 /* 105 * stdio implementation of output stream - writes data to standard 106 * output 107 */ 108 class CTcUnasOutStdio: public CTcUnasOut 109 { 110 public: print(const char * fmt,...)111 void print(const char *fmt, ...) 112 { 113 va_list va; 114 115 /* display the data on the standard output */ 116 va_start(va, fmt); 117 vprintf(fmt, va); 118 va_end(va); 119 } 120 }; 121 122 /* 123 * Text file (osfildef) implementation of output stream. The file handle 124 * is managed by the caller. 125 */ 126 class CTcUnasOutFile: public CTcUnasOut 127 { 128 public: CTcUnasOutFile(osfildef * fp)129 CTcUnasOutFile(osfildef *fp) { fp_ = fp; } 130 print(const char * fmt,...)131 void print(const char *fmt, ...) 132 { 133 char buf[1024]; 134 va_list va; 135 136 /* format the text */ 137 va_start(va, fmt); 138 t3vsprintf(buf, sizeof(buf), fmt, va); 139 va_end(va); 140 141 /* write the formatted text to the file */ 142 os_fprintz(fp_, buf); 143 } 144 145 protected: 146 /* our file handle */ 147 osfildef *fp_; 148 }; 149 150 #endif /* TCUNAS_H */ 151 152