Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

body.h

00001 /***************************************************************************
00002     copyright            : (C) 2002-2008 by Stefano Barbato
00003     email                : stefano@codesink.org
00004 
00005     $Id: body.h,v 1.16 2008-10-07 11:06:25 tat Exp $
00006  ***************************************************************************/
00007 #ifndef _MIMETIC_BODY_H_
00008 #define _MIMETIC_BODY_H_
00009 #include <string>
00010 #include <math.h>
00011 #include <mimetic/rfc822/body.h>
00012 #include <mimetic/codec/code.h>
00013 #include <mimetic/mimeentitylist.h>
00014 #include <mimetic/os/file.h>
00015 
00016 
00017 namespace mimetic
00018 {
00019 
00020 /// MIME message body
00021 class Body: public Rfc822Body
00022 {
00023 public:
00024     friend class MimeEntity;
00025     Body();
00026 
00027     /**
00028       set body content
00029      */
00030     void set(const std::string&);
00031 
00032     /**
00033       load file as is, no encoding is performed
00034      */
00035     bool load(const std::string&);
00036 
00037     /**
00038       load file and code it using \p Codec 
00039      */
00040     template<typename Codec>
00041     bool load(const std::string&, const Codec&);
00042     
00043     /**
00044       en/decode body content
00045      */
00046     template<typename Codec>
00047     bool code(const Codec&);
00048     
00049     /**
00050       set body \e preamble 
00051 
00052       \sa RFC822
00053      */
00054     void preamble(const std::string&);
00055     /**
00056       get body \e preamble 
00057 
00058       \sa RFC822
00059      */
00060     const std::string& preamble() const;
00061     std::string& preamble();
00062     
00063     /**
00064       set body \e epilogue 
00065 
00066       \sa RFC822
00067      */
00068     void epilogue(const std::string&);
00069     /**
00070       get body \e epilogue 
00071 
00072       \sa RFC822
00073      */
00074     const std::string& epilogue() const;
00075     std::string& epilogue();
00076     
00077     /**
00078       get body's parts list 
00079      */
00080     MimeEntityList& parts();
00081     const MimeEntityList& parts() const;
00082 
00083     /**
00084       get body's MimeEntity owner
00085      */
00086     MimeEntity* owner();
00087     const MimeEntity* owner() const;
00088     
00089 protected:
00090     void owner(MimeEntity*);
00091 protected:
00092     MimeEntity* m_owner;
00093     MimeEntityList m_parts;
00094     std::string m_preamble, m_epilogue;
00095 };
00096 
00097 template<typename Codec>
00098 bool Body::load(const std::string& fqn, const Codec& cc)
00099 {
00100     File in(fqn);
00101     if(!in)
00102         return false;
00103 
00104     File::iterator beg = in.begin(), end = in.end();
00105     Codec codec(cc);
00106 
00107     if(codec.codeSizeMultiplier() > 1.0)
00108     {
00109         /* increase body string size */
00110         struct stat st;
00111         if(::stat(fqn.c_str(), &st))
00112             return false;
00113         reserve((size_type)(::ceil(st.st_size * codec.codeSizeMultiplier())));
00114     }
00115 
00116     this->clear();
00117     mimetic::code(beg, end, codec, back_inserter(*this) );
00118     return true;
00119 }
00120 
00121 
00122 template<typename Codec>
00123 bool Body::code(const Codec& cc)
00124 {
00125     // OPTIMIZE
00126     std::string coded;
00127     Codec codec(cc);
00128 
00129     if(codec.codeSizeMultiplier() > 1.0)
00130         coded.reserve((size_type)::ceil(size() * codec.codeSizeMultiplier()));
00131 
00132     mimetic::code(begin(), end(), codec, back_inserter(coded) );
00133     this->assign(coded);
00134     return true;
00135 }
00136 
00137 }
00138 
00139 #endif