1 //=============================================================================
2 // File:       field.h
3 // Contents:   Declarations for DwField
4 // Maintainer: Doug Sauder <dwsauder@fwb.gulf.net>
5 // WWW:        http://www.fwb.gulf.net/~dwsauder/mimepp.html
6 // $Revision: 1.9 $
7 // $Date: 2002/04/22 09:45:44 $
8 //
9 // Copyright (c) 1996, 1997 Douglas W. Sauder
10 // All rights reserved.
11 //
12 // IN NO EVENT SHALL DOUGLAS W. SAUDER BE LIABLE TO ANY PARTY FOR DIRECT,
13 // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
14 // THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DOUGLAS W. SAUDER
15 // HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 //
17 // DOUGLAS W. SAUDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
18 // NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19 // PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
20 // BASIS, AND DOUGLAS W. SAUDER HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
21 // SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
22 //
23 //=============================================================================
24 
25 #ifndef DW_FIELD_H
26 #define DW_FIELD_H
27 
28 #include <iostream>
29 
30 #ifndef DW_CONFIG_H
31 #include <mimelib/config.h>
32 #endif
33 
34 #ifndef DW_STRING_H
35 #include <mimelib/string.h>
36 #endif
37 
38 #ifndef DW_MSGCMP_H
39 #include <mimelib/msgcmp.h>
40 #endif
41 
42 class DwHeaders;
43 class DwFieldBody;
44 
45 //=============================================================================
46 //+ Name DwField -- Class representing a MIME header field
47 //+ Description
48 //. {\tt DwField} represents a header field as described in RFC-822.
49 //. According to RFC-822, a field contains a field name and a field body.
50 //. In MIME++, a {\tt DwField} contains three elements: a {\tt DwString}
51 //. that contains its field name, a {\tt DwString} that contains its
52 //. field body, and a {\tt DwFieldBody} object that contains a broken-down
53 //. (that is, parsed) version of its field body.
54 //.
55 //. In the tree (broken-down) representation of message, a {\tt DwField}
56 //. object is always an intermediate node, having a parent node and a single
57 //. child node.  The parent node is the {\tt DwHeaders} object that contains
58 //. it.  The child node is the {\tt DwFieldBody} object it contains.
59 //.
60 //. To get and set the field name, use the member functions
61 //. {\tt FieldNameStr()} and {\tt SetFieldNameStr()}.
62 //. To get and set the field body, use the member functions
63 //. {\tt FieldBodyStr()} and {\tt SetFieldBodyStr()}.
64 //. To get and set the {\tt DwFieldBody} object, use {\tt FieldBody()}
65 //. and {\tt SetFieldBody()}.
66 //.
67 //. A {\tt DwField} object can be included in a list of {\tt DwField}
68 //. objects; usually this is the list of {\tt DwField} objects maintained
69 //. by its parent {\tt DwHeaders} object.  To get the next {\tt DwField}
70 //. object in a list, use the member function {\tt Next()}.
71 //=============================================================================
72 // Last updated 1997-08-23
73 //+ Noentry ~DwField _CreateFieldBody mFieldNameStr mFieldBodyStr
74 //+ Noentry mFieldBody _PrintDebugInfo
75 
76 class DW_EXPORT DwField : public DwMessageComponent {
77 
78     friend class DwHeaders;
79 
80 public:
81 
82     DwField();
83     DwField(const DwField& aField);
84     DwField(const DwString& aStr, DwMessageComponent* aParent=0);
85     //. The first constructor is the default constructor, which sets the
86     //. {\tt DwField} object's field name and field body to the empty
87     //. string, set its parent to {\tt NULL}, and sets its {\tt DwFieldBody}
88     //. object to {\tt NULL}.
89     //.
90     //. The second constructor is the copy constructor, which performs
91     //. a deep copy of {\tt aField}.
92     //. The parent of the new {\tt DwField} object is set to {\tt NULL}.
93     //.
94     //. The third constructor copies {\tt aStr} to the {\tt DwField}
95     //. object's string representation and sets {\tt aParent} as its parent.
96     //. The virtual member function {\tt Parse()} should be called immediately
97     //. after this constructor in order to parse the string representation.
98     //. Unless it is {\tt NULL}, {\tt aParent} should point to an object of
99     //. a class derived from {\tt DwHeaders}.
100 
101     virtual ~DwField();
102 
103     const DwField& operator = (const DwField& aField);
104     //. This is the assignment operator, which performs a deep copy of
105     //. {\tt aField}.  The parent node of the {\tt DwField} object
106     //. is not changed.
107 
108     virtual void Parse();
109     //. This virtual function, inherited from {\tt DwMessageComponent},
110     //. executes the parse method for {\tt DwField} objects.  The parse
111     //. method creates or updates the broken-down representation from the
112     //. string representation.  For {\tt DwField} objects, the parse method
113     //. parses the string representation, sets the values of the field
114     //. name string and the field body string, and creates an instance
115     //. of the appropriate subclass of {\tt DwFieldBody}.  This member
116     //. function also calls the {\tt Parse()} member function of its
117     //. contained {\tt DwFieldBody} object.
118     //.
119     //. You should call this member function after you set or modify the
120     //. string representation, and before you access the field name, the
121     //. field body, or the contained {\tt DwFieldBody} object.
122     //.
123     //. This function clears the is-modified flag.
124 
125     virtual void Assemble();
126     //. This virtual function, inherited from {\tt DwMessageComponent},
127     //. executes the assemble method for {\tt DwField} objects. The
128     //. assemble method creates or updates the string representation from
129     //. the broken-down representation.  In more concrete terms, the
130     //. assemble method builds the string representation from the field
131     //. name and the string representation of the contained {\tt DwFieldBody}
132     //. object.  This member function calls the {\tt Assemble()} member
133     //. function of its contained {\tt DwFieldBody} object.
134     //.
135     //. You should call this member function after you modify either the
136     //. field name or the contained {\tt DwFieldBody} object, and before
137     //. you retrieve the string representation.
138     //.
139     //. This function clears the is-modified flag.
140 
141     virtual DwMessageComponent* Clone() const;
142     //. This virtual function, inherited from {\tt DwMessageComponent},
143     //. creates a new {\tt DwField} on the free store that has the same
144     //. value as this {\tt DwField} object.  The basic idea is that of
145     //. a virtual copy constructor.
146 
147     DwFieldBody* FieldBody() const;
148     //. Returns the {\tt DwFieldBody} object contained by this {\tt DwField}
149     //. object.  If there is no field body, {\tt NULL} will be returned.
150 
151     const DwString& FieldNameStr() const;
152     //. Returns the field name of this header field as a string.
153 
154     const DwString& FieldBodyStr() const;
155     //. Returns the field body of this header field as a string.
156 
157     DwField* Next() const;
158     //. Returns the next {\tt DwField} object following this
159     //. {\tt DwField} object in the list contained in a {\tt DwHeaders}.
160     //. Returns {\tt NULL} if this object is last in the list.
161 
162     void SetFieldBody(DwFieldBody* aFieldBody);
163     //. Sets the {\tt DwFieldBody} object contained by this object.
164 
165     void SetFieldNameStr(const DwString& aStr);
166     //. Sets the field name of this header field.
167 
168     void SetFieldBodyStr(const DwString& aStr);
169     //. Sets the field body of this header field.
170 
171     void SetNext(const DwField* aField);
172     //. This {\it advanced} function sets {\tt aField} as the next field
173     //. following this field in the list of fields contained in the headers.
174     //. Since {\tt DwHeaders} contains member functions for adding
175     //. {\tt DwField} objects to its list, this function should be
176     //. avoided for most applications.
177 
178     static DwField* NewField(const DwString& aStr,
179         DwMessageComponent* aParent);
180     //. Creates a new {\tt DwField} object on the free store.
181     //. If the static data member {\tt sNewField} is {\tt NULL},
182     //. this member function will create a new {\tt DwField}
183     //. and return it.  Otherwise, {\tt NewField()} will call
184     //. the user-supplied function pointed to by {\tt sNewField},
185     //. which is assumed to return an object from a class derived from
186     //. {\tt DwField}, and return that object.
187 
188     static DwFieldBody* CreateFieldBody(const DwString& aFieldName,
189         const DwString& aFieldBody, DwMessageComponent* aParent);
190     //. The static member function {\tt CreateFieldBody()} is called from
191     //. the {\tt Parse()} member function and is responsible for creating a
192     //. {\tt DwFieldBody} object for this particular field.  A typical
193     //. scenario might go as follows:
194     //. This member function examines the field name for this field,
195     //. finds that it contains "To", creates a {\tt DwAddressList} object
196     //. to contain the field body, calls the {\tt Parse()} member
197     //. function for the {\tt DwAddressList}, and sets the {\tt DwAddressList}
198     //. object as this {\tt DwField} object's {\tt DwFieldBody}.
199     //.
200     //. If you want to override the behavior of {\tt CreateFieldBody()},
201     //. you can do so by setting the public data member
202     //. {\tt sCreateFieldBody} to point to your own function.
203     //. {\tt CreateFieldBody()} first checks to see if
204     //. {\tt sCreateFieldBody} is {\tt NULL}.  If it is not,
205     //. {\tt CreateFieldBody()} will assume that it points to a user-supplied
206     //. function and will call that function.  If it is {\tt NULL},
207     //. {\tt CreateFieldBody()} will call {\tt _CreateFieldBody()}, which
208     //. actually creates the {\tt DwFieldBody} object.  You may call
209     //. {\tt _CreateFieldBody()} from your own function for fields you
210     //. do not wish to handle.
211 
212     static DwFieldBody* _CreateFieldBody(const DwString& aFieldName,
213         const DwString& aFieldBody, DwMessageComponent* aParent);
214 
215     //+ Var sNewField
216     static DwField* (*sNewField)(const DwString&, DwMessageComponent*);
217     //. If {\tt sNewField} is not {\tt NULL}, it is assumed to point
218     //. to a user-supplied function that returns an object from a class
219     //. derived from {\tt DwField}.
220 
221     //+ Var sCreateFieldBody
222     static DwFieldBody* (*sCreateFieldBody)(const DwString& aFieldName,
223         const DwString& aFieldBody, DwMessageComponent* aParent);
224     //. See {\tt CreateFieldBody()}.
225 
226 protected:
227 
228     DwString mFieldNameStr;
229     // the {\it field-name}
230 
231     DwString mFieldBodyStr;
232     // the {\it field-body}
233 
234     DwFieldBody* mFieldBody;
235     // pointer to the {\tt DwFieldBody} object
236 
237     void _SetFieldBody(DwFieldBody* aFieldBody);
238     //. Sets the {\tt DwFieldBody} object contained by this object.  This
239     //. function differs from {\tt SetFieldBody()} in that it does not
240     //. set the is-modified flag.
241 
242 private:
243 
244     const DwField* mNext;
245     static const char* const sClassName;
246 
247 public:
248 
249     virtual void PrintDebugInfo(std::ostream& aStrm, int aDepth=0) const;
250     //. This virtual function, inherited from {\tt DwMessageComponent},
251     //. prints debugging information about this object to {\tt aStrm}.
252     //. It will also call {\tt PrintDebugInfo()} for any of its child
253     //. components down to a level of {\tt aDepth}.
254     //.
255     //. This member function is available only in the debug version of
256     //. the library.
257 
258     virtual void CheckInvariants() const;
259     //. Aborts if one of the invariants of the object fails.  Use this
260     //. member function to track down bugs.
261     //.
262     //. This member function is available only in the debug version of
263     //. the library.
264 
265 protected:
266 
267     void _PrintDebugInfo(std::ostream& aStrm) const;
268 
269 };
270 
271 #endif
272