Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

mimeentity.h

00001 /***************************************************************************
00002     copyright            : (C) 2002-2008 by Stefano Barbato
00003     email                : stefano@codesink.org
00004 
00005     $Id: mimeentity.h,v 1.29 2008-10-07 11:06:25 tat Exp $
00006  ***************************************************************************/
00007 #ifndef _MIMETIC_MIMEENTITY_H_
00008 #define _MIMETIC_MIMEENTITY_H_
00009 #include <string>
00010 #include <iostream>
00011 #include <streambuf>
00012 #include <fstream>
00013 #include <iterator>
00014 #include <algorithm>
00015 #include <mimetic/strutils.h>
00016 #include <mimetic/utils.h>
00017 #include <mimetic/contenttype.h>
00018 #include <mimetic/contenttransferencoding.h>
00019 #include <mimetic/contentdisposition.h>
00020 #include <mimetic/mimeversion.h>
00021 #include <mimetic/mimeentitylist.h>
00022 #include <mimetic/codec/codec.h>
00023 #include <mimetic/os/file.h>
00024 #include <mimetic/header.h>
00025 #include <mimetic/body.h>
00026 #include <mimetic/parser/itparserdecl.h>
00027 #include <mimetic/streambufs.h>
00028 
00029 
00030 namespace mimetic
00031 {
00032 
00033 class MimeEntity;
00034 
00035 
00036 /// Represent a MIME entity    
00037 class MimeEntity
00038 {
00039     friend class Body;
00040     friend class MimeEntityLoader;
00041     typedef std::list<std::string> BoundaryList;
00042     typedef unsigned long int size_type;
00043 public:
00044     /**
00045      *  Blank MIME entity
00046      */
00047     MimeEntity();
00048     /**
00049      *  Parse [beg, end] and build entity based on content
00050      */
00051     template<typename Iterator>
00052     MimeEntity(Iterator beg, Iterator end, int mask = imNone);
00053     /**
00054      *  Parse istream and build entity based on content
00055      */
00056     MimeEntity(std::istream&);
00057 
00058     virtual ~MimeEntity();
00059 
00060     /**
00061      * copy text rapresentation of the MimeEntity to the output iterator
00062      */
00063     template<typename OutputIt>
00064     size_type copy(OutputIt out);
00065 
00066     Header& header();
00067     const Header& header() const;
00068 
00069     Body& body();
00070     const Body& body() const;
00071 
00072     /** 
00073      * single step load functions: parse the input provided and build the
00074      * entity
00075      *
00076      * use load(..., mask) to ignore some part of the message when it's
00077      * not needed saving memory space and execution time
00078      */
00079     template<typename Iterator>
00080     void load(Iterator, Iterator, int mask = imNone);
00081     void load(std::istream&, int mask = imNone);
00082 
00083     /**
00084      * helper functions: return header().hasField(str)
00085      */
00086     bool hasField(const std::string&) const;
00087 
00088     /**
00089      * returns entity size 
00090      * Note: this function is slow, use it if you really need
00091      */
00092     size_type size() const;
00093     friend std::ostream& operator<<(std::ostream&, const MimeEntity&);
00094 protected:
00095     void commonInit();
00096 
00097     virtual std::ostream& write(std::ostream&, const char* eol = 0) const;
00098 
00099 protected:
00100     Header m_header;
00101     Body m_body;
00102     size_type m_lines;
00103     size_type m_size;
00104 
00105 private:
00106     //MimeEntity(const MimeEntity&);
00107     //MimeEntity& operator=(const MimeEntity&);
00108 };
00109 
00110 
00111 
00112 template<typename Iterator>
00113 MimeEntity::MimeEntity(Iterator bit, Iterator eit, int mask)
00114 {
00115     commonInit();
00116     load(bit, eit, mask);
00117 }
00118 
00119 
00120 template<typename Iterator>
00121 void MimeEntity::load(Iterator bit, Iterator eit, int mask)
00122 {
00123     IteratorParser<Iterator, 
00124         typename std::iterator_traits<Iterator>::iterator_category> prs(*this);
00125     prs.iMask(mask);
00126     prs.run(bit, eit);
00127 }
00128 
00129 template<typename OutputIt>
00130 MimeEntity::size_type MimeEntity::copy(OutputIt out)
00131 {
00132     passthrough_streambuf<OutputIt> psb(out);
00133     std::ostream os(&psb);
00134     os << *this;
00135     return psb.size();
00136 }
00137 
00138 }
00139 
00140 #endif