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

  ViewVC Help
Powered by ViewVC 1.1.26