ppp: minor cleanups to match other chatscripts order and documentation
[barry.git] / src / mimeio.cc
blob43fe2886899394bf55cf0027116bb08aae3e061d
1 ///
2 /// \file mimeio.cc
3 /// Storage, parser, builder classes for MIME objects
4 /// (vcard, vevent, vtodo, vjournal)
5 ///
7 /*
8 Copyright (C) 2010, 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, type;
51 while( ReadMimeRecord(m_is, vrec, type) ) {
52 if( !vrec.size() ) {
53 // end of file
54 return false;
56 else if( strcasecmp(type.c_str(), Sync::vCard::GetVName()) == 0 ) {
57 Sync::vCard vcard;
58 SetDBData(vcard.ToBarry(vrec.c_str(), 0),
59 data, offset, ic);
60 return true;
62 else if( strcasecmp(type.c_str(), Sync::vCalendar::GetVName()) == 0 ) {
63 Sync::vTimeConverter vtc;
64 Sync::vCalendar vcal(vtc);
65 SetDBData(vcal.ToBarry(vrec.c_str(), 0),
66 data, offset, ic);
67 return true;
69 else if( strcasecmp(type.c_str(), Sync::vTodo::GetVName()) == 0 ) {
70 Sync::vTimeConverter vtc;
71 Sync::vTodo vtodo(vtc);
72 SetDBData(vtodo.ToBarry(vrec.c_str(), 0),
73 data, offset, ic);
74 return true;
76 else if( strcasecmp(type.c_str(), Sync::vJournal::GetVName()) == 0 ) {
77 Sync::vJournal vjournal;
78 SetDBData(vjournal.ToBarry(vrec.c_str(), 0),
79 data, offset, ic);
80 return true;
82 else {
83 // read the next one
87 // end of file
88 return false;
91 bool MimeBuilder::FetchRecord(DBData &data, const IConverter *ic)
93 size_t offset = 0;
94 return BuildRecord(data, offset, ic);
97 bool MimeBuilder::EndOfFile() const
99 return !m_is;
102 // return false at end of file, true if a record was read
103 bool MimeBuilder::ReadMimeRecord(std::istream &is, std::string &vrec,
104 std::string &type)
106 vrec.clear();
107 type.clear();
109 string line;
111 // ignore whitespace
112 while( getline(is, line) ) {
113 if( strcasecmp(line.substr(0, 6).c_str(), "BEGIN:") == 0 ) {
114 vrec += line;
115 vrec += "\n";
116 type = line.substr(6);
117 break;
121 if( !vrec.size() )
122 return false;
124 // load until end
125 int count = 0;
126 while( getline(is, line) ) {
127 // end on blank lines
128 if( !line.size() )
129 return true;
131 vrec += line;
132 vrec += "\n";
134 // pick up innermost BEGIN line
135 if( strcasecmp(line.substr(0, 6).c_str(), "BEGIN:") == 0 ) {
136 type = line.substr(6);
137 while( type.size() && type[type.size()-1] == '\r' ) {
138 type = type.substr(0, type.size()-1);
142 // place an upper limit on the number of lines...
143 // since a MIME data block shouldn't be more than
144 // a few pages of lines!
145 count++;
146 if( count > 200 )
147 return false;
149 // assume that end of file is the same as "blank line"
150 return true;
153 } // namespace Barry