Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

body.h

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