From ce25d24e0f2cc4ad85f65bd91e7bf0299077fd26 Mon Sep 17 00:00:00 2001 From: cdfrey Date: Fri, 24 Aug 2007 22:06:19 +0000 Subject: [PATCH] - fixed Category handling in the Contact record class, to parse the comma separated string into an array, and back Note: this involves a change to the boost serialization and will affect backward compatibility with old serialization datafiles --- src/r_contact.cc | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/r_contact.h | 11 ++++++++- src/s11n-boost.h | 2 +- 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/src/r_contact.cc b/src/r_contact.cc index fa4f9561..c3dae53c 100644 --- a/src/r_contact.cc +++ b/src/r_contact.cc @@ -119,7 +119,6 @@ FieldLink ContactFieldLinks[] = { { CFC_PUBLIC_KEY, "PublicKey", 0,0, &Contact::PublicKey, 0, 0 }, { CFC_URL, "URL", 0,0, &Contact::URL, 0, 0 }, { CFC_PREFIX, "Prefix", 0,0, &Contact::Prefix, 0, 0 }, - { CFC_CATEGORY, "Category", 0,0, &Contact::Category, 0, 0 }, { CFC_HOME_ADDRESS1,"HomeAddress1", 0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::Address1, }, { CFC_HOME_ADDRESS2,"HomeAddress2", 0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::Address2, }, { CFC_HOME_ADDRESS3,"HomeAddress3", 0,0, 0, 0, 0, &Contact::HomeAddress, &PostalAddress::Address3, }, @@ -213,6 +212,12 @@ const unsigned char* Contact::ParseField(const unsigned char *begin, // ignore the group flag... the presense of group link items // behaves as the flag in this class return begin; + + case CFC_CATEGORY: { + std::string catstring = ParseFieldString(field); + CategoryStr2List(catstring, Categories); + } + return begin; } // if still not handled, add to the Unknowns list @@ -300,6 +305,12 @@ void Contact::BuildFields(Data &data, size_t &offset) const BuildField(data, offset, CFC_GROUP_LINK, link); } + if( Categories.size() ) { + string store; + CategoryList2Str(Categories, store); + BuildField(data, offset, CFC_CATEGORY, store); + } + // and finally save unknowns UnknownsType::const_iterator ub = Unknowns.begin(), ue = Unknowns.end(); @@ -344,6 +355,8 @@ void Contact::Clear() WorkAddress.Clear(); HomeAddress.Clear(); + Categories.clear(); + GroupLinks.clear(); Unknowns.clear(); @@ -398,6 +411,12 @@ void Contact::Dump(std::ostream &os) const } } + if( Categories.size() ) { + string display; + CategoryList2Str(Categories, display); + os << " Categories : " << display << "\n"; + } + // print any group links GroupLinksType::const_iterator gb = GroupLinks.begin(), ge = GroupLinks.end(); @@ -432,6 +451,55 @@ void Contact::SplitName(const std::string &full, std::string &first, std::string } } +void Contact::CategoryStr2List(const std::string &str, + Barry::CategoryList &list) +{ + // start fresh + list.clear(); + + if( !str.size() ) + return; + + // parse the comma-delimited string to a list, stripping away + // any white space around each category name + string::size_type start = 0, end = 0, delim = str.find(',', start); + while( start != string::npos ) { + if( delim == string::npos ) + end = str.size() - 1; + else + end = delim - 1; + + // strip surrounding whitespace + while( str[start] == ' ' ) + start++; + while( end && str[end] == ' ' ) + end--; + + if( start <= end ) { + string token = str.substr(start, end-start+1); + list.push_back(token); + } + + // next + start = delim; + if( start != string::npos ) + start++; + delim = str.find(',', start); + } +} + +void Contact::CategoryList2Str(const Barry::CategoryList &list, + std::string &str) +{ + str.clear(); + + Barry::CategoryList::const_iterator i = list.begin(); + for( ; i != list.end(); ++i ) { + if( str.size() ) + str += ", "; + str += *i; + } +} } // namespace Barry diff --git a/src/r_contact.h b/src/r_contact.h index 2ddc671d..9373f9fb 100644 --- a/src/r_contact.h +++ b/src/r_contact.h @@ -47,6 +47,7 @@ struct ContactGroupLink {} }; +typedef std::vector CategoryList; /// \addtogroup RecordParserClasses /// @{ @@ -54,6 +55,7 @@ struct ContactGroupLink class Contact { public: + typedef Barry::CategoryList CategoryList; typedef ContactGroupLink GroupLink; typedef std::vector GroupLinksType; typedef std::vector UnknownsType; @@ -82,7 +84,6 @@ public: PublicKey, URL, Prefix, - Category, Notes, UserDefined1, UserDefined2, @@ -93,6 +94,12 @@ public: PostalAddress WorkAddress; PostalAddress HomeAddress; + // Categories are not allowed to have commas in them. + // A category name containing a comma will be split into + // two categories, not only by this library, but by the + // device itself. + CategoryList Categories; + GroupLinksType GroupLinks; UnknownsType Unknowns; @@ -137,6 +144,8 @@ public: // helpers static void SplitName(const std::string &full, std::string &first, std::string &last); + static void CategoryStr2List(const std::string &str, Barry::CategoryList &list); + static void CategoryList2Str(const Barry::CategoryList &list, std::string &str); }; inline std::ostream& operator<< (std::ostream &os, const Contact &contact) { diff --git a/src/s11n-boost.h b/src/s11n-boost.h index 7d824c04..3b831f2f 100644 --- a/src/s11n-boost.h +++ b/src/s11n-boost.h @@ -91,7 +91,7 @@ void serialize(ArchiveT &ar, Barry::Contact &c, const unsigned int ver) ar & make_nvp("PublicKey", c.PublicKey); ar & make_nvp("URL", c.URL); ar & make_nvp("Prefix", c.Prefix); - ar & make_nvp("Category", c.Category); + ar & make_nvp("Categories", c.Categories); ar & make_nvp("HomeAddress1", c.HomeAddress.Address1); ar & make_nvp("HomeAddress2", c.HomeAddress.Address2); ar & make_nvp("HomeAddress3", c.HomeAddress.Address3); -- 2.11.4.GIT