Instantly share code, notes, and snippets.

Embed
What would you like to do?
VCard phone number normalizer
/*! @brief Simple utility for normalizing phone numbers in VCard (.vcf) files
*
* @author fluffy <fluffy@beesbuzz.biz>
*
* Requires the Google libphonenumber (C++ version), available at:
*
* https://code.google.com/p/libphonenumber/
*
* Just build this with -lphonenumber (or whatever equivalent is
* necessary on your toolchain of choice), and pipe the VCard on
* standard input and it comes out on standard output. You can
* easily filter all the files in a directory with:
*
* for i in *.vcf ; do
* echo $i ; fix-vcard < $i > $i.new && mv $i.new $i
* done
*
* Any warnings will be printed to stderr.
*
* If you're using a CardDAV server, you can just access it with your
* standard WebDAV mounting tools, e.g.:
*
* mkdir carddav
* mount.davfs https://cal.example.com/caldav.php/username carddav
* cd carddav/contacts
*
* and then you can also use your text editor of choice to fix
* whatever flaws you find.
*
* If you are in a locale other than US, pass it in as the command
* line arg.
*
* Copyright © 2012 fluffy <fluffy@beesbuzz.biz>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of the owner nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*/
#include <iostream>
#include <string>
#include <phonenumbers/phonenumberutil.h>
using namespace i18n::phonenumbers;
int main(int argc, char *argv[])
{
const char *locale = "US";
if (argc > 1) {
locale = argv[1];
}
PhoneNumberUtil *util = PhoneNumberUtil::GetInstance();
do {
std::string line;
std::getline(std::cin, line);
if (!std::cin.fail() && line.size()) {
//! Strip off all line endings
line = line.substr(0, line.find_last_not_of("\r\n") + 1);
if (line.find("TEL") == 0) {
std::string::size_type split = line.find(':');
if (split == std::string::npos) {
std::cerr << "Invalid TEL line: " << line << std::endl;
} else {
std::string pfx = line.substr(0, split + 1);
std::string num = line.substr(split + 1);
PhoneNumber number;
util->Parse(num, locale, &number);
if (util->IsValidNumber(number)) {
util->Format(number,
PhoneNumberUtil::INTERNATIONAL,
&num);
} else {
std::cerr << "Could not parse phone number: "
<< num << std::endl;
}
std::cout << pfx << num << "\r\n";
}
} else {
std::cout << line << "\r\n";
}
}
} while (!std::cin.fail());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment