headers/bsd: Add sys/queue.h.
[haiku.git] / src / tests / add-ons / print / pcl6 / disasm.h
blob8b1bb5e2daae75033eabae0f3fbe51fbb018bc8e
1 /*
3 PCL6 Disassembler
5 Copyright (c) 2003 OpenBeOS.
7 Author:
8 Michael Pfeiffer
10 Permission is hereby granted, free of charge, to any person obtaining a copy of
11 this software and associated documentation files (the "Software"), to deal in
12 the Software without restriction, including without limitation the rights to
13 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14 of the Software, and to permit persons to whom the Software is furnished to do
15 so, subject to the following conditions:
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 THE SOFTWARE.
30 #ifndef _DISASM_H
31 #define _DISASM_H
33 #include "jetlib.h"
34 #include <SupportDefs.h>
35 #include <List.h>
37 class InputStream {
38 uint8 fBuffer; // one byte only
39 int8 fBufferSize;
40 uint32 fPos;
42 public:
43 InputStream() : fBufferSize(0), fPos(0) { }
44 virtual ~InputStream() {};
45 virtual int RawRead(void* buffer, int size) = 0;
46 int Read(void* buffer, int size);
47 int Pos() const { return fPos; }
48 bool PutUByte(uint8 v);
49 bool ReadUByte(uint8& v);
50 bool ReadUInt16(uint16& v);
51 bool ReadUInt32(uint32& v);
52 bool ReadSInt16(int16& v);
53 bool ReadSInt32(int32& v);
54 bool ReadReal32(float& v);
57 class File : public InputStream {
58 FILE* fFile;
59 public:
60 File(const char* filename);
61 ~File();
62 bool InitCheck() { return fFile != NULL; }
63 int RawRead(void* buffer, int size);
66 typedef struct {
67 uint8 value;
68 const char* name;
69 } AttrValue;
71 #define NUM_OF_ELEMS(array, type) (sizeof(array) / sizeof(type))
73 class Disasm {
74 public:
75 Disasm(InputStream* stream) : fStream(stream), fVerbose(false) { };
76 void SetVerbose(bool v) { fVerbose = v; }
77 void Print();
79 private:
80 InputStream* fStream;
81 BList fArgs;
82 bool fVerbose;
84 void Error(const char* text);
85 bool Expect(const char* text);
86 bool SkipTo(const char* text);
87 bool ReadNumber(int32& n);
88 bool ParsePJL();
90 void PushArg(struct ATTRIBUTE* attr) { fArgs.AddItem(attr); }
91 int NumOfArgs() { return fArgs.CountItems(); }
92 struct ATTRIBUTE* ArgAt(int i) { return (struct ATTRIBUTE*)fArgs.ItemAt(i); }
93 void DeleteAttr(struct ATTRIBUTE* attr);
94 void PrintAttr(struct ATTRIBUTE* attr);
95 void ClearAttrs();
97 bool DecodeOperator(uint8 byte);
98 struct ATTRIBUTE* ReadData(uint8 byte);
99 bool PushData(uint8 byte);
100 bool ReadArrayLength(uint32& length);
101 bool ReadArray(uint8 type, uint32 length, struct ATTRIBUTE* attr);
102 const char* AttributeName(uint8 id);
103 void GenericPrintAttribute(uint8 id);
104 void PrintAttributeValue(uint8 id, const AttrValue* table, int tableSize);
105 bool DecodeAttribute(uint8 byte);
106 bool DecodeEmbedData(uint8 byte);
107 bool ParsePCL6();
109 bool IsOperator(uint8 byte) const { return byte >= 0x41 && byte <= 0xb9; }
110 bool IsDataType(uint8 byte) const { return byte >= 0xc0 && byte <= 0xef; }
111 bool IsAttribute(uint8 byte) const { return byte >= 0xf8 && byte <= 0xf9; }
112 bool IsEmbedData(uint8 byte) const { return byte >= 0xfa && byte <= 0xfb; }
113 bool IsWhiteSpace(uint8 byte) const { return byte == 0 || byte >= 0x09 && byte <= 0x0d || byte == 0x20; }
114 bool IsEsc(uint8 byte) const { return byte == 0x1b; }
117 #endif