1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
////////////////////////////////////////////////////////////////////////////////
// This source file is part of the ZipArchive library source distribution and
// is Copyrighted 2000 - 2007 by Artpol Software - Tadeusz Dracz
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// For the licensing details refer to the License.txt file.
//
// Web Site: http://www.artpol-software.com
////////////////////////////////////////////////////////////////////////////////
#ifndef ZIPARCHIVE_ZIPCOLLECTIONS_DOT_H
#error Do not include this file directly. Include ZipCollections.h instead
#endif
#if _MSC_VER > 1000
#pragma warning( push, 3 ) // STL
#pragma warning (disable : 4284) //return type for 'identifier::operator >' is not a UDT or reference to a UDT. Will produce errors if applied using infix notation
#pragma warning (disable : 4018) //'expression' : signed/unsigned mismatch
#endif
#include <vector>
#include <list>
#include <map>
#include <algorithm>
#include <functional>
#include "ZipString.h"
#include "ZipException.h"
#define ZIP_ARRAY_SIZE_TYPE size_t
template<class TYPE>
class CZipArray : private std::vector<TYPE>
{
public:
typedef int (*CompareFunction)(const void* pArg1, const void* pArg2);
private:
struct Sorter
{
CompareFunction m_pFunction;
Sorter(CompareFunction pFunction)
{
m_pFunction = pFunction;
}
bool operator ()(TYPE const& t1, TYPE const& t2)
{
return (*m_pFunction)(&t1, &t2) < 0;
}
};
public:
typedef typename std::vector<TYPE>::iterator iterator;
typedef typename std::vector<TYPE> inherited;
protected:
iterator GetIterFromIndex(size_t uIndex)
{
iterator iter = this->begin();
iter += uIndex;
// int t = 0; while (t != uIndex) {iter++;t++;}
return iter;
}
public:
void Sort(bool bAscending)
{
if (bAscending)
std::sort (this->begin(), this->end(), std::less<TYPE>());
else
std::sort (this->begin(), this->end(), std::greater<TYPE>());
}
void Sort(CompareFunction pFunction)
{
std::sort(this->begin(), this->end(), Sorter(pFunction));
}
size_t GetSize() const{return this->size(); }
size_t GetCount() const{return this->size(); }
size_t GetUpperBound() const
{
size_t ret = this->size();
if (ret == 0)
CZipException::Throw(CZipException::outOfBounds);
return ret - 1;
}
TYPE& GetAt(size_t uIndex) {return this->at(uIndex);}
const TYPE& GetAt(size_t uIndex) const {return this->at(uIndex);}
size_t Add(const TYPE& x) {this->push_back(x);return GetUpperBound();}
void RemoveAll() {this->clear();}
void RemoveAt(size_t uIndex) { this->erase(GetIterFromIndex(uIndex));}
void InsertAt(size_t uIndex, const TYPE& x){this->insert(GetIterFromIndex(uIndex), x);}
TYPE& operator[](size_t uIndex)
{
return inherited::operator[](uIndex);
}
TYPE operator[](size_t uIndex) const
{
return inherited::operator[](uIndex);
}
};
typedef CZipArray<CZipString> CZipStringArray;
typedef CZipArray<WORD> CZipWordArray;
template<class TYPE>
class CZipPtrList : private std::list<TYPE>
{
public:
typedef typename std::list<TYPE>::iterator iterator;
typedef typename std::list<TYPE>::const_iterator const_iterator;
size_t GetCount() const {return this->size();}
void AddTail(const TYPE& x){this->push_back(x);}
void AddHead(const TYPE& x){this->push_front(x);}
void RemoveHead() {this->pop_front();}
void RemoveTail() {this->pop_back();}
void RemoveAll() {this->clear();}
TYPE& GetHead() {return this->front();}
TYPE GetHead() const {return this->front();}
TYPE& GetTail() {return this->back();}
TYPE GetTail() const {return this->back();}
iterator GetHeadPosition() { return this->begin();}
const_iterator GetHeadPosition() const { return this->begin();}
iterator GetTailPosition() { return this->back();}
TYPE& GetNext(iterator& pos) { return *pos++;}
const TYPE GetNext(const_iterator& pos) const{ return *pos++;}
TYPE& GetPrev(iterator& pos) { return *pos--;}
TYPE GetPrev(iterator& pos) const{ return *pos--;}
iterator Find(TYPE& x) { return std::find(this->begin(), this->end(), x);}
void RemoveAt(iterator& pos) { this->erase(pos);}
bool IteratorValid(const_iterator &iter) const
{
return iter != this->end();
}
bool IteratorValid(iterator &iter)
{
return iter != this->end();
}
iterator FindIndex(size_t uIndex)
{
iterator iter = this->begin();
size_t t = 0; while (t != uIndex) {iter++;t++;}
return iter;
}
const_iterator FindIndex(size_t uIndex) const
{
const_iterator iter = this->begin();
size_t t = 0; while (t != uIndex) {iter++;t++;}
return iter;
}
TYPE& GetAt(const iterator& pos) { return *pos;}
TYPE GetAt(const_iterator& pos) const{ return *pos;}
};
// simplified and partial only
template<class KEY, class VALUE>
class CZipMap : private std::map<KEY, VALUE>
{
public:
typedef typename std::map<KEY, VALUE>::iterator iterator;
typedef typename std::map<KEY, VALUE>::const_iterator const_iterator;
typedef typename std::map<KEY,VALUE, std::less<KEY>, std::allocator<std::pair<const KEY, VALUE> > >::value_type v_type;
void SetAt( KEY key, VALUE newValue)
{
this->insert(v_type(key, newValue));
}
ZBOOL RemoveKey( KEY key )
{
return this->erase(key) != 0;
}
ZBOOL Lookup( KEY key, VALUE& rValue ) const
{
#if (__GNUC__ >= 3) // The actual version number may be different.
const_iterator iter = std::map<KEY, VALUE>::find(key);
if (iter == std::map<KEY, VALUE>::end())
#else
const_iterator iter = find(key);
if (iter == end())
#endif
return FALSE;
else
{
rValue = iter->second;
return TRUE;
}
}
iterator GetStartPosition() { return this->begin();}
const_iterator GetStartPosition() const { return this->begin();}
bool IteratorValid(const_iterator &iter) const
{
return iter != this->end();
}
bool IteratorValid(iterator &iter)
{
return iter != this->end();
}
void GetNextAssoc(iterator &iter, KEY& key, VALUE& value)
{
key = iter->first;
value = iter->second;
iter++;
}
void GetNextAssoc(const_iterator &iter, KEY& key, VALUE& value)
{
key = iter->first;
value = iter->second;
iter++;
}
void RemoveAll() {this->clear();}
};
#if _MSC_VER > 1000
#pragma warning( pop)
#endif
|