2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002 Merkur ( devs@emule-project.net / http://www.emule-project.net )
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
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 OTHERFUNCTIONS_H
27 #define OTHERFUNCTIONS_H
29 #include <wx/intl.h> // Needed for wxLANGUAGE_ constants
31 #include "Types.h" // Needed for uint16, uint32 and uint64
33 #include <algorithm> // Needed for std::for_each // Do_not_auto_remove (mingw-gcc-3.4.5)
42 * @param ArgA The base value.
43 * @param ArgB The value to compare ArgA against.
46 * Use this function to safely compare two arguments of a type that supports
47 * the "<" operator. It works like strcmp and returns a negative value if ArgA
48 * is less than ArgB, zero if ArgA is equal to ArgB and a positive value if
49 * ArgA is greater than ArgB.
52 int CmpAny(const TYPE
& ArgA
, const TYPE
& ArgB
)
56 } else if ( ArgB
< ArgA
) {
63 //! Overloaded version of CmpAny for use with wxStrings.
64 inline int CmpAny(const wxString
& ArgA
, const wxString
& ArgB
)
66 return ArgA
.CmpNoCase( ArgB
);
69 //! Overloaded version of CmpAny for use with C-Strings (Unicoded).
70 inline int CmpAny(const wxChar
* ArgA
, const wxChar
* ArgB
)
72 return wxString( ArgA
).CmpNoCase( ArgB
);
77 * Removes the first instance of a value from a STL-like list: list, vector or deque.
79 * @param list The list to manipulate.
80 * @param item The value to search for and remove.
81 * @return The number of instances removed.
83 template <typename LIST
, typename ITEM
>
84 unsigned int EraseFirstValue( LIST
& list
, const ITEM
& item
)
86 typename
LIST::iterator it
= list
.begin();
88 for (; it
!= list
.end(); ++it
) {
101 * Removes all instances of a value from a STL-like list: list, vector or deque.
103 * @param list The list to manipulate.
104 * @param item The value to search for and remove.
105 * @return The number of instances removed.
107 template <typename LIST
, typename ITEM
>
108 unsigned int EraseValue( LIST
& list
, const ITEM
& item
)
110 typename
LIST::iterator it
= list
.begin();
111 unsigned int count
= 0;
113 for ( ; it
!= list
.end(); ) {
115 it
= list
.erase( it
);
126 //! Used by DeleteContents
129 // Used for lists, vectors, deques, etc.
130 template <typename TYPE
>
131 void operator()(TYPE
* ptr
) {
135 // Used for maps, hashmaps, rangemaps, etc.
136 template <typename FIRST
, typename SECOND
>
137 void operator()(const std::pair
<FIRST
, SECOND
>& pair
) {
143 /** Frees the contents of a list or map like stl container, clearing it afterwards. */
144 template <typename STL_CONTAINER
>
145 void DeleteContents(STL_CONTAINER
& container
)
147 // Ensure that the actual container wont contain dangling pointers during
148 // this operation, to ensure that the destructors cant access them.
151 std::swap(copy
, container
);
152 std::for_each(copy
.begin(), copy
.end(), SDoDelete());
157 * Copies elements from the range [first, first + n) to the range [result, result + n).
159 template <class InputIterator
, class OutputIterator
>
160 OutputIterator
STLCopy_n(InputIterator first
, size_t n
, OutputIterator result
)
162 return std::copy(first
, first
+ n
, result
);
167 * Returns a description of the version of aMule being used.
169 * @return A detailed description of the aMule version, including wx information.
171 * Use this rather than just using the VERSION or CURRENT_VERSION_LONG
172 * constants, when displaying information to the user. The purpose is to
173 * help with debugging.
175 wxString
GetMuleVersion();
178 * This functions is like the GetMuleVersion function above, with the exception
179 * that it also includes the name of the application. This can be one of the
186 wxString
GetFullMuleVersion();
190 * Helperfunction for accessing a child of the calling widget.
192 * @param IdOrName The ID or the Name of the widget to find.
193 * @param type The widget-type to cast the found widget to.
195 * Use this function as a replacement for the following constructs:
196 * - wxStaticCast( FindWindow( <IdOrName> ), <type> )
197 * - (<type>*)FindWindow( <IdOrName> )
199 * It has the advantage of validating the cast in debug builds and being much
200 * shorter than than manually typing wxStaticCast + FindWindow. This mean that
201 * we will be alerted in case of widget changing type, instead of getting just
202 * getting bad mojo due to casting a pointer to the wrong type.
204 #define CastChild( IdOrName, type ) dynamic_cast<type*>( FindWindow( IdOrName ) )
208 * Helperfunction for accessing the child of a any widget by ID.
210 * @param ID The ID of the widget to find.
211 * @param parent The parent of the widget to find, or NULL to search from the top.
212 * @param type The type to cast the widget to.
216 #define CastByID( ID, parent, type ) dynamic_cast<type*>( wxWindow::FindWindowById( (ID), (parent) ) )
220 * Helperfunction for accessing the child of a any widget by Name.
222 * @param Name The Name of the widget to find.
223 * @param parent The parent of the widget to find, or NULL to search from the top.
224 * @param type The type to cast the widget to.
228 #define CastByName( Name, parent, type ) dynamic_cast<type*>( wxWindow::FindWindowByName( (Name), (parent) ) )
231 // From Gnucleus project [found by Tarod]
232 // Base16/Base32/Base64 Encode/Decode functions
233 wxString
EncodeBase16(const unsigned char* buffer
, unsigned int bufLen
);
234 unsigned int DecodeBase16(const wxString
&base16Buffer
, unsigned int base16BufLen
, unsigned char *buffer
);
235 wxString
EncodeBase32(const unsigned char* buffer
, unsigned int bufLen
);
236 unsigned int DecodeBase32(const wxString
&base32Buffer
, unsigned int base32BufLen
, unsigned char *buffer
);
237 wxString
EncodeBase64(const char* buffer
, unsigned int bufLen
);
238 unsigned int DecodeBase64(const wxString
&base64Buffer
, unsigned int base64BufLen
, unsigned char *buffer
);
240 // Converts the number of bytes to human readable form.
241 wxString
CastItoXBytes(uint64 count
);
242 // Converts the number to human readable form, abbreviating when nessecary.
243 wxString
CastItoIShort(uint64 number
);
244 // Converts a number of bytes to a human readable speed value.
245 wxString
CastItoSpeed(uint32 bytes
);
246 // Converts an amount of seconds to human readable time.
247 wxString
CastSecondsToHM(uint64 seconds
, uint16 msecs
= 0);
248 // Returns the amount of Bytes the provided size-type represents
249 uint32
GetTypeSize(uint8 type
);
250 // Returns the string associated with a file-rating value.
251 wxString
GetRateString(uint16 rate
);
254 // The following functions are used to identify and/or name the type of a file
255 enum FileType
{ ftAny
, ftVideo
, ftAudio
, ftArchive
, ftCDImage
, ftPicture
, ftText
, ftProgram
};
256 // Examins a filename and returns the enumerated value assosiated with it, or ftAny if unknown extension
257 FileType
GetFiletype(const CPath
& filename
);
258 // Returns the description of a filetype: Movies, Audio, Pictures and so on...
259 wxString
GetFiletypeDesc(FileType type
, bool translated
= true);
260 // Shorthand for GetFiletypeDesc(GetFiletype(filename))
261 wxString
GetFiletypeByName(const CPath
& filename
, bool translated
= true);
264 // Returns the max number of connections the current OS can handle.
265 // Currently anything but windows will return the default value (-1);
266 int GetMaxConnections();
267 // Returns the name assosiated with a category value.
268 wxString
GetCatTitle(int catid
);
273 //! Returns the number of items in an array.
274 #define itemsof(x) (sizeof(x)/sizeof(x[0]))
277 ///////////////////////////////////////////////////////////////////////////////
293 class EED2KFileTypeClass
300 EED2KFileTypeClass(EED2KFileType t
)
304 EED2KFileType
GetType() const
313 EED2KFileType
GetED2KFileTypeID(const CPath
& fileName
);
314 wxString
GetED2KFileTypeSearchTerm(EED2KFileType iFileID
);
315 wxString
GetFileTypeByName(const CPath
& fileName
);
316 EED2KFileType
GetED2KFileTypeSearchID(EED2KFileType iFileID
);
317 ///////////////////////////////////////////////////////////////////////////////
319 // md4cmp -- replacement for memcmp(hash1,hash2,16)
320 // Like 'memcmp' this function returns 0, if hash1==hash2, and !0, if hash1!=hash2.
321 // NOTE: Do *NOT* use that function for determining if hash1<hash2 or hash1>hash2.
322 inline int md4cmp(const void* hash1
, const void* hash2
)
324 return memcmp(hash1
, hash2
, 16);
328 // md4clr -- replacement for memset(hash,0,16)
329 inline void md4clr(void* hash
)
335 // md4cpy -- replacement for memcpy(dst,src,16)
336 inline void md4cpy(void* dst
, const void* src
)
338 memcpy(dst
, src
, 16);
342 // DumpMem ... Dumps mem ;)
343 wxString
DumpMemToStr(const void *buff
, int n
, const wxString
& msg
= wxEmptyString
, bool ok
= true);
344 void DumpMem(const void *buff
, int n
, const wxString
& msg
= wxEmptyString
, bool ok
= true);
345 void DumpMem_DW(const uint32
*ptr
, int count
);
347 // Returns special source ID for GUI.
348 // It's actually IP<<16+Port
349 #define GUI_ID(x,y) (uint64)((((uint64)x)<<16) + (uint64)y)
351 #define PORT_FROM_GUI_ID(x) (x & 0xFFFF)
352 #define IP_FROM_GUI_ID(x) (x >> 16)
355 void MilliSleep(uint32 msecs
);
358 inline const long int make_full_ed2k_version(int a
, int b
, int c
) {
359 return ((a
<< 17) | (b
<< 10) | (c
<< 7));
363 wxString
GetConfigDir();
365 #define wxLANGUAGE_CUSTOM wxLANGUAGE_USER_DEFINED+1
366 #define wxLANGUAGE_ITALIAN_NAPOLITAN wxLANGUAGE_USER_DEFINED+2
369 * Adds aMule's custom languages to db.
371 void InitCustomLanguages();
376 void InitLocale(wxLocale
& locale
, int language
);
379 * Converts a string locale definition to a wxLANGUAGE id.
381 int StrLang2wx(const wxString
& language
);
384 * Converts a wxLANGUAGE id to a string locale name.
386 wxString
wxLang2Str(const int lang
);
389 * Generate MD5Hash of prompt input
391 wxString
GetPassword();
396 #include <wx/thread.h>
399 * Automatically unlocks a mutex on construction and locks it on destruction.
401 * This class is the complement of wxMutexLocker. It is intended to be used
402 * when a mutex, which is locked for a period of time, needs to be
403 * temporarily unlocked for a bit. For example:
405 * wxMutexLocker lock(mutex);
407 * // ... do stuff that requires that the mutex is locked ...
410 * CMutexUnlocker unlocker(mutex);
411 * // ... do stuff that requires that the mutex is unlocked ...
414 * // ... do more stuff that requires that the mutex is locked ...
420 // unlock the mutex in the ctor
421 CMutexUnlocker(wxMutex
& mutex
)
422 : m_isOk(false), m_mutex(mutex
)
423 { m_isOk
= ( m_mutex
.Unlock() == wxMUTEX_NO_ERROR
); }
425 // returns true if mutex was successfully unlocked in ctor
429 // lock the mutex in dtor
431 { if ( IsOk() ) m_mutex
.Lock(); }
434 // no assignment operator nor copy ctor
435 CMutexUnlocker(const CMutexUnlocker
&);
436 CMutexUnlocker
& operator=(const CMutexUnlocker
&);
441 #endif /* wxUSE_THREADS */
444 #endif // OTHERFUNCTIONS_H
445 // File_checked_for_headers