Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  
body.h
1 /***************************************************************************
2  copyright : (C) 2002-2008 by Stefano Barbato
3  email : stefano@codesink.org
4 
5  $Id: body.h,v 1.16 2008-10-07 11:06:25 tat Exp $
6  ***************************************************************************/
7 #ifndef _MIMETIC_BODY_H_
8 #define _MIMETIC_BODY_H_
9 #include <string>
10 #include <math.h>
11 #include <mimetic/rfc822/body.h>
12 #include <mimetic/codec/code.h>
13 #include <mimetic/mimeentitylist.h>
14 #include <mimetic/os/file.h>
15 
16 
17 namespace mimetic
18 {
19 
20 /// MIME message body
21 class Body: public Rfc822Body
22 {
23 public:
24  friend class MimeEntity;
25  Body();
26 
27  /**
28  set body content
29  */
30  void set(const std::string&);
31 
32  /**
33  load file as is, no encoding is performed
34  */
35  bool load(const std::string&);
36 
37  /**
38  load file and code it using \p Codec
39  */
40  template<typename Codec>
41  bool load(const std::string&, const Codec&);
42 
43  /**
44  en/decode body content
45  */
46  template<typename Codec>
47  bool code(const Codec&);
48 
49  /**
50  set body \e preamble
51 
52  \sa RFC822
53  */
54  void preamble(const std::string&);
55  /**
56  get body \e preamble
57 
58  \sa RFC822
59  */
60  const std::string& preamble() const;
61  std::string& preamble();
62 
63  /**
64  set body \e epilogue
65 
66  \sa RFC822
67  */
68  void epilogue(const std::string&);
69  /**
70  get body \e epilogue
71 
72  \sa RFC822
73  */
74  const std::string& epilogue() const;
75  std::string& epilogue();
76 
77  /**
78  get body's parts list
79  */
81  const MimeEntityList& parts() const;
82 
83  /**
84  get body's MimeEntity owner
85  */
86  MimeEntity* owner();
87  const MimeEntity* owner() const;
88 
89 protected:
90  void owner(MimeEntity*);
91 protected:
92  MimeEntity* m_owner;
93  MimeEntityList m_parts;
94  std::string m_preamble, m_epilogue;
95 };
96 
97 template<typename Codec>
98 bool Body::load(const std::string& fqn, const Codec& cc)
99 {
100  File in(fqn);
101  if(!in)
102  return false;
103 
104  File::iterator beg = in.begin(), end = in.end();
105  Codec codec(cc);
106 
107  if(codec.codeSizeMultiplier() > 1.0)
108  {
109  /* increase body string size */
110  struct stat st;
111  if(::stat(fqn.c_str(), &st))
112  return false;
113  reserve((size_type)(::ceil(st.st_size * codec.codeSizeMultiplier())));
114  }
115 
116  this->clear();
117  mimetic::code(beg, end, codec, back_inserter(*this) );
118  return true;
119 }
120 
121 
122 template<typename Codec>
123 bool Body::code(const Codec& cc)
124 {
125  // OPTIMIZE
126  std::string coded;
127  Codec codec(cc);
128 
129  if(codec.codeSizeMultiplier() > 1.0)
130  coded.reserve((size_type)::ceil(size() * codec.codeSizeMultiplier()));
131 
132  mimetic::code(begin(), end(), codec, back_inserter(coded) );
133  this->assign(coded);
134  return true;
135 }
136 
137 }
138 
139 #endif
MimeEntityList & parts()
Codecs base class.
Definition: codec_base.h:23
Represent a MIME entity.
Definition: mimeentity.h:37
std::string Rfc822Body
RFC822 body type.
Definition: rfc822/body.h:14
void set(const std::string &)
bool code(const Codec &)
Definition: body.h:123
MIME message body.
Definition: body.h:21
const std::string & epilogue() const
MimeEntity * owner()
const std::string & preamble() const
bool load(const std::string &)
std::list< MimeEntity * > MimeEntityList
List of MimeEntity classes.
Definition: mimeentitylist.h:15