diff src/win32/7zip/7z/CPP/Windows/PropVariant.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/7zip/7z/CPP/Windows/PropVariant.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,312 @@
     1.4 +// Windows/PropVariant.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#include "PropVariant.h"
     1.9 +
    1.10 +#include "../Common/Defs.h"
    1.11 +
    1.12 +namespace NWindows {
    1.13 +namespace NCOM {
    1.14 +
    1.15 +CPropVariant::CPropVariant(const PROPVARIANT& varSrc)
    1.16 +{
    1.17 +  vt = VT_EMPTY;
    1.18 +  InternalCopy(&varSrc);
    1.19 +}
    1.20 +
    1.21 +CPropVariant::CPropVariant(const CPropVariant& varSrc)
    1.22 +{
    1.23 +  vt = VT_EMPTY;
    1.24 +  InternalCopy(&varSrc);
    1.25 +}
    1.26 +
    1.27 +CPropVariant::CPropVariant(BSTR bstrSrc)
    1.28 +{
    1.29 +  vt = VT_EMPTY;
    1.30 +  *this = bstrSrc;
    1.31 +}
    1.32 +
    1.33 +CPropVariant::CPropVariant(LPCOLESTR lpszSrc)
    1.34 +{
    1.35 +  vt = VT_EMPTY;
    1.36 +  *this = lpszSrc;
    1.37 +}
    1.38 +
    1.39 +CPropVariant& CPropVariant::operator=(const CPropVariant& varSrc)
    1.40 +{
    1.41 +  InternalCopy(&varSrc);
    1.42 +  return *this;
    1.43 +}
    1.44 +CPropVariant& CPropVariant::operator=(const PROPVARIANT& varSrc)
    1.45 +{
    1.46 +  InternalCopy(&varSrc);
    1.47 +  return *this;
    1.48 +}
    1.49 +
    1.50 +CPropVariant& CPropVariant::operator=(BSTR bstrSrc)
    1.51 +{
    1.52 +  *this = (LPCOLESTR)bstrSrc;
    1.53 +  return *this;
    1.54 +}
    1.55 +
    1.56 +CPropVariant& CPropVariant::operator=(LPCOLESTR lpszSrc)
    1.57 +{
    1.58 +  InternalClear();
    1.59 +  vt = VT_BSTR;
    1.60 +  wReserved1 = 0;
    1.61 +  bstrVal = ::SysAllocString(lpszSrc);
    1.62 +  if (bstrVal == NULL && lpszSrc != NULL)
    1.63 +  {
    1.64 +    vt = VT_ERROR;
    1.65 +    scode = E_OUTOFMEMORY;
    1.66 +  }
    1.67 +  return *this;
    1.68 +}
    1.69 +
    1.70 +
    1.71 +CPropVariant& CPropVariant::operator=(bool bSrc)
    1.72 +{
    1.73 +  if (vt != VT_BOOL)
    1.74 +  {
    1.75 +    InternalClear();
    1.76 +    vt = VT_BOOL;
    1.77 +  }
    1.78 +  boolVal = bSrc ? VARIANT_TRUE : VARIANT_FALSE;
    1.79 +  return *this;
    1.80 +}
    1.81 +
    1.82 +CPropVariant& CPropVariant::operator=(UInt32 value)
    1.83 +{
    1.84 +  if (vt != VT_UI4)
    1.85 +  {
    1.86 +    InternalClear();
    1.87 +    vt = VT_UI4;
    1.88 +  }
    1.89 +  ulVal = value;
    1.90 +  return *this;
    1.91 +}
    1.92 +
    1.93 +CPropVariant& CPropVariant::operator=(UInt64 value)
    1.94 +{
    1.95 +  if (vt != VT_UI8)
    1.96 +  {
    1.97 +    InternalClear();
    1.98 +    vt = VT_UI8;
    1.99 +  }
   1.100 +  uhVal.QuadPart = value;
   1.101 +  return *this;
   1.102 +}
   1.103 +
   1.104 +CPropVariant& CPropVariant::operator=(const FILETIME &value)
   1.105 +{
   1.106 +  if (vt != VT_FILETIME)
   1.107 +  {
   1.108 +    InternalClear();
   1.109 +    vt = VT_FILETIME;
   1.110 +  }
   1.111 +  filetime = value;
   1.112 +  return *this;
   1.113 +}
   1.114 +
   1.115 +CPropVariant& CPropVariant::operator=(Int32 value)
   1.116 +{
   1.117 +  if (vt != VT_I4)
   1.118 +  {
   1.119 +    InternalClear();
   1.120 +    vt = VT_I4;
   1.121 +  }
   1.122 +  lVal = value;
   1.123 +  
   1.124 +  return *this;
   1.125 +}
   1.126 +
   1.127 +CPropVariant& CPropVariant::operator=(Byte value)
   1.128 +{
   1.129 +  if (vt != VT_UI1)
   1.130 +  {
   1.131 +    InternalClear();
   1.132 +    vt = VT_UI1;
   1.133 +  }
   1.134 +  bVal = value;
   1.135 +  return *this;
   1.136 +}
   1.137 +
   1.138 +CPropVariant& CPropVariant::operator=(Int16 value)
   1.139 +{
   1.140 +  if (vt != VT_I2)
   1.141 +  {
   1.142 +    InternalClear();
   1.143 +    vt = VT_I2;
   1.144 +  }
   1.145 +  iVal = value;
   1.146 +  return *this;
   1.147 +}
   1.148 +
   1.149 +/*
   1.150 +CPropVariant& CPropVariant::operator=(LONG value)
   1.151 +{
   1.152 +  if (vt != VT_I4)
   1.153 +  {
   1.154 +    InternalClear();
   1.155 +    vt = VT_I4;
   1.156 +  }
   1.157 +  lVal = value;
   1.158 +  return *this;
   1.159 +}
   1.160 +*/
   1.161 +
   1.162 +static HRESULT MyPropVariantClear(PROPVARIANT *propVariant)
   1.163 +{
   1.164 +  switch(propVariant->vt)
   1.165 +  {
   1.166 +    case VT_UI1:
   1.167 +    case VT_I1:
   1.168 +    case VT_I2:
   1.169 +    case VT_UI2:
   1.170 +    case VT_BOOL:
   1.171 +    case VT_I4:
   1.172 +    case VT_UI4:
   1.173 +    case VT_R4:
   1.174 +    case VT_INT:
   1.175 +    case VT_UINT:
   1.176 +    case VT_ERROR:
   1.177 +    case VT_FILETIME:
   1.178 +    case VT_UI8:
   1.179 +    case VT_R8:
   1.180 +    case VT_CY:
   1.181 +    case VT_DATE:
   1.182 +      propVariant->vt = VT_EMPTY;
   1.183 +      propVariant->wReserved1 = 0;
   1.184 +      return S_OK;
   1.185 +  }
   1.186 +  return ::VariantClear((VARIANTARG *)propVariant);
   1.187 +}
   1.188 +
   1.189 +HRESULT CPropVariant::Clear()
   1.190 +{
   1.191 +  return MyPropVariantClear(this);
   1.192 +}
   1.193 +
   1.194 +HRESULT CPropVariant::Copy(const PROPVARIANT* pSrc)
   1.195 +{
   1.196 +  ::VariantClear((tagVARIANT *)this);
   1.197 +  switch(pSrc->vt)
   1.198 +  {
   1.199 +    case VT_UI1:
   1.200 +    case VT_I1:
   1.201 +    case VT_I2:
   1.202 +    case VT_UI2:
   1.203 +    case VT_BOOL:
   1.204 +    case VT_I4:
   1.205 +    case VT_UI4:
   1.206 +    case VT_R4:
   1.207 +    case VT_INT:
   1.208 +    case VT_UINT:
   1.209 +    case VT_ERROR:
   1.210 +    case VT_FILETIME:
   1.211 +    case VT_UI8:
   1.212 +    case VT_R8:
   1.213 +    case VT_CY:
   1.214 +    case VT_DATE:
   1.215 +      memmove((PROPVARIANT*)this, pSrc, sizeof(PROPVARIANT));
   1.216 +      return S_OK;
   1.217 +  }
   1.218 +  return ::VariantCopy((tagVARIANT *)this, (tagVARIANT *)(pSrc));
   1.219 +}
   1.220 +
   1.221 +
   1.222 +HRESULT CPropVariant::Attach(PROPVARIANT* pSrc)
   1.223 +{
   1.224 +  HRESULT hr = Clear();
   1.225 +  if (FAILED(hr))
   1.226 +    return hr;
   1.227 +  memcpy(this, pSrc, sizeof(PROPVARIANT));
   1.228 +  pSrc->vt = VT_EMPTY;
   1.229 +  return S_OK;
   1.230 +}
   1.231 +
   1.232 +HRESULT CPropVariant::Detach(PROPVARIANT* pDest)
   1.233 +{
   1.234 +  HRESULT hr = MyPropVariantClear(pDest);
   1.235 +  if (FAILED(hr))
   1.236 +    return hr;
   1.237 +  memcpy(pDest, this, sizeof(PROPVARIANT));
   1.238 +  vt = VT_EMPTY;
   1.239 +  return S_OK;
   1.240 +}
   1.241 +
   1.242 +HRESULT CPropVariant::InternalClear()
   1.243 +{
   1.244 +  HRESULT hr = Clear();
   1.245 +  if (FAILED(hr))
   1.246 +  {
   1.247 +    vt = VT_ERROR;
   1.248 +    scode = hr;
   1.249 +  }
   1.250 +  return hr;
   1.251 +}
   1.252 +
   1.253 +void CPropVariant::InternalCopy(const PROPVARIANT* pSrc)
   1.254 +{
   1.255 +  HRESULT hr = Copy(pSrc);
   1.256 +  if (FAILED(hr))
   1.257 +  {
   1.258 +    vt = VT_ERROR;
   1.259 +    scode = hr;
   1.260 +  }
   1.261 +}
   1.262 +
   1.263 +int CPropVariant::Compare(const CPropVariant &a)
   1.264 +{
   1.265 +  if (vt != a.vt)
   1.266 +    return 0; // it's mean some bug
   1.267 +  switch (vt)
   1.268 +  {
   1.269 +    case VT_EMPTY:
   1.270 +      return 0;
   1.271 +    
   1.272 +    /*
   1.273 +    case VT_I1:
   1.274 +      return MyCompare(cVal, a.cVal);
   1.275 +    */
   1.276 +    case VT_UI1:
   1.277 +      return MyCompare(bVal, a.bVal);
   1.278 +
   1.279 +    case VT_I2:
   1.280 +      return MyCompare(iVal, a.iVal);
   1.281 +    case VT_UI2:
   1.282 +      return MyCompare(uiVal, a.uiVal);
   1.283 +    
   1.284 +    case VT_I4:
   1.285 +      return MyCompare(lVal, a.lVal);
   1.286 +    /*
   1.287 +    case VT_INT:
   1.288 +      return MyCompare(intVal, a.intVal);
   1.289 +    */
   1.290 +    case VT_UI4:
   1.291 +      return MyCompare(ulVal, a.ulVal);
   1.292 +    /*
   1.293 +    case VT_UINT:
   1.294 +      return MyCompare(uintVal, a.uintVal);
   1.295 +    */
   1.296 +    case VT_I8:
   1.297 +      return MyCompare(hVal.QuadPart, a.hVal.QuadPart);
   1.298 +    case VT_UI8:
   1.299 +      return MyCompare(uhVal.QuadPart, a.uhVal.QuadPart);
   1.300 +
   1.301 +    case VT_BOOL:
   1.302 +      return -MyCompare(boolVal, a.boolVal);
   1.303 +
   1.304 +    case VT_FILETIME:
   1.305 +      return ::CompareFileTime(&filetime, &a.filetime);
   1.306 +    case VT_BSTR:
   1.307 +      return 0; // Not implemented
   1.308 +      // return MyCompare(aPropVarint.cVal);
   1.309 +
   1.310 +    default:
   1.311 +      return 0;
   1.312 +  }
   1.313 +}
   1.314 +
   1.315 +}}