~ubuntu-branches/ubuntu/lucid/libwpd/lucid

« back to all changes in this revision

Viewing changes to src/lib/WPXPropertyList.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Rene Engelhard
  • Date: 2007-01-11 15:15:57 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20070111151557-rn1kysabqbccx3as
Tags: 0.8.8-2
run make check for stream check; build-depend on libcppunit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* libwpd
2
2
 * Copyright (C) 2004 William Lachance (wrlach@gmail.com)
3
3
 * Copyright (C) 2005 Net Integration Technologies (http://www.net-itech.com)
 
4
 * Copyright (C) 2006 Fridrich Strba (fridrich.strba@bluewin.ch)
4
5
 *
5
6
 * This library is free software; you can redistribute it and/or
6
7
 * modify it under the terms of the GNU Library General Public
27
28
#include <map>
28
29
#include <string>
29
30
 
30
 
class WPXStdMapImpl : public WPXMapImpl
 
31
class WPXMapImpl
31
32
{
32
33
public:
33
 
        WPXStdMapImpl() {}
34
 
        virtual ~WPXStdMapImpl();
35
 
        virtual void insert(const char *name, WPXProperty *property);
36
 
        virtual const WPXProperty * operator[](const char *name) const;
37
 
        virtual void remove(const char *name);
38
 
        virtual void clear();
 
34
        WPXMapImpl() {}
 
35
        ~WPXMapImpl();
 
36
        void insert(const char *name, WPXProperty *property);
 
37
        const WPXProperty * operator[](const char *name) const;
 
38
        void remove(const char *name);
 
39
        void clear();
39
40
 
40
41
private:
41
42
        mutable std::map<std::string, WPXProperty *> m_map;
42
43
 
43
 
        friend class WPXStdMapIterImpl;
 
44
        friend class WPXMapIterImpl;
44
45
};
45
46
 
46
 
WPXStdMapImpl::~WPXStdMapImpl()
 
47
WPXMapImpl::~WPXMapImpl()
47
48
{
48
49
        for (std::map<std::string, WPXProperty *>::iterator iter = m_map.begin();
49
50
             iter != m_map.end();
50
51
             iter++) { delete iter->second; } 
51
52
}
52
53
 
53
 
const WPXProperty * WPXStdMapImpl::operator[](const char *name) const
 
54
const WPXProperty * WPXMapImpl::operator[](const char *name) const
54
55
{
55
 
        std::map<std::string, WPXProperty *>::iterator i;
56
 
        const std::string s(name);
57
 
        i = m_map.find(s);
 
56
        std::map<std::string, WPXProperty *>::iterator i = m_map.find(name);
58
57
        if (i != m_map.end()) {
59
58
                return i->second;
60
59
        }
61
60
 
62
 
        return NULL;
63
 
}
64
 
 
65
 
void WPXStdMapImpl::insert(const char *name, WPXProperty *prop)
66
 
{
67
 
        m_map[name] = prop;
68
 
}
69
 
 
70
 
void WPXStdMapImpl::remove(const char *name)
71
 
{
72
 
        // FIXME: delete element?
73
 
        m_map.erase(name);
74
 
}
75
 
 
76
 
void WPXStdMapImpl::clear()
 
61
        return 0;
 
62
}
 
63
 
 
64
void WPXMapImpl::insert(const char *name, WPXProperty *prop)
 
65
{
 
66
        std::map<std::string, WPXProperty *>::iterator i = m_map.lower_bound(name);
 
67
        if (i != m_map.end() && !(m_map.key_comp()(name, i->first))) {
 
68
                WPXProperty *tmpProp = i->second;
 
69
                i->second = prop;
 
70
                delete tmpProp;
 
71
                return;
 
72
        }
 
73
        m_map.insert(i, std::map<std::string, WPXProperty *>::value_type(name, prop));
 
74
}
 
75
 
 
76
void WPXMapImpl::remove(const char *name)
 
