Bumped copyright dates for 2013
[barry.git] / opensync-plugin / src / idmap.cc
blob19c93d6b6d6de70c6501b29c3868fcd119832cb7
1 //
2 // \file idmap.cc
3 // Class that maps opensync UID strings to Blackberry
4 // Record ID's and back.
5 //
7 /*
8 Copyright (C) 2007-2013, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include "idmap.h"
24 #include <fstream>
25 #include "trace.h"
27 idmap::idmap()
28 : m_blank_uid(""),
29 m_blank_rid(0)
33 idmap::~idmap()
37 bool idmap::Load(const char *filename)
39 // start fresh
40 m_map.clear();
42 std::ifstream ifs(filename);
43 if( !ifs )
44 return false;
46 std::string line;
47 uint32_t recordId;
48 while( ifs ) {
49 recordId = 0;
50 ifs >> recordId >> std::ws;
51 std::getline(ifs, line);
52 if( ifs && recordId && line.size() ) {
53 Map(line, recordId);
56 return ifs.eof();
59 bool idmap::Save(const char *filename) const
61 std::ofstream ofs(filename);
62 if( !ofs )
63 return false;
65 const_iterator i = m_map.begin();
66 for( ; i != m_map.end(); ++i ) {
67 ofs << i->second << " " << i->first << std::endl;
69 return !ofs.bad() && !ofs.fail();
72 bool idmap::UidExists(const uid_type &uid, const_iterator *it) const
74 const_iterator i = m_map.find(uid);
75 if( it )
76 *it = i;
77 return i != m_map.end();
80 bool idmap::RidExists(const rid_type &rid, const_iterator *it) const
82 const_iterator i = m_map.begin();
83 for( ; i != m_map.end(); ++i ) {
84 if( i->second == rid ) {
85 if( it )
86 *it = i;
87 return true;
90 if( it )
91 *it = m_map.end();
92 return false;
95 const idmap::uid_type& idmap::GetUid(const rid_type &rid) const
97 const_iterator i = m_map.begin();
98 for( ; i != m_map.end(); ++i ) {
99 if( i->second == rid )
100 return i->first;
102 return m_blank_uid;
105 const idmap::rid_type& idmap::GetRid(const uid_type &uid) const
107 const_iterator i = m_map.find(uid);
108 return i->second;
111 const idmap::uid_type& idmap::operator[] (const rid_type &rid) const
113 return GetUid(rid);
116 const idmap::rid_type& idmap::operator[] (const uid_type &uid) const
118 return GetRid(uid);
121 // returns map::end() if either id already exists, otherwise
122 // returns newly mapped item iterator.
124 // The other versions of the function are to make conversion
125 // between different types easier, giving ability to map
126 // with any reasonable type, and then access the real
127 // values through the iterator if needed.
128 idmap::const_iterator idmap::Map(const uid_type &uid, const rid_type &rid)
130 // neither id can be blank
131 if( uid.size() == 0 || rid == 0 )
132 return m_map.end();
134 // neither id must already exist
135 if( UidExists(uid) || RidExists(rid) )
136 return m_map.end();
138 return m_map.insert(m_map.begin(), make_pair(uid, rid));
142 idmap::const_iterator idmap::Map(unsigned long uid_number, const rid_type &rid)
146 idmap::const_iterator idmap::Map(unsigned long uid_number, const std::string &rid_string)
151 void idmap::UnmapUid(const uid_type &uid)
153 m_map.erase(uid);
156 void idmap::UnmapRid(const rid_type &rid)
158 iterator i = m_map.begin();
159 for( ; i != m_map.end(); ++i ) {
160 if( i->second == rid ) {
161 m_map.erase(i);
162 return;