debian: bumped opensync 0.4x plugin to compat 6 (with similar fixes)
[barry/progweb.git] / src / mimeio.cc
blob5fff0e63176f58fb4f8724329661122ad709548b
1 ///
2 /// \file mimeio.cc
3 /// Storage, parser, builder classes for MIME objects
4 /// (vcard, vevent, vtodo, vjournal)
5 ///
7 /*
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.
23 #include "mimeio.h"
24 #include "vcard.h"
25 #include "vevent.h"
26 #include "vtodo.h"
27 #include "vjournal.h"
28 #include <iostream>
29 #include <fstream>
30 #include <strings.h>
32 using namespace std;
34 namespace Barry {
36 MimeBuilder::MimeBuilder(const std::string &filename)
37 : m_ifs( new std::ifstream(filename.c_str()) )
38 , m_is(*m_ifs)
42 MimeBuilder::MimeBuilder(std::istream &is)
43 : m_is(is)
47 bool MimeBuilder::BuildRecord(DBData &data, size_t &offset,
48 const IConverter *ic)
50 string vrec;
51 vector<string> types;
52 while( ReadMimeRecord(m_is, vrec, types) ) {
53 if( !vrec.size() ) {
54 // end of file
55 return false;
57 else if( IsMember(Sync::vCard::GetVName(), types) ) {
58 Sync::vCard vcard;
59 SetDBData(vcard.ToBarry(vrec.c_str(), 0),
60 data, offset, ic);
61 return true;
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),
67 data, offset, ic);
68 return true;
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),
74 data, offset, ic);
75 return true;
77 else if( IsMember(Sync::vJournal::GetVName(), types) ) {
78 Sync::vJournal vjournal;
79 SetDBData(vjournal.ToBarry(vrec.c_str(), 0),
80 data, offset, ic);
81 return true;
83 else {
84 // read the next one
88 // end of file
89 return false;
92 bool MimeBuilder::FetchRecord(DBData &data, const IConverter *ic)
94 size_t offset = 0;
95 return BuildRecord(data, offset, ic);
98 bool MimeBuilder::EndOfFile() const
100 return !m_is;
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)
107 vrec.clear();
108 types.clear();
110 string line;
112 // ignore whitespace
113 while( getline(is, line) ) {
114 if( strcasecmp(line.substr(0, 6).c_str(), "BEGIN:") == 0 ) {
115 vrec += line;
116 vrec += "\n";
117 types.push_back(line.substr(6));
118 break;
122 if( !vrec.size() )
123 return false;
125 // load until end
126 int count = 0;
127 while( getline(is, line) ) {
128 // end on blank lines
129 if( !line.size() )
130 return true;
132 vrec += line;
133 vrec += "\n";
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!
147 count++;
148 if( count > 200 )
149 return false;
151 // assume that end of file is the same as "blank line"
152 return true;
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 )
161 return true;
163 return false;
166 } // namespace Barry