/* * Copyright (C) 2003-2006 Funambol */ /** * This program reads one vcard from the file * given as first parameter and applies certain * conversions to it. * * The content of the file has to be ASCII or * UTF-8 encoded. */ #include #include #include "posixadapter.h" #include "VConverter.h" #include "base/util/utils.h" using namespace vocl; // very simply auto_ptr for arrays template class auto_array { T *m_array; public: auto_array(T *array = 0) : m_array(array) {} ~auto_array() { if (m_array) delete [] m_array; } void operator = (T *array) { if (m_array) delete [] m_array; m_array = array; } operator T * () { return m_array; } T *get() { return m_array; } T &operator [] (int index) { return m_array[index]; } }; int main( int argc, char **argv ) { char *sep = TEXT("--------------- %s -----------------------\n"); char *sep2 = TEXT("-----------------------------------------------------------\n"); if (argc != 2) { fprintf(stdout, "usage: %s \n", argv[0]); return 1; } // read as char * char *buffer; size_t len; if (!readFile(argv[1], &buffer, &len, true)) { fprintf(stdout, "%s: reading failed", argv[1]); } auto_array vcard(buffer); // convert to wchar_t auto_array wvcard(toWideChar(vcard)); fwprintf(stdout, sep, TEXT("original vcard")); fwprintf(stdout, TEXT("%s\n"), wvcard.get()); fwprintf(stdout, sep2); fwprintf(stdout, TEXT("\n")); // parse it std::auto_ptr vobj(VConverter::parse(wvcard)); if (vobj.get() == 0) { fprintf(stdout, "VConverter::parse()failed\n"); return 1; } vobj->toNativeEncoding(); VProperty *fileas = vobj->getProperty(TEXT("X-EVOLUTION-FILE-AS")); VProperty *n = vobj->getProperty(TEXT("FN")); fwprintf(stdout, TEXT("version: %s\nprodid: %s\nfull name: %s\nfile-as: %s\n\n"), vobj->getVersion(), vobj->getProdID(), n ? n->getValue() : TEXT(""), fileas ? fileas->getValue() : TEXT("")); // convert into the other version, then back again wchar_t *versions[2]; if (!wcscmp(vobj->getVersion(), TEXT("3.0"))) { versions[0] = TEXT("2.1"); versions[1] = TEXT("3.0"); } else { versions[0] = TEXT("3.0"); versions[1] = TEXT("2.1"); } for (int index = 0; index < 2; index++) { vobj->setVersion(versions[index]); VProperty *vprop = vobj->getProperty(TEXT("VERSION")); for (int property = vobj->propertiesCount() - 1; property >= 0; property--) { VProperty *vprop = vobj->getProperty(property); // replace 3.0 ENCODING=B with 2.1 ENCODING=BASE64 and vice versa char *encoding = vprop->getParameterValue("ENCODING"); if (encoding && (!wcsicmp(TEXT("B"), encoding) || !wcsicmp(TEXT("BASE64"), encoding))) { vprop->removeParameter("ENCODING"); vprop->addParameter("ENCODING", !wcscmp(versions[index], TEXT("2.1")) ? "BASE64" : "b"); } } vprop->setValue(versions[index]); vobj->fromNativeEncoding(); wvcard = vobj->toString(); vobj->toNativeEncoding(); fwprintf(stdout, sep, versions[index]); fwprintf(stdout, TEXT("%s\n"), wvcard.get()); fwprintf(stdout, sep2); fwprintf(stdout, TEXT("\n")); } #if 0 // convert into validated contact vCardConverter converter; converter.setSource(wvcard); Contact *contactPtr; long errorCode; if (!converter.convert(lastErrorMsg, &errorCode)) { fwprintf(stdout, TEXT("converter failed: %s (%ld)\n"), lastErrorMsg, errorCode); return 1; } converter.getContact(&contactPtr); std::auto_ptr contact(contactPtr); wvcard = contact->toString(); fwprintf(stdout, sep, TEXT("after parsing")); fwprintf(stdout, TEXT("%s\n"), wvcard.get()); fwprintf(stdout, sep2); fwprintf(stdout, TEXT("\n")); // let's see how the Contact class interprets the properties Name *name = contact->getName(); vCardProperty *displayname = name->getDisplayName(); fwprintf(stdout, TEXT("display name\nencoding: %s\ncharset: %s\nlanguage: %s\nvalue: %s\n\n"), displayname->getEncoding(), displayname->getCharset(), displayname->getLanguage(), displayname->getValue()); #endif return 0; }