77
{
 
78
        std::map<std::string, WPXProperty *>::iterator i = m_map.find(name);
 
79
        if (i != m_map.end()) {
 
80
                if (i->second) delete (i->second);
 
81
                m_map.erase(i);
 
82
        }
 
83
}
 
84
 
 
85
void WPXMapImpl::clear()
77
86
{
78
87
        for (std::map<std::string, WPXProperty *>::iterator iter = m_map.begin();
79
88
             iter != m_map.end();
83
92
}
84
93
 
85
94
WPXPropertyList::WPXPropertyList() :
86
 
        m_mapImpl(new WPXStdMapImpl())
 
95
        m_mapImpl(new WPXMapImpl())
87
96
{
88
97
}
89
98
 
90
99
WPXPropertyList::WPXPropertyList(const WPXPropertyList &propList) :
91
 
        m_mapImpl(new WPXStdMapImpl())
 
100
        m_mapImpl(new WPXMapImpl())
92
101
{
93
102
        WPXPropertyList::Iter i(propList);
94
103
        for (i.rewind(); i.next(); )
154
163
        m_mapImpl->clear();
155
164
}
156
165
 
157
 
class WPXStdMapIterImpl : public WPXMapIterImpl
 
166
class WPXMapIterImpl
158
167
{
159
168
public:
160
 
        WPXStdMapIterImpl(const WPXStdMapImpl *impl);
161
 
        virtual void rewind();
162
 
        virtual bool next();
163
 
        virtual bool last();
164
 
        virtual const WPXProperty * operator()() const;
165
 
        virtual const char * key();
 
169
        WPXMapIterImpl(const WPXMapImpl *impl);
 
170
        void rewind();
 
171
        bool next();
 
172
        bool last();
 
173
        const WPXProperty * operator()() const;
 
174
        const char * key();
166
175
 
167
176
private:
168
177
        bool m_imaginaryFirst;
171
180
};
172
181
 
173
182
 
174
 
WPXStdMapIterImpl::WPXStdMapIterImpl(const WPXStdMapImpl *impl)
 
183
WPXMapIterImpl::WPXMapIterImpl(const WPXMapImpl *impl)
175
184
{
176
185
        m_map = &(impl->m_map);
177
186
        m_iter = impl->m_map.begin();
178
187
        m_imaginaryFirst = false;
179
188
}
180
189
 
181
 
void WPXStdMapIterImpl::rewind()
 
190
void WPXMapIterImpl::rewind()
182
191
{
183
192
    // rewind to an imaginary element that preceeds the first one
184
193
    m_imaginaryFirst = true;
185
194
    m_iter = m_map->begin(); 
186
195
}
187
196
 
188
 
bool WPXStdMapIterImpl::next()
 
197
bool WPXMapIterImpl::next()
189
198
{
190
199
    if (!m_imaginaryFirst) 
191
200
        m_iter++; 
196
205
    return true; 
197
206
}
198
207
 
199
 
bool WPXStdMapIterImpl::last()
 
208
bool WPXMapIterImpl::last()
200
209
{
201
210
        if (m_iter == m_map->end()) 
202
211
                return true;
204
213
        return false;
205
214
}
206
215
 
207
 
const WPXProperty * WPXStdMapIterImpl::operator()() const
 
216
const WPXProperty * WPXMapIterImpl::operator()() const
208
217
{
209
218
        return m_iter->second;
210
219
}
211
220
 
212
 
const char * WPXStdMapIterImpl::key()
 
221
const char * WPXMapIterImpl::key()
213
222
{
214
223
        return m_iter->first.c_str();
215
224
}
217
226
WPXPropertyList::Iter::Iter(const WPXPropertyList &propList) 
218
227
{
219
228
        WPXMapImpl *impl = propList.m_mapImpl;
220
 
        m_iterImpl = new WPXStdMapIterImpl(static_cast<WPXStdMapImpl *>(impl));
 
229
        m_iterImpl = new WPXMapIterImpl(static_cast<WPXMapImpl *>(impl));
221
230
}
222
231
 
223
232
WPXPropertyList::Iter::~Iter()