~ubuntu-branches/debian/squeeze/gsmlib/squeeze

« back to all changes in this revision

Viewing changes to gsmlib/gsm_sorted_sms_store.h

  • Committer: Bazaar Package Importer
  • Author(s): Mikael Hedin
  • Date: 2002-01-24 12:59:07 UTC
  • Revision ID: james.westby@ubuntu.com-20020124125907-b7qkpokx5283jdpu
Tags: upstream-1.8
ImportĀ upstreamĀ versionĀ 1.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// *************************************************************************
 
2
// * GSM TA/ME library
 
3
// *
 
4
// * File:    gsm_sorted_sms_store.h
 
5
// *
 
6
// * Purpose: Sorted SMS store (residing in files or in the ME)
 
7
// *
 
8
// * Author:  Peter Hofmann (software@pxh.de)
 
9
// *
 
10
// * Created: 14.8.1999
 
11
// *************************************************************************
 
12
 
 
13
#ifndef GSM_SORTED_SMS_STORE_H
 
14
#define GSM_SORTED_SMS_STORE_H
 
15
 
 
16
#include <gsmlib/gsm_sms_store.h>
 
17
#include <gsmlib/gsm_util.h>
 
18
#include <gsmlib/gsm_map_key.h>
 
19
#include <string>
 
20
#include <map>
 
21
#include <assert.h>
 
22
 
 
23
using namespace std;
 
24
 
 
25
namespace gsmlib
 
26
{
 
27
  // MapKey for SortedSMSStore
 
28
  
 
29
  class SortedSMSStore;
 
30
  typedef MapKey<SortedSMSStore> SMSMapKey;
 
31
 
 
32
  // maps key (see SortedSMSStore::SortOrder) to entry
 
33
  
 
34
  typedef multimap<SMSMapKey, SMSStoreEntry*> SMSStoreMap;
 
35
 
 
36
  // iterator for SortedSMSStore that hides the "second" member of the map
 
37
  
 
38
  typedef SMSStoreMap::iterator SMSStoreMapIterator;
 
39
  class SortedSMSStoreIterator : public SMSStoreMapIterator
 
40
  {
 
41
  public:
 
42
    SortedSMSStoreIterator() {}
 
43
    SortedSMSStoreIterator(SMSStoreMap::iterator i) :
 
44
      SMSStoreMapIterator(i) {}
 
45
 
 
46
    SMSStoreEntry &operator*()
 
47
      {return *((SMSStoreMap::iterator)*this)->second;}
 
48
 
 
49
    SMSStoreEntry *operator->()
 
50
      {return ((SMSStoreMap::iterator)*this)->second;}
 
51
  };
 
52
 
 
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
 
56
 
 
57
  class SortedSMSStore : public RefBase, public NoCopy
 
58
  {
 
59
  private:
 
60
 
 
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
 
70
 
 
71
    unsigned int _nextIndex;    // next index to use for file-based store
 
72
 
 
73
    // initial read of SMS file
 
74
    void readSMSFile(istream &pbs, string filename) throw(GsmException);
 
75
    
 
76
    // synchronize SortedSMSStore with file (no action if in ME)
 
77
    void sync(bool fromDestructor) throw(GsmException);
 
78
    
 
79
    // throw an exception if _readonly is set
 
80
    void checkReadonly() throw(GsmException);
 
81
 
 
82
  public:
 
83
    // iterator defs
 
84
    typedef SortedSMSStoreIterator iterator;
 
85
    typedef SMSStoreMap::size_type size_type;
 
86
 
 
87
    // constructor for file-based store
 
88
    // read from file
 
89
    SortedSMSStore(string filename) throw(GsmException);
 
90
    // read from stdin or start empty and write to stdout
 
91
    SortedSMSStore(bool fromStdin) throw(GsmException);
 
92
 
 
93
    // constructor for ME-based store
 
94
    SortedSMSStore(SMSStoreRef meSMSStore) throw(GsmException);
 
95
 
 
96
    // handle sorting
 
97
    void setSortOrder(SortOrder newOrder);
 
98
    SortOrder sortOrder() const {return _sortOrder;}
 
99
    
 
100
    // store traversal commands
 
101
    // these are suitable to use stdc++ lib algorithms and iterators
 
102
    
 
103
    // traversal commands
 
104
    iterator begin() {return _sortedSMSStore.begin();}
 
105
    iterator end() {return _sortedSMSStore.end();}
 
106
 
 
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;}
 
112
 
 
113
    // existing iterators may be invalidated after an insert operation
 
114
    // return position
 
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)
 
