vCard (`.vcf`) 本質是結構化文本,格式規範見 RFC 6350。C++ 無成熟專用庫,但自己寫解析器極簡單。
BEGIN:VCARD
VERSION:3.0
N:Zhang;San;;;
FN:Zhang San
TEL;TYPE=CELL:+86 138 0000 1111
EMAIL:zhangsan@example.com
END:VCARD
std::string readFile(const std::string& path) { /* 讀全文 */ }
std::vector parseVCard(const std::string& content) {
std::vector contacts;
std::regex cardRegex("BEGIN:VCARD([\\s\\S]*?)END:VCARD");
std::regex propRegex("([A-Z]+)(?:;([^:]*))?:(.*)");
for (auto it = std::sregex_iterator(content.begin(), content.end(), cardRegex);
it != std::sregex_iterator(); ++it) {
Contact c;
std::string card = (*it)[1];
for (auto pit = std::sregex_iterator(card.begin(), card.end(), propRegex);
pit != std::sregex_iterator(); ++pit) {
std::string key = (*pit)[1];
std::string params = (*pit)[2];
std::string value = (*pit)[3];
// 解析 key/params/value 填充 Contact 結構體
}
contacts.push_back(c);
}
return contacts;
}
enum State { START, IN_CARD, IN_PROP };
State state = START;
Contact current;
for (char ch : content) {
switch (state) {
case START:
if (ch == 'B') { /* 檢測 BEGIN:VCARD */ state = IN_CARD; }
break;
case IN_CARD:
if (ch == 'E') { /* 檢測 END:VCARD */ contacts.push_back(current); current = {}; state = START; }
else if (ch != '\r' && ch != '\n') { /* 解析屬性行 */ }
break;
}
}
**結論**:vCard 解析屬於「造輪子更快」的場景,正則幾十行搞定,無需引入重型依賴。