3 /// Storage, parser, builder classes for MIME objects
4 /// (vcard, vevent, vtodo, vjournal)
8 Copyright (C) 2010-2011, 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::vJournal vjournal
;
79 SetDBData(vjournal
.ToBarry(vrec
.c_str(), 0),
92 bool MimeBuilder::FetchRecord(DBData
&data
, const IConverter
*ic
)
95 return BuildRecord(data
, offset
, ic
);
98 bool MimeBuilder::EndOfFile() const
103 // return false at end of file, true if a record was read
104 bool MimeBuilder::ReadMimeRecord(std::istream
&is
, std::string
&vrec
,
105 std::vector
<std::string
> &types
)
113 while( getline(is
, line
) ) {
114 if( strcasecmp(line
.substr(0, 6).c_str(), "BEGIN:") == 0 ) {
117 types
.push_back(line
.substr(6));
127 while( getline(is
, line
) ) {
128 // end on blank lines
135 // pick up innermost BEGIN line
136 if( strcasecmp(line
.substr(0, 6).c_str(), "BEGIN:") == 0 ) {
137 string type
= line
.substr(6);
138 while( type
.size() && type
[type
.size()-1] == '\r' ) {
139 type
= type
.substr(0, type
.size()-1);
141 types
.push_back(type
);
144 // place an upper limit on the number of lines...
145 // since a MIME data block shouldn't be more than
146 // a few pages of lines!
151 // assume that end of file is the same as "blank line"
155 bool MimeBuilder::IsMember(const std::string
&item
,
156 const std::vector
<std::string
> &types
)
158 std::vector
<std::string
>::const_iterator i
= types
.begin();
159 for( ; i
!= types
.end(); ++i
) {
160 if( strcasecmp(i
->c_str(), item
.c_str()) == 0 )