Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

BENode.h

Go to the documentation of this file.
00001 //
00002 // BENode.h
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #if !defined(AFX_BENODE_H__8E447816_68F5_461A_A032_09A0CB97F3CB__INCLUDED_)
00023 #define AFX_BENODE_H__8E447816_68F5_461A_A032_09A0CB97F3CB__INCLUDED_
00024 
00025 #pragma once
00026 
00027 class CBuffer;
00028 
00029 
00030 class CBENode  
00031 {
00032 // Construction
00033 public:
00034         CBENode();
00035         ~CBENode();
00036 
00037 // Attributes
00038 public:
00039         int                     m_nType;
00040         LPVOID          m_pValue;
00041         QWORD           m_nValue;
00042         
00043         enum { beNull, beString, beInt, beList, beDict };
00044         
00045 // Operations
00046 public:
00047         void            Clear();
00048         CBENode*        Add(const LPBYTE pKey, int nKey);
00049         CBENode*        GetNode(LPCSTR pszKey) const;
00050         CBENode*        GetNode(const LPBYTE pKey, int nKey) const;
00051         void            GetSHA1(SHA1* pSHA1) const;
00052         CString         GetStringFromSubNode(LPCSTR pszKey, UINT nEncoding, BOOL* pEncodingError);
00053         CString         GetStringFromSubNode(int nItem, UINT nEncoding, BOOL* pEncodingError);
00054         void            Encode(CBuffer* pBuffer) const;
00055 public:
00056         static CBENode* Decode(CBuffer* pBuffer);
00057 private:
00058         void            Decode(LPBYTE& pInput, DWORD& nInput);
00059         static int      DecodeLen(LPBYTE& pInput, DWORD& nInput);
00060 
00061 
00062 // Inline
00063 public:
00064         inline bool IsType(int nType) const
00065         {
00066                 if ( this == NULL ) return false;
00067                 return m_nType == nType;
00068         }
00069         
00070         inline QWORD GetInt() const
00071         {
00072                 if ( m_nType != beInt ) return 0;
00073                 return m_nValue;
00074         }
00075         
00076         inline void SetInt(QWORD nValue)
00077         {
00078                 Clear();
00079                 m_nType         = beInt;
00080                 m_nValue        = nValue;
00081         }
00082         
00083         inline CString GetString() const
00084         {
00085                 CString str;
00086                 if ( m_nType != beString ) return str;
00087                 str = (LPCSTR)m_pValue;
00088 
00089                 const DWORD nFlags = ( theApp.m_dwWindowsVersion == 4 ) ? 0 : MB_ERR_INVALID_CHARS;
00090 
00091                 int nLength = MultiByteToWideChar( CP_UTF8, nFlags, (LPCSTR)m_pValue, -1, NULL, 0 );
00092                 if ( nLength > 0 )
00093                         MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)m_pValue, -1, str.GetBuffer( nLength ), nLength );
00094                 else
00095                 {
00096                         // Bad encoding
00097                         str.ReleaseBuffer();
00098                         str = _T("#ERROR#");
00099                         return str;
00100                 }
00101                 str.ReleaseBuffer();
00102 
00103                 return str;
00104         }
00105 
00106         // If a torrent is badly encoded, you can try forcing a code page.
00107         inline CString DecodeString(UINT nCodePage) const
00108         {
00109                 CString str;
00110                 if ( m_nType != beString ) return str;
00111                 str = (LPCSTR)m_pValue;
00112                 int nSource = str.GetLength();
00113                 if ( nSource == 0 ) return str;
00114                 const DWORD nFlags = ( theApp.m_dwWindowsVersion == 4 ) ? 0 : MB_ERR_INVALID_CHARS;
00115                 int nLength = 0;
00116 
00117                 // Use the torrent code page (if present)
00118                 if ( nCodePage != 0 ) 
00119                         nLength = MultiByteToWideChar( nCodePage, nFlags, (LPCSTR)m_pValue, -1 , NULL, 0 );
00120 
00121                 if ( nLength > 0 )
00122                         MultiByteToWideChar( nCodePage, 0, (LPCSTR)m_pValue, -1 , str.GetBuffer( nLength ), nLength );
00123                 else
00124                 {               
00125                         // Try the user-specified code page if it's set, else use the system code page
00126                         if ( Settings.BitTorrent.TorrentCodePage != 0 )
00127                                 nCodePage = Settings.BitTorrent.TorrentCodePage;
00128                         else
00129                                 nCodePage = GetOEMCP();
00130 
00131                         nLength = MultiByteToWideChar( nCodePage, nFlags, (LPCSTR)m_pValue, nSource, NULL, 0 );
00132 
00133                         if ( nLength > 0 )
00134                                 MultiByteToWideChar( nCodePage, 0, (LPCSTR)m_pValue, nSource, str.GetBuffer( nLength ), nLength );
00135                         else
00136                         {
00137                                 // Try ACP. (Should convert anything, but badly)
00138                                 nCodePage = CP_ACP;
00139                                 nLength = MultiByteToWideChar( nCodePage, nFlags, (LPCSTR)m_pValue, nSource, NULL, 0 );
00140                                 if ( nLength > 0 )
00141                                         MultiByteToWideChar( nCodePage, 0, (LPCSTR)m_pValue, nSource, str.GetBuffer( nLength ), nLength );
00142                                 else
00143                                 {
00144                                         // Nothing more we can do
00145                                         str = _T("#ERROR#");
00146                                         return str;
00147                                 }
00148                         }
00149                 }
00150                 str.ReleaseBuffer();
00151 
00152                 return str;
00153         }
00154 
00155         // Check if a string is a valid path/file name.
00156         inline BOOL IsValid(LPCTSTR psz) const
00157         {
00158                 if ( _tcsclen( psz ) == 0 ) return FALSE;
00159                 if ( _tcschr( psz, '?' ) != NULL ) return FALSE;
00160                 if ( _tcsicmp( psz , _T("#ERROR#") ) == 0 ) return FALSE;
00161                 
00162                 return TRUE;
00163         }
00164 
00165         inline void SetString(LPCSTR psz)
00166         {
00167                 SetString( psz, strlen(psz), TRUE );
00168         }
00169         
00170         void SetString(LPCWSTR psz)
00171         {
00172                 USES_CONVERSION;
00173                 LPCSTR pszASCII = W2CA(psz);
00174                 SetString( pszASCII, strlen(pszASCII), TRUE );
00175         }
00176         
00177         inline void SetString(LPCVOID pString, DWORD nLength, BOOL bNull = FALSE)
00178         {
00179                 Clear();
00180                 m_nType         = beString;
00181                 m_nValue        = (QWORD)nLength;
00182                 m_pValue        = new BYTE[ nLength + ( bNull ? 1 : 0 ) ];
00183                 CopyMemory( m_pValue, pString, nLength + ( bNull ? 1 : 0 ) );
00184         }
00185         
00186         inline CBENode* Add(LPCSTR pszKey = NULL)
00187         {
00188                 return Add( (LPBYTE)pszKey, pszKey != NULL ? strlen( pszKey ) : 0 );
00189         }
00190         
00191         inline int GetCount() const
00192         {
00193                 if ( m_nType != beList && m_nType != beDict ) return 0;
00194                 return (int)m_nValue;
00195         }
00196         
00197         inline CBENode* GetNode(int nItem) const
00198         {
00199                 if ( m_nType != beList && m_nType != beDict ) return NULL;
00200                 if ( m_nType == beDict ) nItem *= 2;
00201                 if ( nItem < 0 || nItem >= (int)m_nValue ) return NULL;
00202                 return *( (CBENode**)m_pValue + nItem );
00203         }
00204 };
00205 
00206 #endif // !defined(AFX_BENODE_H__8E447816_68F5_461A_A032_09A0CB97F3CB__INCLUDED_)

Generated on Thu Dec 15 10:39:33 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2