unrarsrc  6.1.7
About: unrar extracts, views and tests the contents of archives created with the RAR archiver.
  Fossies Dox: unrarsrc-6.1.7.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

array.hpp
Go to the documentation of this file.
1#ifndef _RAR_ARRAY_
2#define _RAR_ARRAY_
3
5
6template <class T> class Array
7{
8 private:
10 size_t BufSize;
11 size_t AllocSize;
12 size_t MaxSize;
13 bool Secure; // Clean memory if true.
14 public:
16 Array(size_t Size);
17 Array(const Array &Src); // Copy constructor.
19 inline void CleanData();
20 inline T& operator [](size_t Item) const;
21 inline T* operator + (size_t Pos);
22 inline size_t Size(); // Returns the size in items, not in bytes.
23 void Add(size_t Items);
24 void Alloc(size_t Items);
25 void Reset();
26 void SoftReset();
28 void Push(T Item);
29 void Append(T *Item,size_t Count);
30 T* Addr(size_t Item) {return Buffer+Item;}
31 void SetMaxSize(size_t Size) {MaxSize=Size;}
32 T* Begin() {return Buffer;}
33 T* End() {return Buffer==NULL ? NULL:Buffer+BufSize;}
34 void SetSecure() {Secure=true;}
35};
36
37
38template <class T> void Array<T>::CleanData()
39{
40 Buffer=NULL;
41 BufSize=0;
42 AllocSize=0;
43 MaxSize=0;
44 Secure=false;
45}
46
47
48template <class T> Array<T>::Array()
49{
50 CleanData();
51}
52
53
54template <class T> Array<T>::Array(size_t Size)
55{
56 CleanData();
57 Add(Size);
58}
59
60
61// Copy constructor in case we need to pass an object as value.
62template <class T> Array<T>::Array(const Array &Src)
63{
64 CleanData();
65 Alloc(Src.BufSize);
66 if (Src.BufSize!=0)
67 memcpy((void *)Buffer,(void *)Src.Buffer,Src.BufSize*sizeof(T));
68}
69
70
71template <class T> Array<T>::~Array()
72{
73 if (Buffer!=NULL)
74 {
75 if (Secure)
76 cleandata(Buffer,AllocSize*sizeof(T));
77 free(Buffer);
78 }
79}
80
81
82template <class T> inline T& Array<T>::operator [](size_t Item) const
83{
84 return Buffer[Item];
85}
86
87
88template <class T> inline T* Array<T>::operator +(size_t Pos)
89{
90 return Buffer+Pos;
91}
92
93
94template <class T> inline size_t Array<T>::Size()
95{
96 return BufSize;
97}
98
99
100template <class T> void Array<T>::Add(size_t Items)
101{
102 BufSize+=Items;
103 if (BufSize>AllocSize)
104 {
105 if (MaxSize!=0 && BufSize>MaxSize)
106 {
107 ErrHandler.GeneralErrMsg(L"Maximum allowed array size (%u) is exceeded",MaxSize);
109 }
110
111 size_t Suggested=AllocSize+AllocSize/4+32;
112 size_t NewSize=Max(BufSize,Suggested);
113
114 T *NewBuffer;
115 if (Secure)
116 {
117 NewBuffer=(T *)malloc(NewSize*sizeof(T));
118 if (NewBuffer==NULL)
120 if (Buffer!=NULL)
121 {
122 memcpy(NewBuffer,Buffer,AllocSize*sizeof(T));
123 cleandata(Buffer,AllocSize*sizeof(T));
124 free(Buffer);
125 }
126 }
127 else
128 {
129 NewBuffer=(T *)realloc(Buffer,NewSize*sizeof(T));
130 if (NewBuffer==NULL)
132 }
133 Buffer=NewBuffer;
134 AllocSize=NewSize;
135 }
136}
137
138
139template <class T> void Array<T>::Alloc(size_t Items)
140{
141 if (Items>AllocSize)
142 Add(Items-BufSize);
143 else
144 BufSize=Items;
145}
146
147
148template <class T> void Array<T>::Reset()
149{
150 if (Buffer!=NULL)
151 {
152 free(Buffer);
153 Buffer=NULL;
154 }
155 BufSize=0;
156 AllocSize=0;
157}
158
159
160// Reset buffer size, but preserve already allocated memory if any,
161// so we can reuse it without wasting time to allocation.
162template <class T> void Array<T>::SoftReset()
163{
164 BufSize=0;
165}
166
167
168template <class T> void Array<T>::operator =(Array<T> &Src)
169{
170 Reset();
171 Alloc(Src.BufSize);
172 if (Src.BufSize!=0)
173 memcpy((void *)Buffer,(void *)Src.Buffer,Src.BufSize*sizeof(T));
174}
175
176
177template <class T> void Array<T>::Push(T Item)
178{
179 Add(1);
180 (*this)[Size()-1]=Item;
181}
182
183
184template <class T> void Array<T>::Append(T *Items,size_t Count)
185{
186 size_t CurSize=Size();
187 Add(Count);
188 memcpy(Buffer+CurSize,Items,Count*sizeof(T));
189}
190
191#endif
ErrorHandler ErrHandler
Definition: array.hpp:7
void SetSecure()
Definition: array.hpp:34
bool Secure
Definition: array.hpp:13
void Append(T *Item, size_t Count)
Definition: array.hpp:184
size_t BufSize
Definition: array.hpp:10
size_t MaxSize
Definition: array.hpp:12
void SetMaxSize(size_t Size)
Definition: array.hpp:31
size_t Size()
Definition: array.hpp:94
Array(size_t Size)
Definition: array.hpp:54
void operator=(Array< T > &Src)
Definition: array.hpp:168
void Add(size_t Items)
Definition: array.hpp:100
T & operator[](size_t Item) const
Definition: array.hpp:82
void Reset()
Definition: array.hpp:148
void CleanData()
Definition: array.hpp:38
void Alloc(size_t Items)
Definition: array.hpp:139
T * operator+(size_t Pos)
Definition: array.hpp:88
~Array()
Definition: array.hpp:71
T * End()
Definition: array.hpp:33
Array(const Array &Src)
Definition: array.hpp:62
T * Buffer
Definition: array.hpp:9
void SoftReset()
Definition: array.hpp:162
void Push(T Item)
Definition: array.hpp:177
Array()
Definition: array.hpp:48
T * Addr(size_t Item)
Definition: array.hpp:30
T * Begin()
Definition: array.hpp:32
size_t AllocSize
Definition: array.hpp:11
void GeneralErrMsg(const wchar *fmt,...)
Definition: errhnd.cpp:145
void MemoryError()
Definition: errhnd.cpp:22
#define Max(x, y)
Definition: rardefs.hpp:5
void cleandata(void *data, size_t size)
Definition: secpassword.cpp:80