1
// *************************************************************************
4
// * File: gsm_sorted_sms_store.h
6
// * Purpose: Sorted SMS store (residing in files or in the ME)
8
// * Author: Peter Hofmann (software@pxh.de)
10
// * Created: 14.8.1999
11
// *************************************************************************
13
#ifndef GSM_SORTED_SMS_STORE_H
14
#define GSM_SORTED_SMS_STORE_H
16
#include <gsmlib/gsm_sms_store.h>
17
#include <gsmlib/gsm_util.h>
18
#include <gsmlib/gsm_map_key.h>
27
// MapKey for SortedSMSStore
30
typedef MapKey<SortedSMSStore> SMSMapKey;
32
// maps key (see SortedSMSStore::SortOrder) to entry
34
typedef multimap<SMSMapKey, SMSStoreEntry*> SMSStoreMap;
36
// iterator for SortedSMSStore that hides the "second" member of the map
38
typedef SMSStoreMap::iterator SMSStoreMapIterator;
39
class SortedSMSStoreIterator : public SMSStoreMapIterator
42
SortedSMSStoreIterator() {}
43
SortedSMSStoreIterator(SMSStoreMap::iterator i) :
44
SMSStoreMapIterator(i) {}
46
SMSStoreEntry &operator*()
47
{return *((SMSStoreMap::iterator)*this)->second;}
49
SMSStoreEntry *operator->()
50
{return ((SMSStoreMap::iterator)*this)->second;}
53
// The class SortedSMSStore makes the SMS store more manageable:
54
// - empty slots in the ME phonebook are hidden by the API
55
// - the class transparently handles stores that reside in files
57
class SortedSMSStore : public RefBase, public NoCopy
61
bool _changed; // true if file has changed after last save
62
bool _fromFile; // true if store read from file
63
bool _madeBackupFile; // true if backup file was created
64
SortOrder _sortOrder; // sort order of the _sortedSMSStore
65
// (default is ByDate)
66
bool _readonly; // =true if read from stdin
67
string _filename; // name of the file if store from file
68
SMSStoreMap _sortedSMSStore; // store from file
69
SMSStoreRef _meSMSStore; // store if from ME
71
unsigned int _nextIndex; // next index to use for file-based store
73
// initial read of SMS file
74
void readSMSFile(istream &pbs, string filename) throw(GsmException);
76
// synchronize SortedSMSStore with file (no action if in ME)
77
void sync(bool fromDestructor) throw(GsmException);
79
// throw an exception if _readonly is set
80
void checkReadonly() throw(GsmException);
84
typedef SortedSMSStoreIterator iterator;
85
typedef SMSStoreMap::size_type size_type;
87
// constructor for file-based store
89
SortedSMSStore(string filename) throw(GsmException);
90
// read from stdin or start empty and write to stdout
91
SortedSMSStore(bool fromStdin) throw(GsmException);
93
// constructor for ME-based store
94
SortedSMSStore(SMSStoreRef meSMSStore) throw(GsmException);
97
void setSortOrder(SortOrder newOrder);
98
SortOrder sortOrder() const {return _sortOrder;}
100
// store traversal commands
101
// these are suitable to use stdc++ lib algorithms and iterators
103
// traversal commands
104
iterator begin() {return _sortedSMSStore.begin();}
105
iterator end() {return _sortedSMSStore.end();}
107
// the size macros return the number of used entries
108
int size() const {return _sortedSMSStore.size();}
109
int max_size() const;
110
int capacity() const;
111
bool empty() const throw(GsmException) {return size() == 0;}
113
// existing iterators may be invalidated after an insert operation
115
// insert only writes to available positions
116
// warning: insert fails silently if size() == max_size()
117
iterator insert(const SMSStoreEntry& x) throw(GsmException);
118
iterator insert(iterator position, const SMSStoreEntry& x)
121
SMSStoreMap::size_type count(Address &key)
123
assert(_sortOrder == ByAddress);
124
return _sortedSMSStore.count(SMSMapKey(*this, key));
126
iterator find(Address &key)
128
assert(_sortOrder == ByAddress);
129
return _sortedSMSStore.find(SMSMapKey(*this, key));
131
iterator lower_bound(Address &key)
133
assert(_sortOrder == ByAddress);
134
return _sortedSMSStore.lower_bound(SMSMapKey(*this, key));
136
iterator upper_bound(Address &key)
138
assert(_sortOrder == ByAddress);
139
return _sortedSMSStore.upper_bound(SMSMapKey(*this, key));
141
pair<iterator, iterator> equal_range(Address &key)
143
assert(_sortOrder == ByAddress);
144
return _sortedSMSStore.equal_range(SMSMapKey(*this, key));
147
SMSStoreMap::size_type count(Timestamp &key)
149
assert(_sortOrder == ByDate);
150
return _sortedSMSStore.count(SMSMapKey(*this, key));
152
iterator find(Timestamp &key)
154
assert(_sortOrder == ByDate);
155
return _sortedSMSStore.find(SMSMapKey(*this, key));
157
iterator lower_bound(Timestamp &key)
159
assert(_sortOrder == ByDate);
160
return _sortedSMSStore.lower_bound(SMSMapKey(*this, key));
162
iterator upper_bound(Timestamp &key)
164
assert(_sortOrder == ByDate);
165
return _sortedSMSStore.upper_bound(SMSMapKey(*this, key));
167
pair<iterator, iterator> equal_range(Timestamp &key)
169
assert(_sortOrder == ByDate);
170
return _sortedSMSStore.equal_range(SMSMapKey(*this, key));
173
SMSStoreMap::size_type count(int key)
175
assert(_sortOrder == ByIndex || _sortOrder == ByType);
176
return _sortedSMSStore.count(SMSMapKey(*this, key));
178
iterator find(int key)
180
assert(_sortOrder == ByIndex || _sortOrder == ByType);
181
return _sortedSMSStore.find(SMSMapKey(*this, key));
183
iterator lower_bound(int key)
185
assert(_sortOrder == ByIndex || _sortOrder == ByType);
186
return _sortedSMSStore.lower_bound(SMSMapKey(*this, key));
188
iterator upper_bound(int key)
190
assert(_sortOrder == ByIndex || _sortOrder == ByType);
191
return _sortedSMSStore.upper_bound(SMSMapKey(*this, key));
193
pair<iterator, iterator> equal_range(int key)
195
assert(_sortOrder == ByIndex || _sortOrder == ByType);
196
return _sortedSMSStore.equal_range(SMSMapKey(*this, key));
199
size_type erase(Address &key) throw(GsmException);
200
size_type erase(int key) throw(GsmException);
201
size_type erase(Timestamp &key) throw(GsmException);
202
void erase(iterator position) throw(GsmException);
203
void erase(iterator first, iterator last) throw(GsmException);
204
void clear() throw(GsmException);
206
// synchronize SortedPhonebook with file (no action if in ME)
207
void sync() throw(GsmException) {sync(false);}
210
// writes back change to file if store is in file
214
typedef Ref<SortedSMSStore> SortedSMSStoreRef;
217
#endif // GSM_SORTED_SMS_STORE_H