/[debian]/mimetic/branches/upstream/current/mimetic/rfc822/field.cxx
ViewVC logotype

Contents of /mimetic/branches/upstream/current/mimetic/rfc822/field.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1729 - (show annotations)
Fri May 15 15:30:29 2009 UTC (12 years, 2 months ago) by gregoa
File size: 4635 byte(s)
[svn-upgrade] Integrating new upstream version, mimetic (0.9.6)
1 /***************************************************************************
2 copyright : (C) 2002-2008 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: field.cxx,v 1.7 2009-02-27 15:56:34 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 int in_quote, prev, skip;
161
162 // default folding idiot-algorithm
163 // override to customize
164 if(fold)
165 {
166 int i, sp;
167 string ostr = name() + ": " + value();
168
169 // skip the "fieldname: " part just on the first inner iteration
170 skip = name().length() + 2;
171
172 while(ostr.length() > fold)
173 {
174 prev = 0; // prev char processed
175 in_quote = 0;
176 sp = 0;
177
178 for(i = skip; i < ostr.length(); ++i)
179 {
180 if(ostr[i] == '"' && prev != '\\')
181 in_quote = !in_quote;
182
183 if(!in_quote && isblank(ostr[i]))
184 sp = i; // last blank found
185
186 if(i >= fold && sp)
187 {
188 os.write(ostr.c_str(), sp);
189 ostr.erase(0, 1+sp);
190 if(ostr.length() && !utils::string_is_blank(ostr))
191 os << crlf << "\t"; // keep folding
192 break;
193 }
194
195 prev = ostr[i];
196 }
197 if(sp == 0)
198 break; // can't fold anymore
199 skip = 0;
200 }
201
202 os << ostr;
203 return os;
204 } else
205 return os << name() << ": " << value();
206 }
207
208
209 }

  ViewVC Help
Powered by ViewVC 1.1.26