/[debian]/mimetic/branches/upstream/current/mimetic/mimeentity.cxx
ViewVC logotype

Contents of /mimetic/branches/upstream/current/mimetic/mimeentity.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 128 - (show annotations)
Sat Feb 25 16:49:20 2006 UTC (15 years, 5 months ago) by gregoa
File size: 4070 byte(s)
[svn-inject] Installing original source of mimetic
1 /***************************************************************************
2 copyright : (C) 2002-2005 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: mimeentity.cxx,v 1.4 2005/03/07 15:35:08 tat Exp $
6 ***************************************************************************/
7
8 /***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16 #include <iostream>
17 #include <fstream>
18 #include <streambuf>
19 #include <algorithm>
20 #include <mimetic/mimeentity.h>
21 #include <mimetic/tokenizer.h>
22 #include <mimetic/strutils.h>
23 #include <mimetic/libconfig.h>
24 #include <mimetic/codec/codec.h>
25 #include <mimetic/parser/itparser.h>
26 #include <mimetic/streambufs.h>
27
28 namespace mimetic
29 {
30
31 using namespace std;
32
33 void MimeEntity::load(istream& is, int mask)
34 {
35 typedef istreambuf_iterator<char> it_type;
36 typedef it_type::iterator_category it_cat;
37 IteratorParser<it_type, it_cat> prs(*this);
38 prs.iMask(mask);
39 prs.run( it_type(is), it_type());
40 }
41
42 bool MimeEntity::hasField(const string& name) const
43 {
44 return m_header.hasField(name);
45 }
46
47 ostream& MimeEntity::write(ostream& os, const char* eol) const
48 {
49 enum { max_line_len = 76 };
50 if(eol != 0)
51 {
52 // TODO
53 //build nl2localnl filter streambuf
54 }
55 // header field
56 Header::const_iterator hbit = header().begin(), heit = header().end();
57 for(; hbit != heit; ++hbit)
58 hbit->write(os, max_line_len) << crlf;
59 const ContentType& ct = m_header.contentType();
60 // body
61 if(ct.isMultipart())
62 {
63 string boundary = "--" + ct.param("boundary");
64 if(body().preamble().length())
65 os << crlf << body().preamble();
66 // opening boundary
67 if(body().parts().size() == 0)
68 os << crlf << boundary << crlf;
69 MimeEntityList::const_iterator bit, eit;
70 bit = body().parts().begin(), eit = body().parts().end();
71 for(; bit != eit; ++bit)
72 {
73 os << crlf << boundary << crlf;
74 MimeEntity* pMe = *bit;
75 os << *pMe;
76 }
77 // closing boundary
78 os << crlf << boundary + "--" << crlf;
79 if(body().epilogue().length())
80 os << body().epilogue();
81 } else if(ct.type() == "message" && ct.subtype() == "rfc822") {
82 MimeEntityList::const_iterator bit, eit;
83 bit = body().parts().begin(), eit = body().parts().end();
84 for(; bit != eit; ++bit)
85 {
86 os << crlf;
87 MimeEntity* pMe = *bit;
88 os << *pMe;
89 }
90 } else {
91 os << crlf << body();
92 }
93 os.flush();
94 return os;
95 }
96
97 ostream& operator<<(ostream& os, const MimeEntity& m)
98 {
99 return m.write(os);
100 }
101
102
103 // called by all constructors()
104 void MimeEntity::commonInit()
105 {
106 m_body.owner(this);
107 }
108
109
110 MimeEntity::MimeEntity()
111 {
112 commonInit();
113 }
114
115
116 MimeEntity::MimeEntity(std::istream& is)
117 {
118 commonInit();
119 load(is);
120 }
121
122 MimeEntity::~MimeEntity()
123 {
124 MimeEntityList::iterator bit = m_body.parts().begin(),
125 eit = m_body.parts().end();
126 for(; bit != eit; ++bit)
127 if(*bit)
128 delete *bit;
129 m_body.clear();
130 }
131
132 Header& MimeEntity::header()
133 {
134 return m_header;
135 }
136
137 const Header& MimeEntity::header() const
138 {
139 return m_header;
140 }
141
142 Body& MimeEntity::body()
143 {
144 return m_body;
145 }
146
147 const Body& MimeEntity::body() const
148 {
149 return m_body;
150 }
151
152 MimeEntity::size_type MimeEntity::size() const
153 {
154 count_streambuf csb;
155 ostream os(&csb);
156 os << *this;
157 return csb.size();
158 }
159
160
161
162
163 }

  ViewVC Help
Powered by ViewVC 1.1.26