reduce the scope of some variables
[amule.git] / src / Tag.h
blobeb5ae2570deb3fa29119a65c2e56b95c62cdd7d5
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #ifndef TAG_H
27 #define TAG_H
30 #include <common/StringFunctions.h> // Needed for EUtf8Str
32 #include <tags/TagTypes.h>
34 #include "OtherFunctions.h"
36 class CMD4Hash;
37 class CFileDataIO;
39 ///////////////////////////////////////////////////////////////////////////////
40 // CTag
42 class CTag
44 public:
45 CTag(const CTag& rTag);
46 CTag(const CFileDataIO& data, bool bOptUTF8);
47 virtual ~CTag();
48 CTag& operator=(const CTag&);
50 uint8 GetType() const { return m_uType; }
51 uint8 GetNameID() const { return m_uName; }
52 const wxString& GetName() const { return m_Name; }
54 bool IsStr() const { return m_uType == TAGTYPE_STRING; }
55 bool IsInt() const { return
56 (m_uType == TAGTYPE_UINT64) ||
57 (m_uType == TAGTYPE_UINT32) ||
58 (m_uType == TAGTYPE_UINT16) ||
59 (m_uType == TAGTYPE_UINT8); }
60 bool IsFloat() const { return m_uType == TAGTYPE_FLOAT32; }
61 bool IsHash() const { return m_uType == TAGTYPE_HASH16; }
62 bool IsBlob() const { return m_uType == TAGTYPE_BLOB; }
63 bool IsBsob() const { return m_uType == TAGTYPE_BSOB; }
65 uint64 GetInt() const;
67 const wxString& GetStr() const;
69 float GetFloat() const;
71 const CMD4Hash& GetHash() const;
73 const byte* GetBlob() const;
74 uint32 GetBlobSize() const;
76 const byte* GetBsob() const;
77 uint32 GetBsobSize() const;
79 CTag* CloneTag() { return new CTag(*this); }
81 bool WriteTagToFile(CFileDataIO* file,
82 EUtf8Str eStrEncode = utf8strNone,
83 bool restrictive = true) const; // old eD2K tags
84 bool WriteNewEd2kTag(CFileDataIO* file,
85 EUtf8Str eStrEncode = utf8strNone) const; // new eD2K tags
87 wxString GetFullInfo() const;
89 protected:
90 CTag(const wxString& Name);
91 CTag(uint8 uName);
93 uint8 m_uType;
94 union {
95 CMD4Hash* m_hashVal;
96 wxString* m_pstrVal;
97 uint64 m_uVal;
98 float m_fVal;
99 unsigned char* m_pData;
102 uint32 m_nSize;
104 private:
105 uint8 m_uName;
106 wxString m_Name;
110 typedef std::list<CTag*> TagPtrList;
112 class CTagIntSized : public CTag
114 public:
115 CTagIntSized(const wxString& name, uint64 value, uint8 bitsize)
116 : CTag(name) {
117 Init(value, bitsize);
120 CTagIntSized(uint8 name, uint64 value, uint8 bitsize)
121 : CTag(name) {
122 Init(value, bitsize);
125 protected:
126 CTagIntSized(const wxString& name) : CTag(name) {}
127 CTagIntSized(uint8 name) : CTag(name) {}
129 void Init(uint64 value, uint8 bitsize) {
130 switch (bitsize) {
131 case 64:
132 wxASSERT(value <= ULONGLONG(0xFFFFFFFFFFFFFFFF));
133 m_uVal = value;
134 m_uType = TAGTYPE_UINT64;
135 break;
136 case 32:
137 wxASSERT(value <= 0xFFFFFFFF);
138 m_uVal = value;
139 m_uType = TAGTYPE_UINT32;
140 break;
141 case 16:
142 wxASSERT(value <= 0xFFFF);
143 m_uVal = value;
144 m_uType = TAGTYPE_UINT16;
145 break;
146 case 8:
147 wxASSERT(value <= 0xFF);
148 m_uVal = value;
149 m_uType = TAGTYPE_UINT8;
150 break;
151 default:
152 throw wxString(wxT("Invalid bitsize on int tag"));
157 class CTagVarInt : public CTagIntSized
159 public:
160 CTagVarInt(const wxString& name, uint64 value, uint8 forced_bits = 0)
161 : CTagIntSized(name) {
162 SizedInit(value, forced_bits);
164 CTagVarInt(uint8 name, uint64 value, uint8 forced_bits = 0)
165 : CTagIntSized(name) {
166 SizedInit(value, forced_bits);
168 private:
169 void SizedInit(uint64 value, uint8 forced_bits) {
170 if (forced_bits) {
171 // The bitsize was forced.
172 Init(value,forced_bits);
173 } else {
174 m_uVal = value;
175 if (value <= 0xFF) {
176 m_uType = TAGTYPE_UINT8;
177 } else if (value <= 0xFFFF) {
178 m_uType = TAGTYPE_UINT16;
179 } else if (value <= 0xFFFFFFFF) {
180 m_uType = TAGTYPE_UINT32;
181 } else {
182 m_uType = TAGTYPE_UINT64;
188 class CTagInt64 : public CTagIntSized
190 public:
191 CTagInt64(const wxString& name, uint64 value)
192 : CTagIntSized(name, value, 64) { }
194 CTagInt64(uint8 name, uint64 value)
195 : CTagIntSized(name, value, 64) { }
198 class CTagInt32 : public CTagIntSized
200 public:
201 CTagInt32(const wxString& name, uint64 value)
202 : CTagIntSized(name, value, 32) { }
204 CTagInt32(uint8 name, uint64 value)
205 : CTagIntSized(name, value, 32) { }
208 class CTagInt16 : public CTagIntSized
210 public:
211 CTagInt16(const wxString& name, uint64 value)
212 : CTagIntSized(name, value, 16) { }
214 CTagInt16(uint8 name, uint64 value)
215 : CTagIntSized(name, value, 16) { }
218 class CTagInt8 : public CTagIntSized
220 public:
221 CTagInt8(const wxString& name, uint64 value)
222 : CTagIntSized(name, value, 8) { }
224 CTagInt8(uint8 name, uint64 value)
225 : CTagIntSized(name, value, 8) { }
228 class CTagFloat : public CTag
230 public:
231 CTagFloat(const wxString& name, float value)
232 : CTag(name) {
233 m_fVal = value;
234 m_uType = TAGTYPE_FLOAT32;
237 CTagFloat(uint8 name, float value)
238 : CTag(name) {
239 m_fVal = value;
240 m_uType = TAGTYPE_FLOAT32;
244 class CTagString : public CTag
246 public:
247 CTagString(const wxString& name, const wxString& value)
248 : CTag(name) {
249 m_pstrVal = new wxString(value);
250 m_uType = TAGTYPE_STRING;
253 CTagString(uint8 name, const wxString& value)
254 : CTag(name) {
255 m_pstrVal = new wxString(value);
256 m_uType = TAGTYPE_STRING;
260 class CTagHash : public CTag
262 public:
263 // Implementation on .cpp to allow forward declaration of CMD4Hash
264 CTagHash(const wxString& name, const CMD4Hash& value);
265 CTagHash(uint8 name, const CMD4Hash& value);
268 class CTagBsob : public CTag
270 public:
271 CTagBsob(const wxString& name, const byte* value, uint8 nSize)
272 : CTag(name)
274 m_uType = TAGTYPE_BSOB;
275 m_pData = new byte[nSize];
276 memcpy(m_pData, value, nSize);
277 m_nSize = nSize;
281 class CTagBlob : public CTag
283 public:
284 CTagBlob(const wxString& name, const byte* value, uint8 nSize)
285 : CTag(name)
287 m_uType = TAGTYPE_BLOB;
288 m_pData = new byte[nSize];
289 memcpy(m_pData, value, nSize);
290 m_nSize = nSize;
294 void deleteTagPtrListEntries(TagPtrList* taglist);
296 #endif // TAG_H
297 // File_checked_for_headers