utils.c 4.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
/*
 * Copyright 2017 Piotr Caban for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "vbscript.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(vbscript);

typedef struct {
    IEnumVARIANT IEnumVARIANT_iface;

    LONG ref;

    SAFEARRAY *sa;
    ULONG i, size;
} safearray_iter;

static inline safearray_iter *impl_from_IEnumVARIANT(IEnumVARIANT *iface)
{
    return CONTAINING_RECORD(iface, safearray_iter, IEnumVARIANT_iface);
}

static HRESULT WINAPI safearray_iter_IEnumVARIANT_QueryInterface(
        IEnumVARIANT *iface, REFIID riid, void **ppv)
{
    safearray_iter *This = impl_from_IEnumVARIANT(iface);

    if(IsEqualGUID(riid, &IID_IUnknown)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
        *ppv = &This->IEnumVARIANT_iface;
    }else if(IsEqualGUID(riid, &IID_IEnumVARIANT)) {
        TRACE("(%p)->(IID_IEnumVARIANT %p)\n", This, ppv);
        *ppv = &This->IEnumVARIANT_iface;
    }else {
        FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
        *ppv = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown*)*ppv);
    return S_OK;
}

static ULONG WINAPI safearray_iter_IEnumVARIANT_AddRef(IEnumVARIANT *iface)
{
    safearray_iter *This = impl_from_IEnumVARIANT(iface);
    LONG ref = InterlockedIncrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

    return ref;
}

static ULONG WINAPI safearray_iter_IEnumVARIANT_Release(IEnumVARIANT *iface)
{
    safearray_iter *This = impl_from_IEnumVARIANT(iface);
    LONG ref = InterlockedDecrement(&This->ref);

    TRACE("(%p) ref=%d\n", iface, ref);

    if(!ref) {
        if(This->sa)
            SafeArrayUnlock(This->sa);
        heap_free(This);
    }

    return ref;
}

static HRESULT WINAPI safearray_iter_IEnumVARIANT_Next(IEnumVARIANT *iface,
        ULONG celt, VARIANT *rgVar, ULONG *pCeltFetched)
{
    safearray_iter *This = impl_from_IEnumVARIANT(iface);
    HRESULT hres;
    VARIANT *v;

    TRACE("(%p)->(%u %p %p)\n", This, celt, rgVar, pCeltFetched);

    if(celt != 1) {
        FIXME("celt != 1\n");
        return E_NOTIMPL;
    }

    if(This->i >= This->size) {
        if(pCeltFetched)
            *pCeltFetched = 0;
        return S_FALSE;
    }

    if(!This->sa->cLocks)
        ERR("SAFEARRAY not locked\n");

    v = (VARIANT*)(((BYTE*)This->sa->pvData) + This->i * This->sa->cbElements);
    V_VT(rgVar) = VT_EMPTY;
    hres = VariantCopy(rgVar, v);
    if(FAILED(hres))
        return hres;

    This->i++;
    if(pCeltFetched)
        *pCeltFetched = 1;
    return S_OK;
}

static HRESULT WINAPI safearray_iter_IEnumVARIANT_Skip(IEnumVARIANT *iface, ULONG celt)
{
    FIXME("\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI safearray_iter_IEnumVARIANT_Reset(IEnumVARIANT *iface)
{
    FIXME("\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI safearray_iter_IEnumVARIANT_Clone(
        IEnumVARIANT *iface, IEnumVARIANT **ppEnum)
{
    FIXME("\n");
    return E_NOTIMPL;
}

static const IEnumVARIANTVtbl safearray_iter_EnumVARIANTVtbl = {
    safearray_iter_IEnumVARIANT_QueryInterface,
    safearray_iter_IEnumVARIANT_AddRef,
    safearray_iter_IEnumVARIANT_Release,
    safearray_iter_IEnumVARIANT_Next,
    safearray_iter_IEnumVARIANT_Skip,
    safearray_iter_IEnumVARIANT_Reset,
    safearray_iter_IEnumVARIANT_Clone
};

static ULONG get_safearray_size(SAFEARRAY *sa)
{
    ULONG ret = 1;
    USHORT i;

    if(!sa)
        return 0;

    for(i=0; i<sa->cDims && ret; i++)
        ret *= sa->rgsabound[i].cElements;
    return ret;
}

HRESULT create_safearray_iter(SAFEARRAY *sa, IEnumVARIANT **ev)
{
    safearray_iter *iter;
    HRESULT hres;

    if(sa && !(sa->fFeatures & FADF_VARIANT)) {
        FIXME("enumeration not supported: %x\n", sa->fFeatures);
        return E_NOTIMPL;
    }

    iter = heap_alloc(sizeof(*iter));
    if(!iter)
        return E_OUTOFMEMORY;

    if(sa) {
        hres = SafeArrayLock(sa);
        if(FAILED(hres)) {
            heap_free(iter);
            return hres;
        }
    }

    iter->IEnumVARIANT_iface.lpVtbl = &safearray_iter_EnumVARIANTVtbl;
    iter->ref = 1;
    iter->sa = sa;
    iter->i = 0;
    iter->size = get_safearray_size(sa);

    *ev = &iter->IEnumVARIANT_iface;
    return S_OK;
}