File: body.h

package info (click to toggle)
mimetic 0.9.6-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,656 kB
  • ctags: 1,746
  • sloc: cpp: 11,240; sh: 9,080; makefile: 115
file content (147 lines) | stat: -rw-r--r-- 3,474 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/***************************************************************************
    copyright            : (C) 2002-2008 by Stefano Barbato
    email                : stefano@codesink.org

    $Id: body.h,v 1.16 2008-10-07 11:06:25 tat Exp $
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#ifndef _MIMETIC_BODY_H_
#define _MIMETIC_BODY_H_
#include <string>
#include <math.h>
#include <mimetic/rfc822/body.h>
#include <mimetic/codec/code.h>
#include <mimetic/mimeentitylist.h>
#include <mimetic/os/file.h>


namespace mimetic
{

/// MIME message body
class Body: public Rfc822Body
{
public:
    friend class MimeEntity;
    Body();

    /**
      set body content
     */
    void set(const std::string&);

    /**
      load file as is, no encoding is performed
     */
    bool load(const std::string&);

    /**
      load file and code it using \p Codec 
     */
    template<typename Codec>
    bool load(const std::string&, const Codec&);
    
    /**
      en/decode body content
     */
    template<typename Codec>
    bool code(const Codec&);
    
    /**
      set body \e preamble 

      \sa RFC822
     */
    void preamble(const std::string&);
    /**
      get body \e preamble 

      \sa RFC822
     */
    const std::string& preamble() const;
    std::string& preamble();
    
    /**
      set body \e epilogue 

      \sa RFC822
     */
    void epilogue(const std::string&);
    /**
      get body \e epilogue 

      \sa RFC822
     */
    const std::string& epilogue() const;
    std::string& epilogue();
    
    /**
      get body's parts list 
     */
    MimeEntityList& parts();
    const MimeEntityList& parts() const;

    /**
      get body's MimeEntity owner
     */
    MimeEntity* owner();
    const MimeEntity* owner() const;
    
protected:
    void owner(MimeEntity*);
protected:
    MimeEntity* m_owner;
    MimeEntityList m_parts;
    std::string m_preamble, m_epilogue;
};

template<typename Codec>
bool Body::load(const std::string& fqn, const Codec& cc)
{
    File in(fqn);
    if(!in)
        return false;

    File::iterator beg = in.begin(), end = in.end();
    Codec codec(cc);

    if(codec.codeSizeMultiplier() > 1.0)
    {
        /* increase body string size */
        struct stat st;
        if(::stat(fqn.c_str(), &st))
            return false;
        reserve((size_type)(::ceil(st.st_size * codec.codeSizeMultiplier())));
    }

    mimetic::code(beg, end, codec, back_inserter(*this) );
    return true;
}


template<typename Codec>
bool Body::code(const Codec& cc)
{
    // OPTIMIZE
    std::string coded;
    Codec codec(cc);

    if(codec.codeSizeMultiplier() > 1.0)
        coded.reserve((size_type)::ceil(size() * codec.codeSizeMultiplier()));

    mimetic::code(begin(), end(), codec, back_inserter(coded) );
    this->assign(coded);
    return true;
}

}

#endif