119
      throw(GsmException);
 
120
 
 
121
    SMSStoreMap::size_type count(Address &key)
 
122
      {
 
123
        assert(_sortOrder == ByAddress);
 
124
        return _sortedSMSStore.count(SMSMapKey(*this, key));
 
125
      }
 
126
    iterator find(Address &key)
 
127
      {
 
128
        assert(_sortOrder == ByAddress);
 
129
        return _sortedSMSStore.find(SMSMapKey(*this, key));
 
130
      }
 
131
    iterator lower_bound(Address &key)
 
132
      {
 
133
        assert(_sortOrder == ByAddress);
 
134
        return _sortedSMSStore.lower_bound(SMSMapKey(*this, key));
 
135
      }
 
136
    iterator upper_bound(Address &key)
 
137
      {
 
138
        assert(_sortOrder == ByAddress);
 
139
        return _sortedSMSStore.upper_bound(SMSMapKey(*this, key));
 
140
      }
 
141
    pair<iterator, iterator> equal_range(Address &key)
 
142
      {
 
143
        assert(_sortOrder == ByAddress);
 
144
        return _sortedSMSStore.equal_range(SMSMapKey(*this, key));
 
145
      }
 
146
 
 
147
    SMSStoreMap::size_type count(Timestamp &key)
 
148
      {
 
149
        assert(_sortOrder == ByDate);
 
150
        return _sortedSMSStore.count(SMSMapKey(*this, key));
 
151
      }
 
152
    iterator find(Timestamp &key)
 
153
      {
 
154
        assert(_sortOrder == ByDate);
 
155
        return _sortedSMSStore.find(SMSMapKey(*this, key));
 
156
      }
 
157
    iterator lower_bound(Timestamp &key)
 
158
      {
 
159
        assert(_sortOrder == ByDate);
 
160
        return _sortedSMSStore.lower_bound(SMSMapKey(*this, key));
 
161
      }
 
162
    iterator upper_bound(Timestamp &key)
 
163
      {
 
164
        assert(_sortOrder == ByDate);
 
165
        return _sortedSMSStore.upper_bound(SMSMapKey(*this, key));
 
166
      }
 
167
    pair<iterator, iterator> equal_range(Timestamp &key)
 
168
      {
 
169
        assert(_sortOrder == ByDate);
 
170
        return _sortedSMSStore.equal_range(SMSMapKey(*this, key));
 
171
      }
 
172
 
 
173
    SMSStoreMap::size_type count(int key)
 
174
      {
 
175
        assert(_sortOrder == ByIndex || _sortOrder == ByType);
 
176
        return _sortedSMSStore.count(SMSMapKey(*this, key));
 
177
      }
 
178
    iterator find(int key)
 
179
      {
 
180
        assert(_sortOrder == ByIndex || _sortOrder == ByType);
 
181
        return _sortedSMSStore.find(SMSMapKey(*this, key));
 
182
      }
 
183
    iterator lower_bound(int key)
 
184
      {
 
185
        assert(_sortOrder == ByIndex || _sortOrder == ByType);
 
186
        return _sortedSMSStore.lower_bound(SMSMapKey(*this, key));
 
187
      }
 
188
    iterator upper_bound(int key)
 
189
      {
 
190
        assert(_sortOrder == ByIndex || _sortOrder == ByType);
 
191
        return _sortedSMSStore.upper_bound(SMSMapKey(*this, key));
 
192
      }
 
193
    pair<iterator, iterator> equal_range(int key)
 
194
      {
 
195
        assert(_sortOrder == ByIndex || _sortOrder == ByType);
 
196
        return _sortedSMSStore.equal_range(SMSMapKey(*this, key));
 
197
      }
 
198
 
 
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);
 
205
 
 
206
    // synchronize SortedPhonebook with file (no action if in ME)
 
207
    void sync() throw(GsmException) {sync(false);}
 
208
    
 
209
    // destructor
 
210
    // writes back change to file if store is in file
 
211
    ~SortedSMSStore();
 
212
  };
 
213
 
 
214
  typedef Ref<SortedSMSStore> SortedSMSStoreRef;
 
215
};
 
216
 
 
217
#endif // GSM_SORTED_SMS_STORE_H