This repository has been archived by the owner on Dec 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcardreader.cpp
134 lines (116 loc) · 5.35 KB
/
vcardreader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "vcardreader.h"
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
std::vector<contact> VCardReader::readFromFile(const std::string &filename)
{
std::vector<contact> contacts;
std::ifstream vcardfile(filename);
std::string line;
std::string tag;
while (std::getline(vcardfile, line)) {
if (!line.compare(0, 11, "BEGIN:VCARD")) {
std::cout << "next VCARD" << std::endl;
contact new_contact;
for (;vcardfile.good() && line.compare(0, 9, "END:VCARD"); std::getline(vcardfile, line)) {
//std::cout << line << std::endl;
std::istringstream iline(line);
std::getline(iline, tag, ':');
std::string param;
int s = tag.find(';');
if (s != tag.npos) {
param = tag.substr(s+1);
tag.resize(s);
}
//std::cout << "tag: " << tag << " param: " << param << std::endl;
if (tag == "N") {
std::string last_name;
std::getline(iline, last_name, ';');
if (!last_name.empty())
new_contact.setLastName(last_name);
std::string first_name;
std::getline(iline, first_name, ';');
if (!first_name.empty())
new_contact.setFirstName(first_name);
std::string middle_name;
std::getline(iline, middle_name, ';');
if (!middle_name.empty())
new_contact.setMiddleName(middle_name);
std::string title;
std::getline(iline, title, ';');
if (!title.empty())
new_contact.setTitle(title);
} else if (tag == "FN") {
std::string formatted_name;
std::getline(iline, formatted_name);
if (!formatted_name.empty())
new_contact.setFormattedName(formatted_name);
} else if (tag == "EMAIL") {
std::string address;
email mail;
std::getline(iline, address);
if (!address.empty()) {
mail.setAddress(address);
mail.setCategory(param);
new_contact.addEmail(mail);
}
} else if (tag == "TEL") {
std::string number;
phone num;
std::getline(iline, number);
if (!number.empty()) {
num.setNumber(number);
num.setCategory(param);
new_contact.addPhone(num);
}
} else if (tag == "NOTE") {
std::string note;
std::getline(iline, note);
new_contact.addNotes(note);
} else if (tag == "ADR") {
std::string adr;
address a;
std::getline(iline, adr);
if (!adr.empty()) {
a.setPostAddress(adr);
a.setCategory(param);
new_contact.addAddress(a);
}
} else if (tag == "PHOTO") {
//need to prepend the data that is in line
std::string data;
std::getline(iline, data);
// std::cout << "Parsing PHOTO: first chunk: " << data << std::endl;
//need to search for "=" as the end of data
std::string next_data;
while ( std::getline(vcardfile, next_data) && !next_data.empty() && next_data.at(0) != (char)13) {
data += next_data;
// std::cout << "Next line: " << next_data << std::endl;
// std::cout << "length: " << next_data.length() << std::endl;
// if (next_data.length() > 0)
// std::cout << (int)next_data.c_str()[0] << " ";
// if (next_data.length() > 1)
// std::cout << (int)next_data.c_str()[1];
// std::cout << std::endl;
// std::cout << "New data: " << data << std::endl;
}
// std::cout << "data: " << std::endl << data << std::endl;
// std::cout << "length of data: " << data.length() << std::endl;
} else if (tag == "VERSION") {
std::string ver;
std::getline(iline, ver);
if (ver != "2.1")
std::cerr << "Currently only version 2.1 is \"tested\", you might loose even more data since this is " << ver << std::endl;
} else if (tag == "BEGIN") {
//all fine, nothing to do
} else {
std::cout << "Unhandled tag:" << tag << " param: " << param << std::endl;
}
}
contacts.push_back(new_contact);
}
//else std::cout << line << std::endl;
}
return contacts;
}