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

« back to all changes in this revision

Viewing changes to mimetic/rfc822/group.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: group.cxx,v 1.2 2005/02/23 10:26:15 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/group.h>
 
17
#include <mimetic/strutils.h>
 
18
 
 
19
namespace mimetic
 
20
{
 
21
 
 
22
using namespace std;
 
23
 
 
24
 
 
25
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 
26
//    Rfc822::Group
 
27
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 
28
//     group       =  phrase ":" [#mailbox] ";"
 
29
Group::Group()
 
30
{
 
31
}
 
32
 
 
33
Group::Group(const char* cstr)
 
34
{
 
35
    set(cstr);
 
36
}
 
37
 
 
38
Group::Group(const string& text)
 
39
{
 
40
    set(text);
 
41
}
 
42
 
 
43
static string::size_type find_not_in_quote(const string& s, const string::value_type& c)
 
44
{
 
45
    int len = s.length();
 
46
    bool in_dquote = false;
 
47
    for(int i =0; i < len; ++i)
 
48
    {
 
49
        if(s[i] == '"')
 
50
            in_dquote = !in_dquote;
 
51
        else if( s[i] == c && !in_dquote) {
 
52
            return i;
 
53
        }
 
54
    }
 
55
    return string::npos;
 
56
}
 
57
 
 
58
std::string Group::str() const
 
59
{
 
60
    string rs = m_name;
 
61
    const_iterator bit = begin(), first = bit, eit = end();
 
62
    for(; bit != eit; ++bit)
 
63
    {
 
64
        if(bit != first)
 
65
            rs += ",";
 
66
        rs += bit->str();
 
67
    }
 
68
    return rs + ";";
 
69
}
 
70
 
 
71
void Group::set(const string& text)
 
72
{
 
73
    m_text = text;
 
74
    size_type colon = find_not_in_quote(m_text, ':');
 
75
    if(colon == string::npos)
 
76
        return; // empty or invalid
 
77
    bool in_dquote = false;
 
78
    int in_par = 0, in_angle = 0;
 
79
    string mailbox;
 
80
    string::iterator p = m_text.begin(), start;
 
81
    m_name.assign(m_text, 0, colon);
 
82
    m_name = remove_external_blanks(m_name);
 
83
    for(p += ++colon, start = p; p < m_text.end(); ++p)
 
84
    {
 
85
        if(*p == ';' || *p == ',')
 
86
        { 
 
87
            if(in_dquote || in_par || in_angle)
 
88
                continue;
 
89
            string mbx(start, p);
 
90
            mbx = remove_external_blanks(mbx);
 
91
            push_back(Mailbox(mbx));
 
92
            if(*p == ';')
 
93
                return;
 
94
            start = p + 1;
 
95
        } else if(*p == '"') {
 
96
            in_dquote = !in_dquote;
 
97
        } else if(*p == '<') {
 
98
            ++in_angle;
 
99
        } else if(*p == '>') {
 
100
            --in_angle;
 
101
        } else if(*p == '(') {
 
102
            ++in_par;
 
103
        } else if(*p == ')') {
 
104
            --in_par;
 
105
        } 
 
106
    }
 
107
    // trailing ';' missing
 
108
    push_back(Mailbox(string(start, p-1)));
 
109
}
 
110
 
 
111
string Group::name(int bCanonical) const
 
112
{    return (bCanonical ? canonical(m_name) : m_name);    }
 
113
 
 
114
void Group::name(const string& name)
 
115
{    m_name = name;    }
 
116
 
 
117
FieldValue* Group::clone() const
 
118
{
 
119
    return new Group(*this);
 
120
}
 
121
 
 
122
}