3 /// Storage, parser, builder classes for MIME objects
4 /// (vcard, vevent, vtodo, vjournal)
8 Copyright (C) 2010-2012, Net Direct Inc. (http://www.netdirect.ca/)
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.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
36 MimeBuilder::MimeBuilder(const std::string
&filename
)
37 : m_ifs( new std::ifstream(filename
.c_str()) )
42 MimeBuilder::MimeBuilder(std::istream
&is
)
47 bool MimeBuilder::BuildRecord(DBData
&data
, size_t &offset
,
52 while( ReadMimeRecord(m_is
, vrec
, types
) ) {
57 else if( IsMember(Sync::vCard::GetVName(), types
) ) {
59 SetDBData(vcard
.ToBarry(vrec
.c_str(), 0),
63 else if( IsMember(Sync::vCalendar::GetVName(), types
) ) {
64 Sync::vTimeConverter vtc
;
65 Sync::vCalendar
vcal(vtc
);
66 SetDBData(vcal
.ToBarry(vrec
.c_str(), 0),
70 else if( IsMember(Sync::vTodo::GetVName(), types
) ) {
71 Sync::vTimeConverter vtc
;
72 Sync::vTodo
vtodo(vtc
);
73 SetDBData(vtodo
.ToBarry(vrec
.c_str(), 0),
77 else if( IsMember(Sync::vJournal::GetVName(), types
) ) {
78 Sync::vTimeConverter vtc
;
79 Sync::vJournal
vjournal(vtc
);
80 SetDBData(vjournal
.ToBarry(vrec
.c_str(), 0),
93 bool MimeBuilder::FetchRecord(DBData
&data
, const IConverter
*ic
)
96 return BuildRecord(data
, offset
, ic
);
99 bool MimeBuilder::EndOfFile() const
104 // return false at end of file, true if a record was read
105 bool MimeBuilder::ReadMimeRecord(std::istream
&is
, std::string
&vrec
,
106 std::vector
<std::string
> &types
)
114 while( getline(is
, line
) ) {
115 if( strcasecmp(line
.substr(0, 6).c_str(), "BEGIN:") == 0 ) {
118 types
.push_back(line
.substr(6));
128 while( getline(is
, line
) ) {
129 // end on blank lines
136 // pick up innermost BEGIN line
137 if( strcasecmp(line
.substr(0, 6).c_str(), "BEGIN:") == 0 ) {
138 string type
= line
.substr(6);
139 while( type
.size() && type
[type
.size()-1] == '\r' ) {
140 type
= type
.substr(0, type
.size()-1);
142 types
.push_back(type
);
145 // place an upper limit on the number of lines...
146 // since a MIME data block shouldn't be more than
147 // a few pages of lines!
152 // assume that end of file is the same as "blank line"
156 bool MimeBuilder::IsMember(const std::string
&item
,
157 const std::vector
<std::string
> &types
)
159 std::vector
<std::string
>::const_iterator i
= types
.begin();
160 for( ; i
!= types
.end(); ++i
) {
161 if( strcasecmp(i
->c_str(), item
.c_str()) == 0 )