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

WorldGPS.cpp

Go to the documentation of this file.
00001 //
00002 // WorldGPS.cpp
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 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "WorldGPS.h"
00026 #include "Buffer.h"
00027 #include "XML.h"
00028 
00029 #ifdef _DEBUG
00030 #undef THIS_FILE
00031 static char THIS_FILE[]=__FILE__;
00032 #define new DEBUG_NEW
00033 #endif
00034 
00035 
00037 // CWorldGPS construction
00038 
00039 CWorldGPS::CWorldGPS()
00040 {
00041         m_pCountry = NULL;
00042         m_nCountry = 0;
00043 }
00044 
00045 CWorldGPS::~CWorldGPS()
00046 {
00047         Clear();
00048 }
00049 
00051 // CWorldGPS load
00052 
00053 BOOL CWorldGPS::Load(LPCTSTR pszFile)
00054 {
00055         CFile pFile;
00056 
00057         Clear();
00058 
00059         CString strFile = Settings.General.Path + _T("\\Data\\WorldGPS.dat");
00060         if ( ! pszFile ) pszFile = strFile;
00061 
00062         if ( ! pFile.Open( pszFile, CFile::modeRead ) ) return FALSE;
00063 
00064         if ( _tcsstr( pszFile, _T(".xml") ) == NULL )
00065         {
00066                 CArchive ar( &pFile, CArchive::load );
00067                 Serialize( ar );
00068                 ar.Close();
00069                 pFile.Close();
00070 
00071                 return TRUE;
00072         }
00073 
00074         CBuffer pBuffer;
00075         pBuffer.EnsureBuffer( (DWORD)pFile.GetLength() );
00076         pBuffer.m_nLength = (DWORD)pFile.GetLength();
00077         pFile.Read( pBuffer.m_pBuffer, pBuffer.m_nLength );
00078         pFile.Close();
00079 
00080         CXMLElement* pXML = CXMLElement::FromBytes( pBuffer.m_pBuffer, pBuffer.m_nLength );
00081         if ( NULL == pXML ) return FALSE;
00082 
00083         BOOL bSuccess = LoadFrom( pXML );
00084         delete pXML;
00085 
00086         if ( ! bSuccess ) return FALSE;
00087 
00088         if ( ! pFile.Open( strFile, CFile::modeWrite|CFile::modeCreate ) ) return FALSE;
00089 
00090         CArchive ar( &pFile, CArchive::store );
00091         Serialize( ar );
00092         ar.Close();
00093         pFile.Close();
00094 
00095         return TRUE;
00096 }
00097 
00098 void CWorldGPS::Serialize(CArchive& ar)
00099 {
00100         if ( ar.IsStoring() )
00101         {
00102                 ar.WriteCount( m_nCountry );
00103         }
00104         else
00105         {
00106                 m_nCountry = ar.ReadCount();
00107                 m_pCountry = new CWorldCountry[ m_nCountry ];
00108         }
00109 
00110         for ( DWORD nCountry = 0 ; nCountry < m_nCountry ; nCountry++ )
00111         {
00112                 m_pCountry[ nCountry ].Serialize( ar );
00113         }
00114 }
00115 
00116 BOOL CWorldGPS::LoadFrom(CXMLElement* pRoot)
00117 {
00118         if ( ! pRoot->IsNamed( _T("world") ) ) return FALSE;
00119 
00120         for ( POSITION pos = pRoot->GetElementIterator() ; pos ; )
00121         {
00122                 CXMLElement* pElement = pRoot->GetNextElement( pos );
00123                 if ( pElement->IsNamed( _T("country") ) ) m_nCountry++;
00124         }
00125 
00126         m_pCountry = new CWorldCountry[ m_nCountry ];
00127         m_nCountry = 0;
00128 
00129         for ( POSITION pos = pRoot->GetElementIterator() ; pos ; )
00130         {
00131                 CXMLElement* pElement = pRoot->GetNextElement( pos );
00132                 if ( pElement->IsNamed( _T("country") ) )
00133                         m_pCountry[ m_nCountry++ ].LoadFrom( pElement );
00134         }
00135 
00136         return m_nCountry > 0;
00137 }
00138 
00140 // CWorldGPS clear
00141 
00142 void CWorldGPS::Clear()
00143 {
00144         if ( m_pCountry ) delete [] m_pCountry;
00145         m_pCountry = NULL;
00146         m_nCountry = 0;
00147 }
00148 
00149 
00151 // CWorldCountry construction
00152 
00153 CWorldCountry::CWorldCountry()
00154 {
00155         m_pCity = NULL;
00156         m_nCity = 0;
00157 }
00158 
00159 CWorldCountry::~CWorldCountry()
00160 {
00161         Clear();
00162 }
00163 
00165 // CWorldCountry serialize
00166 
00167 void CWorldCountry::Serialize(CArchive& ar)
00168 {
00169         if ( ar.IsStoring() )
00170         {
00171                 ar << m_szID[0];
00172                 ar << m_szID[1];
00173                 ar << m_sName;
00174 
00175                 ar.WriteCount( m_nCity );
00176         }
00177         else
00178         {
00179                 ar >> m_szID[0];
00180                 ar >> m_szID[1];
00181                 ar >> m_sName;
00182 
00183                 m_nCity = ar.ReadCount();
00184                 m_pCity = new CWorldCity[ m_nCity ];
00185         }
00186 
00187         for ( DWORD nCity = 0 ; nCity < m_nCity ; nCity++ )
00188         {
00189                 m_pCity[ nCity ].Serialize( ar );
00190         }
00191 }
00192 
00193 BOOL CWorldCountry::LoadFrom(CXMLElement* pRoot)
00194 {
00195         if ( ! pRoot->IsNamed( _T("country") ) ) return FALSE;
00196 
00197         m_sName = pRoot->GetAttributeValue( _T("name") );
00198         CString strID = pRoot->GetAttributeValue( _T("code") );
00199 
00200         if ( strID.GetLength() != 2 ) return FALSE;
00201         m_szID[0] = (CHAR)strID.GetAt( 0 );
00202         m_szID[1] = (CHAR)strID.GetAt( 1 );
00203 
00204         for ( POSITION pos = pRoot->GetElementIterator() ; pos ; )
00205         {
00206                 CXMLElement* pElement = pRoot->GetNextElement( pos );
00207                 if ( pElement->IsNamed( _T("city") ) ) m_nCity++;
00208         }
00209 
00210         m_pCity = new CWorldCity[ m_nCity ];
00211         m_nCity = 0;
00212 
00213         for ( POSITION pos = pRoot->GetElementIterator() ; pos ; )
00214         {
00215                 CXMLElement* pElement = pRoot->GetNextElement( pos );
00216                 if ( pElement->IsNamed( _T("city") ) )
00217                         m_pCity[ m_nCity++ ].LoadFrom( pElement );
00218         }
00219 
00220         return m_nCity > 0;
00221 }
00222 
00224 // CWorldCountry clear
00225 
00226 void CWorldCountry::Clear()
00227 {
00228         if ( m_pCity ) delete [] m_pCity;
00229         m_pCity = NULL;
00230         m_nCity = 0;
00231 }
00232 
00234 // CWorldCity load
00235 
00236 void CWorldCity::Serialize(CArchive& ar)
00237 {
00238         WORD nValue;
00239 
00240         if ( ar.IsStoring() )
00241         {
00242                 ar << m_sName;
00243                 ar << m_sState;
00244 
00245                 nValue = (WORD)(signed short)( m_nLatitude / 90.0f * 32700.0f );
00246                 ar << nValue;
00247                 nValue = (WORD)(signed short)( m_nLongitude / 180.0f * 32700.0f );
00248                 ar << nValue;
00249         }
00250         else
00251         {
00252                 ar >> m_sName;
00253                 ar >> m_sState;
00254 
00255                 ar >> nValue;
00256                 m_nLatitude = (float)(signed short)nValue / 32700.0f * 90.0f;
00257 
00258                 ar >> nValue;
00259                 m_nLongitude = (float)(signed short)nValue / 32700.0f * 180.0f;
00260         }
00261 }
00262 
00263 BOOL CWorldCity::LoadFrom(CXMLElement* pRoot)
00264 {
00265         if ( ! pRoot->IsNamed( _T("city") ) ) return FALSE;
00266 
00267         m_sName         = pRoot->GetAttributeValue( _T("name") );
00268         m_sState        = pRoot->GetAttributeValue( _T("state") );
00269 
00270         CString strValue = pRoot->GetAttributeValue( _T("latitude") );
00271         if ( _stscanf( strValue, _T("%f"), &m_nLatitude ) != 1 ) return FALSE;
00272 
00273         strValue = pRoot->GetAttributeValue( _T("longitude") );
00274         if ( _stscanf( strValue, _T("%f"), &m_nLongitude ) != 1 ) return FALSE;
00275 
00276         return TRUE;
00277 }
00278 

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