Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

mimeentity.h

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