vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / translators / rtf / RTF.h
blob504f2295abd16673e2e0380891b117820847a200
1 /*
2 * Copyright 2004-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef RTF_H
6 #define RTF_H
9 #include "Stack.h"
11 #include <List.h>
12 #include <String.h>
13 #include <GraphicsDefs.h>
14 #include <BufferIO.h>
17 namespace RTF {
19 class Group;
20 class Header;
21 class Command;
23 static const size_t kCommandLength = 32;
25 enum group_destination {
26 TEXT_DESTINATION,
27 COMMENT_DESTINATION,
28 OTHER_DESTINATION,
30 ALL_DESTINATIONS = 255
34 class Parser {
35 public:
36 Parser(BPositionIO &stream);
38 status_t Identify();
39 status_t Parse(RTF::Header &header);
41 private:
42 BBufferIO fStream;
43 bool fIdentified;
47 class Element {
48 public:
49 Element();
50 virtual ~Element();
52 void SetParent(Group *parent);
53 Group *Parent() const;
55 virtual bool IsDefinitionDelimiter();
56 virtual void Parse(char first, BDataIO &stream, char &last) throw (status_t) = 0;
57 virtual void PrintToStream(int32 level = 0);
59 private:
60 Group *fParent;
64 class Group : public Element {
65 public:
66 Group();
67 virtual ~Group();
69 status_t AddElement(RTF::Element *element);
70 uint32 CountElements() const;
71 Element *ElementAt(uint32 index) const;
73 Element *FindDefinitionStart(int32 index, int32 *_startIndex = NULL) const;
74 Command *FindDefinition(const char *name, int32 index = 0) const;
75 Group *FindGroup(const char *name) const;
77 const char *Name() const;
79 void DetermineDestination();
80 group_destination Destination() const;
82 virtual void Parse(char first, BDataIO &stream, char &last) throw (status_t);
84 protected:
85 BList fElements;
86 group_destination fDestination;
89 class Header : public Group {
90 public:
91 Header();
92 virtual ~Header();
94 int32 Version() const;
95 const char *Charset() const;
97 rgb_color Color(int32 index);
99 virtual void Parse(char first, BDataIO &stream, char &last) throw (status_t);
101 private:
102 int32 fVersion;
106 class Text : public Element {
107 public:
108 Text();
109 virtual ~Text();
111 status_t SetTo(const char *text);
112 const char *String() const;
113 uint32 Length() const;
115 virtual bool IsDefinitionDelimiter();
116 virtual void Parse(char first, BDataIO &stream, char &last) throw (status_t);
118 private:
119 BString fText;
122 class Command : public Element {
123 public:
124 Command();
125 virtual ~Command();
127 status_t SetName(const char *name);
128 const char *Name();
130 void UnsetOption();
131 void SetOption(int32 option);
132 bool HasOption() const;
133 int32 Option() const;
135 virtual void Parse(char first, BDataIO &stream, char &last) throw (status_t);
137 private:
138 BString fName;
139 bool fHasOption;
140 int32 fOption;
143 //---------------------------------
145 class Iterator {
146 public:
147 Iterator(Element &start, group_destination destination = ALL_DESTINATIONS);
149 void SetTo(Element &start, group_destination destination = ALL_DESTINATIONS);
150 void Rewind();
152 bool HasNext() const;
153 Element *Next();
155 private:
156 Element *fStart;
157 Stack<Element *> fStack;
158 group_destination fDestination;
161 class Worker {
162 public:
163 Worker(RTF::Header &start);
164 virtual ~Worker();
166 void Work() throw (status_t);
168 protected:
169 virtual void Group(RTF::Group *group);
170 virtual void GroupEnd(RTF::Group *group);
171 virtual void Command(RTF::Command *command);
172 virtual void Text(RTF::Text *text);
174 RTF::Header &Start();
175 void Skip();
176 void Abort(status_t status);
178 private:
179 void Dispatch(RTF::Element *element);
181 RTF::Header &fStart;
182 bool fSkip;
185 } // namespace RTF
187 #endif /* RTF_H */