debian: added copyrights for the spanish and french translators
[barry.git] / src / mimeio.cc
blobf9cca9ce1b8d066afa570d511419c18fb3c53ffb
1 ///
2 /// \file mimeio.cc
3 /// Storage, parser, builder classes for MIME objects
4 /// (vcard, vevent, vtodo, vjournal)
5 ///
7 /*
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.
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::vTimeConverter vtc;
79 Sync::vJournal vjournal(vtc);
80 SetDBData(vjournal.ToBarry(vrec.c_str(), 0),
81 data, offset, ic);
82 return true;
84 else {
85 // read the next one
89 // end of file
90 return false;
93 bool MimeBuilder::FetchRecord(DBData &data, const IConverter *ic)
95 size_t offset = 0;
96 return BuildRecord(data, offset, ic);
99 bool MimeBuilder::EndOfFile() const
101 return !m_is;
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)
108 vrec.clear();
109 types.clear();
111 string line;
113 // ignore whitespace
114 while( getline(is, line) ) {
115 if( strcasecmp(line.substr(0, 6).c_str(), "BEGIN:") == 0 ) {
116 vrec += line;
117 vrec += "\n";
118 types.push_back(line.substr(6));
119 break;
123 if( !vrec.size() )
124 return false;
126 // load until end
127 int count = 0;
128 while( getline(is, line) ) {
129 // end on blank lines
130 if( !line.size() )
131 return true;
133 vrec += line;
134 vrec += "\n";
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!
148 count++;
149 if( count > 200 )
150 return false;
152 // assume that end of file is the same as "blank line"
153 return true;
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 )
162 return true;
164 return false;
167 } // namespace Barry