3 // Class that maps opensync UID strings to Blackberry
4 // Record ID's and back.
8 Copyright (C) 2007, 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.
37 bool idmap::Load(const char *filename
)
42 std::ifstream
ifs(filename
);
50 ifs
>> recordId
>> std::ws
;
51 std::getline(ifs
, line
);
52 if( ifs
&& recordId
&& line
.size() ) {
59 bool idmap::Save(const char *filename
) const
61 std::ofstream
ofs(filename
);
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
);
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
) {
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
)
105 const idmap::rid_type
& idmap::GetRid(const uid_type
&uid
) const
107 const_iterator i
= m_map
.find(uid
);
111 const idmap::uid_type
& idmap::operator[] (const rid_type
&rid
) const
116 const idmap::rid_type
& idmap::operator[] (const uid_type
&uid
) const
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 )
134 // neither id must already exist
135 if( UidExists(uid
) || RidExists(rid
) )
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
)
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
) {