~ubuntu-branches/ubuntu/natty/mimetic/natty

« back to all changes in this revision

Viewing changes to mimetic/rfc822/field.cxx

  • Committer: Bazaar Package Importer
  • Author(s): gregor herrmann
  • Date: 2006-06-16 13:16:07 UTC
  • Revision ID: james.westby@ubuntu.com-20060616131607-245mqjypkjuahq6b
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    copyright            : (C) 2002-2005 by Stefano Barbato
 
3
    email                : stefano@codesink.org
 
4
 
 
5
    $Id: field.cxx,v 1.4 2005/02/24 13:08:08 tat Exp $
 
6
 ***************************************************************************/
 
7
 
 
8
/***************************************************************************
 
9
 *                                                                         *
 
10
 *   This program is free software; you can redistribute it and/or modify  *
 
11
 *   it under the terms of the GNU General Public License as published by  *
 
12
 *   the Free Software Foundation; either version 2 of the License, or     *
 
13
 *   (at your option) any later version.                                   *
 
14
 *                                                                         *
 
15
 ***************************************************************************/
 
16
#include <mimetic/rfc822/field.h>
 
17
#include <mimetic/strutils.h>
 
18
#include <mimetic/utils.h>
 
19
#include <mimetic/contenttype.h>
 
20
#include <mimetic/rfc822/mailboxlist.h>
 
21
 
 
22
namespace mimetic
 
23
{
 
24
 
 
25
using namespace std;
 
26
 
 
27
// static init
 
28
const Field Field::null;
 
29
 
 
30
/**
 
31
    Constructs a null Field
 
32
*/
 
33
Field::Field()
 
34
:m_pValue(0)
 
35
{
 
36
}
 
37
 
 
38
 
 
39
/**
 
40
    Parses \p line and sets \e name, \e value and \e valueText of *this
 
41
    \param line input field string as defined in RFC822 (fieldname: text)
 
42
*/
 
43
Field::Field(const string& line)
 
44
:m_pValue(0)
 
45
{
 
46
    string::size_type colon = line.find(':');
 
47
    if(colon != string::npos)
 
48
    {
 
49
        m_name.assign(line.begin(), line.begin() + colon);
 
50
        unsigned int i;
 
51
        for(i = 1 + colon; i < line.length() - 1 && line[i] == ' '; ++i)
 
52
            ; // skip spaces before field-body
 
53
        string val(line.begin() +i, line.end());
 
54
        value(val);
 
55
    }
 
56
}
 
57
 
 
58
 
 
59
/**
 
60
    Initialize *this with \p n and \p v
 
61
    \param n field %name
 
62
    \param v content of the %field
 
63
*/
 
64
Field::Field(const string& n , const string& v)
 
65
:m_pValue(0)
 
66
{
 
67
    m_name = n;
 
68
    m_pValue = new StringFieldValue(v);
 
69
}
 
70
 
 
71
/**
 
72
    Copy constructor
 
73
*/
 
74
Field::Field(const Field& r)
 
75
:m_name(r.m_name), m_pValue(0)
 
76
{
 
77
    if(r.m_pValue)
 
78
        m_pValue = r.m_pValue->clone();
 
79
}
 
80
 
 
81
Field& Field::operator=(const Field& r)
 
82
{
 
83
    m_name = r.m_name;
 
84
    if(m_pValue)
 
85
    {
 
86
        delete m_pValue;
 
87
        m_pValue = 0;
 
88
    }
 
89
    if(r.m_pValue)
 
90
        m_pValue = r.m_pValue->clone();
 
91
    return *this;
 
92
}
 
93
 
 
94
/**
 
95
    Destructor
 
96
*/
 
97
Field::~Field()
 
98
{
 
99
    if(m_pValue)
 
100
    {
 
101
        delete m_pValue;
 
102
        m_pValue = 0;
 
103
    }
 
104
}
 
105
 
 
106
/**
 
107
    Sets the field name to \p n
 
108
    \param name new %field %name
 
109
*/
 
110
void Field::name(const string& name)
 
111
{
 
112
    m_name = name;
 
113
    if(m_pValue != 0)
 
114
    {
 
115
        delete m_pValue;
 
116
        m_pValue = 0;
 
117
    }
 
118
}
 
119
 
 
120
/**
 
121
    Sets the field value to \p v
 
122
    \param val new %value %name
 
123
*/
 
124
void Field::value(const string& val)
 
125
{
 
126
    if(m_pValue == 0)
 
127
        m_pValue = new StringFieldValue(val);
 
128
    else
 
129
        m_pValue->set(val);
 
130
}
 
131
 
 
132
/**
 
133
    Returns the field name
 
134
*/
 
135
const istring& Field::name() const
 
136
{
 
137
    return m_name;
 
138
}
 
139
 
 
140
 
 
141
 
 
142
 
 
143
/**
 
144
    Returns the field value.
 
145
*/
 
146
string Field::value() const
 
147
{
 
148
    if(m_pValue == 0)
 
149
        return nullstring;
 
150
    return m_pValue->str();
 
151
}
 
152
 
 
153
std::ostream& operator<<(std::ostream& os, const Field& f)
 
154
{
 
155
    return f.write(os, 0);
 
156
}
 
157
 
 
158
ostream& Field::write(ostream& os, unsigned int fold) const
 
159
{
 
160
    // default folding idiot-algorithm
 
161
    // override to customize
 
162
    if(fold)
 
163
    {
 
164
        int i, sp;
 
165
        string ostr = name() + ": " + value();
 
166
 
 
167
        while(ostr.length() > fold)
 
168
        {
 
169
            for(i = 0, sp = 0; i < ostr.length(); ++i)
 
170
            {
 
171
                if(isblank(ostr[i]))
 
172
                    sp = i; // last blank found
 
173
 
 
174
                if(i >= fold && sp)
 
175
                {
 
176
                    os.write(ostr.c_str(), sp);
 
177
                    os << crlf << "\t";
 
178
                    ostr.erase(0, 1+sp);
 
179
                    break;
 
180
                }
 
181
            }
 
182
            if(sp == 0)
 
183
                break; // can't fold anymore
 
184
        }
 
185
 
 
186
        os << ostr;
 
187
        return os;
 
188
    } else
 
189
        return os << name() << ": " << value();
 
190
}
 
191
 
 
192
 
 
193
}