00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "stdafx.h"
00023 #include "Utils.h"
00024
00025 namespace Util
00026 {
00027
00028
00029
00030
00031
00032
00033
00034 CComBSTR Copy( const std::string& str )
00035 {
00036 return ( str.empty() ) ? CComBSTR() : CComBSTR( str.c_str() );
00037 }
00038
00039 std::string Copy( const CComBSTR& bstr )
00040 {
00041 CStringA str( bstr );
00042 std::string strResult( str.GetBuffer( str.GetLength() ) );
00043 str.ReleaseBuffer();
00044 return strResult;
00045 }
00046
00047 std::wstring CopyW( const CComBSTR& bstr )
00048 {
00049 return std::wstring(static_cast<const wchar_t*>(bstr));
00050 }
00051
00052
00053
00054
00055
00056
00057
00058 Variant::Variant()
00059 : m_bUndefined( true ), m_eType( Variant::VT_Boolean )
00060 {
00061 }
00062
00063 Variant::Variant( bool bValue )
00064 : m_bUndefined( false ), m_eType( Variant::VT_Boolean )
00065 {
00066 m_uValue.bVal = bValue;
00067 }
00068
00069 Variant::Variant( long lValue )
00070 : m_bUndefined( false ), m_eType( Variant::VT_Integer )
00071 {
00072 m_uValue.lVal = lValue;
00073 }
00074
00075 Variant::Variant( double dValue )
00076 : m_bUndefined( false ), m_eType( Variant::VT_Double )
00077 {
00078 m_uValue.dVal = dValue;
00079 }
00080
00081 Variant::Variant( const std::string& strValue )
00082 : m_bUndefined( false ), m_eType( Variant::VT_String )
00083 {
00084 m_uValue.pstrVal = new std::string( strValue );
00085 }
00086
00087 Variant::Variant( const Variant& vValue )
00088 : m_bUndefined( vValue.m_bUndefined ), m_eType( vValue.m_eType )
00089 {
00090 if ( ! m_bUndefined ) {
00091 m_uValue = vValue.m_uValue;
00092 if ( m_eType == Variant::VT_String )
00093 m_uValue.pstrVal = new std::string( *m_uValue.pstrVal );
00094 }
00095 }
00096
00097 Variant::~Variant()
00098 {
00099 if ( ! m_bUndefined && m_eType == Variant::VT_String )
00100 delete m_uValue.pstrVal;
00101 }
00102
00103 Variant::Type Variant::type() const
00104 {
00105 return m_eType;
00106 }
00107
00108 bool Variant::isUndefined() const
00109 {
00110 return m_bUndefined;
00111 }
00112
00113 Variant& Variant::operator=( const Variant& vValue )
00114 {
00115 if ( this != &vValue ) {
00116 if ( ! m_bUndefined && m_eType == Variant::VT_String )
00117 delete m_uValue.pstrVal;
00118 m_eType = vValue.m_eType;
00119 m_bUndefined = vValue.m_bUndefined;
00120 m_uValue = vValue.m_uValue;
00121 if ( m_eType == Variant::VT_String )
00122 m_uValue.pstrVal = new std::string( *m_uValue.pstrVal );
00123 }
00124 return *this;
00125 }
00126
00127 Variant::operator bool () const
00128 {
00129 if ( m_bUndefined )
00130 return false;
00131 switch ( m_eType ) {
00132 case Variant::VT_Boolean :
00133 return m_uValue.bVal;
00134 case Variant::VT_String :
00135 return ! m_uValue.pstrVal->empty();
00136 case Variant::VT_Integer :
00137 return m_uValue.lVal != 0;
00138 default :
00139 return m_uValue.dVal != 0.0;
00140 }
00141 }
00142
00143 Variant::operator long () const
00144 {
00145 if ( m_bUndefined )
00146 return 0;
00147 switch ( m_eType ) {
00148 case Variant::VT_Boolean :
00149 return ( m_uValue.bVal ) ? 1 : 0;
00150 case Variant::VT_String :
00151 return atol( m_uValue.pstrVal->c_str() );
00152 case Variant::VT_Integer :
00153 return m_uValue.lVal;
00154 default :
00155 return (long) m_uValue.dVal;
00156 }
00157 }
00158
00159 Variant::operator double () const
00160 {
00161 if ( m_bUndefined )
00162 return 0.0;
00163 switch ( m_eType ) {
00164 case Variant::VT_Boolean :
00165 return ( m_uValue.bVal ) ? 1.0 : 0.0;
00166 case Variant::VT_String :
00167 return atof( m_uValue.pstrVal->c_str() );
00168 case Variant::VT_Integer :
00169 return (double) m_uValue.lVal;
00170 default :
00171 return m_uValue.dVal;
00172 }
00173 }
00174
00175 Variant::operator std::string () const
00176 {
00177 if ( m_bUndefined )
00178 return "";
00179 switch ( m_eType ) {
00180 case Variant::VT_Boolean :
00181 return ( m_uValue.bVal ) ? "true" : "false";
00182 case Variant::VT_String :
00183 return *m_uValue.pstrVal;
00184 case Variant::VT_Integer : {
00185 char chBuffer[ 100 ];
00186 sprintf( chBuffer, "%d", m_uValue.lVal );
00187 return chBuffer;
00188 }
00189 default : {
00190 char chBuffer[ 100 ];
00191 sprintf( chBuffer, "%f", m_uValue.dVal );
00192 return chBuffer;
00193 }
00194 }
00195 }
00196
00197 };
00198
00199
00200
00201
00202
00203
00204
00205 Util::InfoOption& operator ++ ( Util::InfoOption& eOption, int )
00206 {
00207 switch ( eOption ) {
00208 case Util::IO_None : eOption = Util::IO_Identifiers; break;
00209 case Util::IO_Identifiers : eOption = Util::IO_ID; break;
00210 case Util::IO_ID : eOption = Util::IO_Meta; break;
00211 case Util::IO_Meta : eOption = Util::IO_Path; break;
00212 case Util::IO_Path : eOption = Util::IO_Specific; break;
00213 case Util::IO_Specific : eOption = Util::IO_NewLine; break;
00214 case Util::IO_NewLine : eOption = Util::IO_All; break;
00215 case Util::IO_All : eOption = Util::IO_None; break;
00216 }
00217 return eOption= Util::IO_None;
00218 }
00219
00220 Util::InfoOption& operator ++ ( Util::InfoOption& eOption )
00221 {
00222 return eOption++;
00223 }
00224
00225 bool isAll( const std::set<Util::InfoOption>& setOptions )
00226 {
00227 if ( setOptions.find( Util::IO_All ) != setOptions.end() )
00228 return true;
00229
00230 for ( Util::InfoOption eOption = Util::IO_Identifiers; eOption != Util::IO_All ; eOption++ )
00231 if ( setOptions.find( eOption ) == setOptions.end() )
00232 return false;
00233 return true;
00234 }
00235
00236 bool isNone( const std::set<Util::InfoOption>& setOptions )
00237 {
00238 if ( setOptions.find( Util::IO_All ) != setOptions.end() )
00239 return true;
00240
00241 for ( Util::InfoOption eOption = Util::IO_Identifiers; eOption != Util::IO_All ; eOption++ )
00242 if ( setOptions.find( eOption ) != setOptions.end() )
00243 return false;
00244 return true;
00245 }
00246
00247 unsigned short copy( const std::set<Util::InfoOption>& setOptions )
00248 {
00249 unsigned short usOption = 0;
00250 for ( Util::InfoOption eOption = Util::IO_Identifiers; eOption != Util::IO_All ; eOption++ )
00251 if ( setOptions.find( eOption ) != setOptions.end() )
00252 usOption |= eOption;
00253 return usOption;
00254 }
00255
00256 std::set<Util::InfoOption> copy( unsigned short usOptions )
00257 {
00258 std::set<Util::InfoOption> setOptions;
00259 for ( Util::InfoOption eOption = Util::IO_Identifiers; eOption != Util::IO_All ; eOption++ )
00260 if ( usOptions & eOption )
00261 setOptions.insert( eOption );
00262 return setOptions;
00263 }