d3d10core: Implement d3d10_device_VSSetShaderResources().
[wine/multimedia.git] / dlls / vbscript / vbregexp.c
blob0a399b98525ee538a346863eeaae9c8e5cb8ea2c
1 /*
2 * Copyright 2013 Piotr Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "vbscript.h"
20 #include "regexp.h"
21 #include "vbsregexp55.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
27 #define REGEXP_TID_LIST \
28 XDIID(RegExp2), \
29 XDIID(Match2), \
30 XDIID(MatchCollection2), \
31 XDIID(SubMatches)
33 typedef enum {
34 #define XDIID(iface) iface ## _tid
35 REGEXP_TID_LIST,
36 #undef XDIID
37 REGEXP_LAST_tid
38 } regexp_tid_t;
40 static REFIID tid_ids[] = {
41 #define XDIID(iface) &IID_I ## iface
42 REGEXP_TID_LIST
43 #undef XDIID
46 static ITypeLib *typelib;
47 static ITypeInfo *typeinfos[REGEXP_LAST_tid];
49 static HRESULT init_regexp_typeinfo(regexp_tid_t tid)
51 HRESULT hres;
53 if(!typelib) {
54 static const WCHAR vbscript_dll3W[] = {'v','b','s','c','r','i','p','t','.','d','l','l','\\','3',0};
55 ITypeLib *tl;
57 hres = LoadTypeLib(vbscript_dll3W, &tl);
58 if(FAILED(hres)) {
59 ERR("LoadRegTypeLib failed: %08x\n", hres);
60 return hres;
63 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
64 ITypeLib_Release(tl);
67 if(!typeinfos[tid]) {
68 ITypeInfo *ti;
70 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
71 if(FAILED(hres)) {
72 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
73 return hres;
76 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
77 ITypeInfo_Release(ti);
80 return S_OK;
83 struct SubMatches {
84 ISubMatches ISubMatches_iface;
86 LONG ref;
88 WCHAR *match;
89 match_state_t *result;
92 typedef struct Match2 {
93 IMatch2 IMatch2_iface;
94 IMatch IMatch_iface;
96 LONG ref;
98 DWORD index;
99 SubMatches *sub_matches;
100 } Match2;
102 typedef struct MatchCollectionEnum {
103 IEnumVARIANT IEnumVARIANT_iface;
105 LONG ref;
107 IMatchCollection2 *mc;
108 LONG pos;
109 LONG count;
110 } MatchCollectionEnum;
112 typedef struct MatchCollection2 {
113 IMatchCollection2 IMatchCollection2_iface;
114 IMatchCollection IMatchCollection_iface;
116 LONG ref;
118 IMatch2 **matches;
119 DWORD count;
120 DWORD size;
121 } MatchCollection2;
123 typedef struct RegExp2 {
124 IRegExp2 IRegExp2_iface;
125 IRegExp IRegExp_iface;
127 LONG ref;
129 WCHAR *pattern;
130 regexp_t *regexp;
131 heap_pool_t pool;
132 WORD flags;
133 } RegExp2;
135 static inline SubMatches* impl_from_ISubMatches(ISubMatches *iface)
137 return CONTAINING_RECORD(iface, SubMatches, ISubMatches_iface);
140 static HRESULT WINAPI SubMatches_QueryInterface(
141 ISubMatches *iface, REFIID riid, void **ppv)
143 SubMatches *This = impl_from_ISubMatches(iface);
145 if(IsEqualGUID(riid, &IID_IUnknown)) {
146 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
147 *ppv = &This->ISubMatches_iface;
148 }else if(IsEqualGUID(riid, &IID_IDispatch)) {
149 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
150 *ppv = &This->ISubMatches_iface;
151 }else if(IsEqualGUID(riid, &IID_ISubMatches)) {
152 TRACE("(%p)->(IID_ISubMatches %p)\n", This, ppv);
153 *ppv = &This->ISubMatches_iface;
154 }else if(IsEqualGUID(riid, &IID_IDispatchEx)) {
155 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
156 *ppv = NULL;
157 return E_NOINTERFACE;
158 }else {
159 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
160 *ppv = NULL;
161 return E_NOINTERFACE;
164 IUnknown_AddRef((IUnknown*)*ppv);
165 return S_OK;
168 static ULONG WINAPI SubMatches_AddRef(ISubMatches *iface)
170 SubMatches *This = impl_from_ISubMatches(iface);
171 LONG ref = InterlockedIncrement(&This->ref);
173 TRACE("(%p) ref=%d\n", This, ref);
175 return ref;
178 static ULONG WINAPI SubMatches_Release(ISubMatches *iface)
180 SubMatches *This = impl_from_ISubMatches(iface);
181 LONG ref = InterlockedDecrement(&This->ref);
183 TRACE("(%p) ref=%d\n", This, ref);
185 if(!ref) {
186 heap_free(This->match);
187 heap_free(This->result);
188 heap_free(This);
191 return ref;
194 static HRESULT WINAPI SubMatches_GetTypeInfoCount(ISubMatches *iface, UINT *pctinfo)
196 SubMatches *This = impl_from_ISubMatches(iface);
198 TRACE("(%p)->(%p)\n", This, pctinfo);
200 *pctinfo = 1;
201 return S_OK;
204 static HRESULT WINAPI SubMatches_GetTypeInfo(ISubMatches *iface,
205 UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
207 SubMatches *This = impl_from_ISubMatches(iface);
208 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
209 return E_NOTIMPL;
212 static HRESULT WINAPI SubMatches_GetIDsOfNames(ISubMatches *iface,
213 REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
215 SubMatches *This = impl_from_ISubMatches(iface);
217 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid),
218 rgszNames, cNames, lcid, rgDispId);
220 return ITypeInfo_GetIDsOfNames(typeinfos[SubMatches_tid], rgszNames, cNames, rgDispId);
223 static HRESULT WINAPI SubMatches_Invoke(ISubMatches *iface, DISPID dispIdMember,
224 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
225 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
227 SubMatches *This = impl_from_ISubMatches(iface);
229 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
230 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
232 return ITypeInfo_Invoke(typeinfos[SubMatches_tid], iface, dispIdMember, wFlags,
233 pDispParams, pVarResult, pExcepInfo, puArgErr);
236 static HRESULT WINAPI SubMatches_get_Item(ISubMatches *iface,
237 LONG index, VARIANT *pSubMatch)
239 SubMatches *This = impl_from_ISubMatches(iface);
241 TRACE("(%p)->(%d %p)\n", This, index, pSubMatch);
243 if(!pSubMatch)
244 return E_POINTER;
246 if(!This->result || index<0 || index>=This->result->paren_count)
247 return E_INVALIDARG;
249 if(This->result->parens[index].index == -1) {
250 V_VT(pSubMatch) = VT_EMPTY;
251 }else {
252 V_VT(pSubMatch) = VT_BSTR;
253 V_BSTR(pSubMatch) = SysAllocStringLen(
254 This->match+This->result->parens[index].index,
255 This->result->parens[index].length);
257 if(!V_BSTR(pSubMatch))
258 return E_OUTOFMEMORY;
261 return S_OK;
264 static HRESULT WINAPI SubMatches_get_Count(ISubMatches *iface, LONG *pCount)
266 SubMatches *This = impl_from_ISubMatches(iface);
268 TRACE("(%p)->(%p)\n", This, pCount);
270 if(!pCount)
271 return E_POINTER;
273 if(!This->result)
274 *pCount = 0;
275 else
276 *pCount = This->result->paren_count;
277 return S_OK;
280 static HRESULT WINAPI SubMatches_get__NewEnum(ISubMatches *iface, IUnknown **ppEnum)
282 SubMatches *This = impl_from_ISubMatches(iface);
283 FIXME("(%p)->(%p)\n", This, ppEnum);
284 return E_NOTIMPL;
287 static const ISubMatchesVtbl SubMatchesVtbl = {
288 SubMatches_QueryInterface,
289 SubMatches_AddRef,
290 SubMatches_Release,
291 SubMatches_GetTypeInfoCount,
292 SubMatches_GetTypeInfo,
293 SubMatches_GetIDsOfNames,
294 SubMatches_Invoke,
295 SubMatches_get_Item,
296 SubMatches_get_Count,
297 SubMatches_get__NewEnum
300 static HRESULT create_sub_matches(DWORD pos, match_state_t *result, SubMatches **sub_matches)
302 SubMatches *ret;
303 DWORD i;
304 HRESULT hres;
306 hres = init_regexp_typeinfo(SubMatches_tid);
307 if(FAILED(hres))
308 return hres;
310 ret = heap_alloc_zero(sizeof(*ret));
311 if(!ret)
312 return E_OUTOFMEMORY;
314 ret->ISubMatches_iface.lpVtbl = &SubMatchesVtbl;
316 ret->result = result;
317 if(result) {
318 ret->match = heap_alloc((result->match_len+1) * sizeof(WCHAR));
319 if(!ret->match) {
320 heap_free(ret);
321 return E_OUTOFMEMORY;
323 memcpy(ret->match, result->cp-result->match_len, result->match_len*sizeof(WCHAR));
324 ret->match[result->match_len] = 0;
326 result->cp = NULL;
327 for(i=0; i<result->paren_count; i++)
328 if(result->parens[i].index != -1)
329 result->parens[i].index -= pos;
330 }else {
331 ret->match = NULL;
334 ret->ref = 1;
335 *sub_matches = ret;
336 return hres;
339 static inline Match2* impl_from_IMatch2(IMatch2 *iface)
341 return CONTAINING_RECORD(iface, Match2, IMatch2_iface);
344 static HRESULT WINAPI Match2_QueryInterface(
345 IMatch2 *iface, REFIID riid, void **ppv)
347 Match2 *This = impl_from_IMatch2(iface);
349 if(IsEqualGUID(riid, &IID_IUnknown)) {
350 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
351 *ppv = &This->IMatch2_iface;
352 }else if(IsEqualGUID(riid, &IID_IDispatch)) {
353 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
354 *ppv = &This->IMatch2_iface;
355 }else if(IsEqualGUID(riid, &IID_IMatch2)) {
356 TRACE("(%p)->(IID_IMatch2 %p)\n", This, ppv);
357 *ppv = &This->IMatch2_iface;
358 }else if(IsEqualGUID(riid, &IID_IMatch)) {
359 TRACE("(%p)->(IID_IMatch %p)\n", This, ppv);
360 *ppv = &This->IMatch_iface;
361 }else if(IsEqualGUID(riid, &IID_IDispatchEx)) {
362 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
363 *ppv = NULL;
364 return E_NOINTERFACE;
365 }else {
366 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
367 *ppv = NULL;
368 return E_NOINTERFACE;
371 IUnknown_AddRef((IUnknown*)*ppv);
372 return S_OK;
375 static ULONG WINAPI Match2_AddRef(IMatch2 *iface)
377 Match2 *This = impl_from_IMatch2(iface);
378 LONG ref = InterlockedIncrement(&This->ref);
380 TRACE("(%p) ref=%d\n", This, ref);
382 return ref;
385 static ULONG WINAPI Match2_Release(IMatch2 *iface)
387 Match2 *This = impl_from_IMatch2(iface);
388 LONG ref = InterlockedDecrement(&This->ref);
390 TRACE("(%p) ref=%d\n", This, ref);
392 if(!ref) {
393 ISubMatches_Release(&This->sub_matches->ISubMatches_iface);
394 heap_free(This);
397 return ref;
400 static HRESULT WINAPI Match2_GetTypeInfoCount(IMatch2 *iface, UINT *pctinfo)
402 Match2 *This = impl_from_IMatch2(iface);
404 TRACE("(%p)->(%p)\n", This, pctinfo);
406 *pctinfo = 1;
407 return S_OK;
410 static HRESULT WINAPI Match2_GetTypeInfo(IMatch2 *iface,
411 UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
413 Match2 *This = impl_from_IMatch2(iface);
414 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
415 return E_NOTIMPL;
418 static HRESULT WINAPI Match2_GetIDsOfNames(IMatch2 *iface,
419 REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
421 Match2 *This = impl_from_IMatch2(iface);
423 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid),
424 rgszNames, cNames, lcid, rgDispId);
426 return ITypeInfo_GetIDsOfNames(typeinfos[Match2_tid], rgszNames, cNames, rgDispId);
429 static HRESULT WINAPI Match2_Invoke(IMatch2 *iface, DISPID dispIdMember,
430 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
431 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
433 Match2 *This = impl_from_IMatch2(iface);
435 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
436 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
438 return ITypeInfo_Invoke(typeinfos[Match2_tid], iface, dispIdMember, wFlags,
439 pDispParams, pVarResult, pExcepInfo, puArgErr);
442 static HRESULT WINAPI Match2_get_Value(IMatch2 *iface, BSTR *pValue)
444 Match2 *This = impl_from_IMatch2(iface);
446 TRACE("(%p)->(%p)\n", This, pValue);
448 if(!pValue)
449 return E_POINTER;
451 if(!This->sub_matches->match) {
452 *pValue = NULL;
453 return S_OK;
456 *pValue = SysAllocString(This->sub_matches->match);
457 return *pValue ? S_OK : E_OUTOFMEMORY;
460 static HRESULT WINAPI Match2_get_FirstIndex(IMatch2 *iface, LONG *pFirstIndex)
462 Match2 *This = impl_from_IMatch2(iface);
464 TRACE("(%p)->(%p)\n", This, pFirstIndex);
466 if(!pFirstIndex)
467 return E_POINTER;
469 *pFirstIndex = This->index;
470 return S_OK;
473 static HRESULT WINAPI Match2_get_Length(IMatch2 *iface, LONG *pLength)
475 Match2 *This = impl_from_IMatch2(iface);
477 TRACE("(%p)->(%p)\n", This, pLength);
479 if(!pLength)
480 return E_POINTER;
482 if(This->sub_matches->result)
483 *pLength = This->sub_matches->result->match_len;
484 else
485 *pLength = 0;
486 return S_OK;
489 static HRESULT WINAPI Match2_get_SubMatches(IMatch2 *iface, IDispatch **ppSubMatches)
491 Match2 *This = impl_from_IMatch2(iface);
493 TRACE("(%p)->(%p)\n", This, ppSubMatches);
495 if(!ppSubMatches)
496 return E_POINTER;
498 *ppSubMatches = (IDispatch*)&This->sub_matches->ISubMatches_iface;
499 ISubMatches_AddRef(&This->sub_matches->ISubMatches_iface);
500 return S_OK;
503 static const IMatch2Vtbl Match2Vtbl = {
504 Match2_QueryInterface,
505 Match2_AddRef,
506 Match2_Release,
507 Match2_GetTypeInfoCount,
508 Match2_GetTypeInfo,
509 Match2_GetIDsOfNames,
510 Match2_Invoke,
511 Match2_get_Value,
512 Match2_get_FirstIndex,
513 Match2_get_Length,
514 Match2_get_SubMatches
517 static inline Match2 *impl_from_IMatch(IMatch *iface)
519 return CONTAINING_RECORD(iface, Match2, IMatch_iface);
522 static HRESULT WINAPI Match_QueryInterface(IMatch *iface, REFIID riid, void **ppv)
524 Match2 *This = impl_from_IMatch(iface);
525 return IMatch2_QueryInterface(&This->IMatch2_iface, riid, ppv);
528 static ULONG WINAPI Match_AddRef(IMatch *iface)
530 Match2 *This = impl_from_IMatch(iface);
531 return IMatch2_AddRef(&This->IMatch2_iface);
534 static ULONG WINAPI Match_Release(IMatch *iface)
536 Match2 *This = impl_from_IMatch(iface);
537 return IMatch2_Release(&This->IMatch2_iface);
540 static HRESULT WINAPI Match_GetTypeInfoCount(IMatch *iface, UINT *pctinfo)
542 Match2 *This = impl_from_IMatch(iface);
543 return IMatch2_GetTypeInfoCount(&This->IMatch2_iface, pctinfo);
546 static HRESULT WINAPI Match_GetTypeInfo(IMatch *iface, UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
548 Match2 *This = impl_from_IMatch(iface);
549 return IMatch2_GetTypeInfo(&This->IMatch2_iface, iTInfo, lcid, ppTInfo);
552 static HRESULT WINAPI Match_GetIDsOfNames(IMatch *iface, REFIID riid,
553 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
555 Match2 *This = impl_from_IMatch(iface);
556 return IMatch2_GetIDsOfNames(&This->IMatch2_iface, riid, rgszNames, cNames, lcid, rgDispId);
559 static HRESULT WINAPI Match_Invoke(IMatch *iface, DISPID dispIdMember,
560 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
561 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
563 Match2 *This = impl_from_IMatch(iface);
564 return IMatch2_Invoke(&This->IMatch2_iface, dispIdMember, riid, lcid,
565 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
568 static HRESULT WINAPI Match_get_Value(IMatch *iface, BSTR *pValue)
570 Match2 *This = impl_from_IMatch(iface);
571 return IMatch2_get_Value(&This->IMatch2_iface, pValue);
574 static HRESULT WINAPI Match_get_FirstIndex(IMatch *iface, LONG *pFirstIndex)
576 Match2 *This = impl_from_IMatch(iface);
577 return IMatch2_get_FirstIndex(&This->IMatch2_iface, pFirstIndex);
580 static HRESULT WINAPI Match_get_Length(IMatch *iface, LONG *pLength)
582 Match2 *This = impl_from_IMatch(iface);
583 return IMatch2_get_Length(&This->IMatch2_iface, pLength);
586 static IMatchVtbl MatchVtbl = {
587 Match_QueryInterface,
588 Match_AddRef,
589 Match_Release,
590 Match_GetTypeInfoCount,
591 Match_GetTypeInfo,
592 Match_GetIDsOfNames,
593 Match_Invoke,
594 Match_get_Value,
595 Match_get_FirstIndex,
596 Match_get_Length
599 static HRESULT create_match2(DWORD pos, match_state_t **result, IMatch2 **match)
601 Match2 *ret;
602 HRESULT hres;
604 hres = init_regexp_typeinfo(Match2_tid);
605 if(FAILED(hres))
606 return hres;
608 ret = heap_alloc_zero(sizeof(*ret));
609 if(!ret)
610 return E_OUTOFMEMORY;
612 ret->index = pos;
613 hres = create_sub_matches(pos, result ? *result : NULL, &ret->sub_matches);
614 if(FAILED(hres)) {
615 heap_free(ret);
616 return hres;
618 if(result)
619 *result = NULL;
621 ret->IMatch2_iface.lpVtbl = &Match2Vtbl;
622 ret->IMatch_iface.lpVtbl = &MatchVtbl;
624 ret->ref = 1;
625 *match = &ret->IMatch2_iface;
626 return hres;
629 static inline MatchCollectionEnum* impl_from_IMatchCollectionEnum(IEnumVARIANT *iface)
631 return CONTAINING_RECORD(iface, MatchCollectionEnum, IEnumVARIANT_iface);
634 static HRESULT WINAPI MatchCollectionEnum_QueryInterface(
635 IEnumVARIANT *iface, REFIID riid, void **ppv)
637 MatchCollectionEnum *This = impl_from_IMatchCollectionEnum(iface);
639 if(IsEqualGUID(riid, &IID_IUnknown)) {
640 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
641 *ppv = &This->IEnumVARIANT_iface;
642 }else if(IsEqualGUID(riid, &IID_IEnumVARIANT)) {
643 TRACE("(%p)->(IID_IEnumVARIANT %p)\n", This, ppv);
644 *ppv = &This->IEnumVARIANT_iface;
645 }else {
646 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
647 *ppv = NULL;
648 return E_NOINTERFACE;
651 IUnknown_AddRef((IUnknown*)*ppv);
652 return S_OK;
655 static ULONG WINAPI MatchCollectionEnum_AddRef(IEnumVARIANT *iface)
657 MatchCollectionEnum *This = impl_from_IMatchCollectionEnum(iface);
658 LONG ref = InterlockedIncrement(&This->ref);
660 TRACE("(%p) ref=%d\n", This, ref);
662 return ref;
665 static ULONG WINAPI MatchCollectionEnum_Release(IEnumVARIANT *iface)
667 MatchCollectionEnum *This = impl_from_IMatchCollectionEnum(iface);
668 LONG ref = InterlockedDecrement(&This->ref);
670 TRACE("(%p) ref=%d\n", This, ref);
672 if(!ref) {
673 IMatchCollection2_Release(This->mc);
674 heap_free(This);
677 return ref;
680 static HRESULT WINAPI MatchCollectionEnum_Next(IEnumVARIANT *iface,
681 ULONG celt, VARIANT *rgVar, ULONG *pCeltFetched)
683 MatchCollectionEnum *This = impl_from_IMatchCollectionEnum(iface);
684 DWORD i;
685 HRESULT hres = S_OK;
687 TRACE("(%p)->(%u %p %p)\n", This, celt, rgVar, pCeltFetched);
689 if(This->pos>=This->count) {
690 if(pCeltFetched)
691 *pCeltFetched = 0;
692 return S_FALSE;
695 for(i=0; i<celt && This->pos+i<This->count; i++) {
696 V_VT(rgVar+i) = VT_DISPATCH;
697 hres = IMatchCollection2_get_Item(This->mc, This->pos+i, &V_DISPATCH(rgVar+i));
698 if(FAILED(hres))
699 break;
701 if(FAILED(hres)) {
702 while(i--)
703 VariantClear(rgVar+i);
704 return hres;
707 if(pCeltFetched)
708 *pCeltFetched = i;
709 This->pos += i;
710 return S_OK;
713 static HRESULT WINAPI MatchCollectionEnum_Skip(IEnumVARIANT *iface, ULONG celt)
715 MatchCollectionEnum *This = impl_from_IMatchCollectionEnum(iface);
717 TRACE("(%p)->(%u)\n", This, celt);
719 if(This->pos+celt <= This->count)
720 This->pos += celt;
721 else
722 This->pos = This->count;
723 return S_OK;
726 static HRESULT WINAPI MatchCollectionEnum_Reset(IEnumVARIANT *iface)
728 MatchCollectionEnum *This = impl_from_IMatchCollectionEnum(iface);
730 TRACE("(%p)\n", This);
732 This->pos = 0;
733 return S_OK;
736 static HRESULT WINAPI MatchCollectionEnum_Clone(IEnumVARIANT *iface, IEnumVARIANT **ppEnum)
738 MatchCollectionEnum *This = impl_from_IMatchCollectionEnum(iface);
739 FIXME("(%p)->(%p)\n", This, ppEnum);
740 return E_NOTIMPL;
743 static const IEnumVARIANTVtbl MatchCollectionEnum_Vtbl = {
744 MatchCollectionEnum_QueryInterface,
745 MatchCollectionEnum_AddRef,
746 MatchCollectionEnum_Release,
747 MatchCollectionEnum_Next,
748 MatchCollectionEnum_Skip,
749 MatchCollectionEnum_Reset,
750 MatchCollectionEnum_Clone
753 static HRESULT create_enum_variant_mc2(IMatchCollection2 *mc, ULONG pos, IEnumVARIANT **enum_variant)
755 MatchCollectionEnum *ret;
757 ret = heap_alloc_zero(sizeof(*ret));
758 if(!ret)
759 return E_OUTOFMEMORY;
761 ret->IEnumVARIANT_iface.lpVtbl = &MatchCollectionEnum_Vtbl;
762 ret->ref = 1;
763 ret->pos = pos;
764 IMatchCollection2_get_Count(mc, &ret->count);
765 ret->mc = mc;
766 IMatchCollection2_AddRef(mc);
768 *enum_variant = &ret->IEnumVARIANT_iface;
769 return S_OK;
772 static inline MatchCollection2* impl_from_IMatchCollection2(IMatchCollection2 *iface)
774 return CONTAINING_RECORD(iface, MatchCollection2, IMatchCollection2_iface);
777 static HRESULT WINAPI MatchCollection2_QueryInterface(
778 IMatchCollection2 *iface, REFIID riid, void **ppv)
780 MatchCollection2 *This = impl_from_IMatchCollection2(iface);
782 if(IsEqualGUID(riid, &IID_IUnknown)) {
783 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
784 *ppv = &This->IMatchCollection2_iface;
785 }else if(IsEqualGUID(riid, &IID_IDispatch)) {
786 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
787 *ppv = &This->IMatchCollection2_iface;
788 }else if(IsEqualGUID(riid, &IID_IMatchCollection2)) {
789 TRACE("(%p)->(IID_IMatchCollection2 %p)\n", This, ppv);
790 *ppv = &This->IMatchCollection2_iface;
791 }else if(IsEqualGUID(riid, &IID_IMatchCollection)) {
792 TRACE("(%p)->(IID_IMatchCollection %p)\n", This, ppv);
793 *ppv = &This->IMatchCollection_iface;
794 }else if(IsEqualGUID(riid, &IID_IDispatchEx)) {
795 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
796 *ppv = NULL;
797 return E_NOINTERFACE;
798 }else {
799 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
800 *ppv = NULL;
801 return E_NOINTERFACE;
804 IUnknown_AddRef((IUnknown*)*ppv);
805 return S_OK;
808 static ULONG WINAPI MatchCollection2_AddRef(IMatchCollection2 *iface)
810 MatchCollection2 *This = impl_from_IMatchCollection2(iface);
811 LONG ref = InterlockedIncrement(&This->ref);
813 TRACE("(%p) ref=%d\n", This, ref);
815 return ref;
818 static ULONG WINAPI MatchCollection2_Release(IMatchCollection2 *iface)
820 MatchCollection2 *This = impl_from_IMatchCollection2(iface);
821 LONG ref = InterlockedDecrement(&This->ref);
823 TRACE("(%p) ref=%d\n", This, ref);
825 if(!ref) {
826 DWORD i;
828 for(i=0; i<This->count; i++)
829 IMatch2_Release(This->matches[i]);
830 heap_free(This->matches);
832 heap_free(This);
835 return ref;
838 static HRESULT WINAPI MatchCollection2_GetTypeInfoCount(IMatchCollection2 *iface, UINT *pctinfo)
840 MatchCollection2 *This = impl_from_IMatchCollection2(iface);
842 TRACE("(%p)->(%p)\n", This, pctinfo);
844 *pctinfo = 1;
845 return S_OK;
848 static HRESULT WINAPI MatchCollection2_GetTypeInfo(IMatchCollection2 *iface,
849 UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
851 MatchCollection2 *This = impl_from_IMatchCollection2(iface);
852 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
853 return E_NOTIMPL;
856 static HRESULT WINAPI MatchCollection2_GetIDsOfNames(IMatchCollection2 *iface,
857 REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
859 MatchCollection2 *This = impl_from_IMatchCollection2(iface);
861 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid),
862 rgszNames, cNames, lcid, rgDispId);
864 return ITypeInfo_GetIDsOfNames(typeinfos[MatchCollection2_tid], rgszNames, cNames, rgDispId);
867 static HRESULT WINAPI MatchCollection2_Invoke(IMatchCollection2 *iface, DISPID dispIdMember,
868 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
869 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
871 MatchCollection2 *This = impl_from_IMatchCollection2(iface);
873 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
874 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
876 return ITypeInfo_Invoke(typeinfos[MatchCollection2_tid], iface, dispIdMember, wFlags,
877 pDispParams, pVarResult, pExcepInfo, puArgErr);
880 static HRESULT WINAPI MatchCollection2_get_Item(IMatchCollection2 *iface,
881 LONG index, IDispatch **ppMatch)
883 MatchCollection2 *This = impl_from_IMatchCollection2(iface);
885 TRACE("(%p)->()\n", This);
887 if(!ppMatch)
888 return E_POINTER;
890 if(index<0 || index>=This->count)
891 return E_INVALIDARG;
893 *ppMatch = (IDispatch*)This->matches[index];
894 IMatch2_AddRef(This->matches[index]);
895 return S_OK;
898 static HRESULT WINAPI MatchCollection2_get_Count(IMatchCollection2 *iface, LONG *pCount)
900 MatchCollection2 *This = impl_from_IMatchCollection2(iface);
902 TRACE("(%p)->()\n", This);
904 if(!pCount)
905 return E_POINTER;
907 *pCount = This->count;
908 return S_OK;
911 static HRESULT WINAPI MatchCollection2_get__NewEnum(IMatchCollection2 *iface, IUnknown **ppEnum)
913 MatchCollection2 *This = impl_from_IMatchCollection2(iface);
915 TRACE("(%p)->(%p)\n", This, ppEnum);
917 if(!ppEnum)
918 return E_POINTER;
920 return create_enum_variant_mc2(&This->IMatchCollection2_iface, 0, (IEnumVARIANT**)ppEnum);
923 static const IMatchCollection2Vtbl MatchCollection2Vtbl = {
924 MatchCollection2_QueryInterface,
925 MatchCollection2_AddRef,
926 MatchCollection2_Release,
927 MatchCollection2_GetTypeInfoCount,
928 MatchCollection2_GetTypeInfo,
929 MatchCollection2_GetIDsOfNames,
930 MatchCollection2_Invoke,
931 MatchCollection2_get_Item,
932 MatchCollection2_get_Count,
933 MatchCollection2_get__NewEnum
936 static inline MatchCollection2 *impl_from_IMatchCollection(IMatchCollection *iface)
938 return CONTAINING_RECORD(iface, MatchCollection2, IMatchCollection_iface);
941 static HRESULT WINAPI MatchCollection_QueryInterface(IMatchCollection *iface, REFIID riid, void **ppv)
943 MatchCollection2 *This = impl_from_IMatchCollection(iface);
944 return IMatchCollection2_QueryInterface(&This->IMatchCollection2_iface, riid, ppv);
947 static ULONG WINAPI MatchCollection_AddRef(IMatchCollection *iface)
949 MatchCollection2 *This = impl_from_IMatchCollection(iface);
950 return IMatchCollection2_AddRef(&This->IMatchCollection2_iface);
953 static ULONG WINAPI MatchCollection_Release(IMatchCollection *iface)
955 MatchCollection2 *This = impl_from_IMatchCollection(iface);
956 return IMatchCollection2_Release(&This->IMatchCollection2_iface);
959 static HRESULT WINAPI MatchCollection_GetTypeInfoCount(IMatchCollection *iface, UINT *pctinfo)
961 MatchCollection2 *This = impl_from_IMatchCollection(iface);
962 return IMatchCollection2_GetTypeInfoCount(&This->IMatchCollection2_iface, pctinfo);
965 static HRESULT WINAPI MatchCollection_GetTypeInfo(IMatchCollection *iface,
966 UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
968 MatchCollection2 *This = impl_from_IMatchCollection(iface);
969 return IMatchCollection2_GetTypeInfo(&This->IMatchCollection2_iface, iTInfo, lcid, ppTInfo);
972 static HRESULT WINAPI MatchCollection_GetIDsOfNames(IMatchCollection *iface, REFIID riid,
973 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
975 MatchCollection2 *This = impl_from_IMatchCollection(iface);
976 return IMatchCollection2_GetIDsOfNames(&This->IMatchCollection2_iface,
977 riid, rgszNames, cNames, lcid, rgDispId);
980 static HRESULT WINAPI MatchCollection_Invoke(IMatchCollection *iface, DISPID dispIdMember,
981 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
982 EXCEPINFO *pExcepInfo, UINT *puArgErr)
984 MatchCollection2 *This = impl_from_IMatchCollection(iface);
985 return IMatchCollection2_Invoke(&This->IMatchCollection2_iface, dispIdMember,
986 riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
989 static HRESULT WINAPI MatchCollection_get_Item(IMatchCollection *iface, LONG index, IDispatch **ppMatch)
991 MatchCollection2 *This = impl_from_IMatchCollection(iface);
992 return IMatchCollection2_get_Item(&This->IMatchCollection2_iface, index, ppMatch);
995 static HRESULT WINAPI MatchCollection_get_Count(IMatchCollection *iface, LONG *pCount)
997 MatchCollection2 *This = impl_from_IMatchCollection(iface);
998 return IMatchCollection2_get_Count(&This->IMatchCollection2_iface, pCount);
1001 static HRESULT WINAPI MatchCollection_get__NewEnum(IMatchCollection *iface, IUnknown **ppEnum)
1003 MatchCollection2 *This = impl_from_IMatchCollection(iface);
1004 return IMatchCollection2_get__NewEnum(&This->IMatchCollection2_iface, ppEnum);
1007 static const IMatchCollectionVtbl MatchCollectionVtbl = {
1008 MatchCollection_QueryInterface,
1009 MatchCollection_AddRef,
1010 MatchCollection_Release,
1011 MatchCollection_GetTypeInfoCount,
1012 MatchCollection_GetTypeInfo,
1013 MatchCollection_GetIDsOfNames,
1014 MatchCollection_Invoke,
1015 MatchCollection_get_Item,
1016 MatchCollection_get_Count,
1017 MatchCollection_get__NewEnum
1020 static HRESULT add_match(IMatchCollection2 *iface, IMatch2 *add)
1022 MatchCollection2 *This = impl_from_IMatchCollection2(iface);
1024 TRACE("(%p)->(%p)\n", This, add);
1026 if(!This->size) {
1027 This->matches = heap_alloc(8*sizeof(IMatch*));
1028 if(!This->matches)
1029 return E_OUTOFMEMORY;
1030 This->size = 8;
1031 }else if(This->size == This->count) {
1032 IMatch2 **new_matches = heap_realloc(This->matches, 2*This->size*sizeof(IMatch*));
1033 if(!new_matches)
1034 return E_OUTOFMEMORY;
1036 This->matches = new_matches;
1037 This->size <<= 1;
1040 This->matches[This->count++] = add;
1041 IMatch2_AddRef(add);
1042 return S_OK;
1045 static HRESULT create_match_collection2(IMatchCollection2 **match_collection)
1047 MatchCollection2 *ret;
1048 HRESULT hres;
1050 hres = init_regexp_typeinfo(MatchCollection2_tid);
1051 if(FAILED(hres))
1052 return hres;
1054 ret = heap_alloc_zero(sizeof(*ret));
1055 if(!ret)
1056 return E_OUTOFMEMORY;
1058 ret->IMatchCollection2_iface.lpVtbl = &MatchCollection2Vtbl;
1059 ret->IMatchCollection_iface.lpVtbl = &MatchCollectionVtbl;
1061 ret->ref = 1;
1062 *match_collection = &ret->IMatchCollection2_iface;
1063 return S_OK;
1066 static inline RegExp2 *impl_from_IRegExp2(IRegExp2 *iface)
1068 return CONTAINING_RECORD(iface, RegExp2, IRegExp2_iface);
1071 static HRESULT WINAPI RegExp2_QueryInterface(IRegExp2 *iface, REFIID riid, void **ppv)
1073 RegExp2 *This = impl_from_IRegExp2(iface);
1075 if(IsEqualGUID(riid, &IID_IUnknown)) {
1076 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
1077 *ppv = &This->IRegExp2_iface;
1078 }else if(IsEqualGUID(riid, &IID_IDispatch)) {
1079 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1080 *ppv = &This->IRegExp2_iface;
1081 }else if(IsEqualGUID(riid, &IID_IRegExp2)) {
1082 TRACE("(%p)->(IID_IRegExp2 %p)\n", This, ppv);
1083 *ppv = &This->IRegExp2_iface;
1084 }else if(IsEqualGUID(riid, &IID_IRegExp)) {
1085 TRACE("(%p)->(IID_IRegExp %p)\n", This, ppv);
1086 *ppv = &This->IRegExp_iface;
1087 }else if(IsEqualGUID(riid, &IID_IDispatchEx)) {
1088 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1089 *ppv = NULL;
1090 return E_NOINTERFACE;
1091 }else {
1092 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
1093 *ppv = NULL;
1094 return E_NOINTERFACE;
1097 IUnknown_AddRef((IUnknown*)*ppv);
1098 return S_OK;
1101 static ULONG WINAPI RegExp2_AddRef(IRegExp2 *iface)
1103 RegExp2 *This = impl_from_IRegExp2(iface);
1104 LONG ref = InterlockedIncrement(&This->ref);
1106 TRACE("(%p) ref=%d\n", This, ref);
1108 return ref;
1111 static ULONG WINAPI RegExp2_Release(IRegExp2 *iface)
1113 RegExp2 *This = impl_from_IRegExp2(iface);
1114 LONG ref = InterlockedDecrement(&This->ref);
1116 TRACE("(%p) ref=%d\n", This, ref);
1118 if(!ref) {
1119 heap_free(This->pattern);
1120 if(This->regexp)
1121 regexp_destroy(This->regexp);
1122 heap_pool_free(&This->pool);
1123 heap_free(This);
1126 return ref;
1129 static HRESULT WINAPI RegExp2_GetTypeInfoCount(IRegExp2 *iface, UINT *pctinfo)
1131 RegExp2 *This = impl_from_IRegExp2(iface);
1133 TRACE("(%p)->(%p)\n", This, pctinfo);
1135 *pctinfo = 1;
1136 return S_OK;
1139 static HRESULT WINAPI RegExp2_GetTypeInfo(IRegExp2 *iface,
1140 UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
1142 RegExp2 *This = impl_from_IRegExp2(iface);
1143 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1144 return E_NOTIMPL;
1147 static HRESULT WINAPI RegExp2_GetIDsOfNames(IRegExp2 *iface, REFIID riid,
1148 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1150 RegExp2 *This = impl_from_IRegExp2(iface);
1152 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid),
1153 rgszNames, cNames, lcid, rgDispId);
1155 return ITypeInfo_GetIDsOfNames(typeinfos[RegExp2_tid], rgszNames, cNames, rgDispId);
1158 static HRESULT WINAPI RegExp2_Invoke(IRegExp2 *iface, DISPID dispIdMember,
1159 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1160 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1162 RegExp2 *This = impl_from_IRegExp2(iface);
1164 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1165 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1167 return ITypeInfo_Invoke(typeinfos[RegExp2_tid], iface, dispIdMember, wFlags,
1168 pDispParams, pVarResult, pExcepInfo, puArgErr);
1171 static HRESULT WINAPI RegExp2_get_Pattern(IRegExp2 *iface, BSTR *pPattern)
1173 RegExp2 *This = impl_from_IRegExp2(iface);
1175 TRACE("(%p)->(%p)\n", This, pPattern);
1177 if(!pPattern)
1178 return E_POINTER;
1180 if(!This->pattern) {
1181 *pPattern = NULL;
1182 return S_OK;
1185 *pPattern = SysAllocString(This->pattern);
1186 return *pPattern ? S_OK : E_OUTOFMEMORY;
1189 static HRESULT WINAPI RegExp2_put_Pattern(IRegExp2 *iface, BSTR pattern)
1191 RegExp2 *This = impl_from_IRegExp2(iface);
1192 WCHAR *p;
1193 DWORD size;
1195 TRACE("(%p)->(%s)\n", This, wine_dbgstr_w(pattern));
1197 if(!pattern) {
1198 heap_free(This->pattern);
1199 if(This->regexp) {
1200 regexp_destroy(This->regexp);
1201 This->regexp = NULL;
1203 This->pattern = NULL;
1204 return S_OK;
1207 size = (SysStringLen(pattern)+1) * sizeof(WCHAR);
1208 p = heap_alloc(size);
1209 if(!p)
1210 return E_OUTOFMEMORY;
1212 heap_free(This->pattern);
1213 This->pattern = p;
1214 memcpy(p, pattern, size);
1215 if(This->regexp) {
1216 regexp_destroy(This->regexp);
1217 This->regexp = NULL;
1219 return S_OK;
1222 static HRESULT WINAPI RegExp2_get_IgnoreCase(IRegExp2 *iface, VARIANT_BOOL *pIgnoreCase)
1224 RegExp2 *This = impl_from_IRegExp2(iface);
1226 TRACE("(%p)->(%p)\n", This, pIgnoreCase);
1228 if(!pIgnoreCase)
1229 return E_POINTER;
1231 *pIgnoreCase = This->flags & REG_FOLD ? VARIANT_TRUE : VARIANT_FALSE;
1232 return S_OK;
1235 static HRESULT WINAPI RegExp2_put_IgnoreCase(IRegExp2 *iface, VARIANT_BOOL ignoreCase)
1237 RegExp2 *This = impl_from_IRegExp2(iface);
1239 TRACE("(%p)->(%s)\n", This, ignoreCase ? "true" : "false");
1241 if(ignoreCase)
1242 This->flags |= REG_FOLD;
1243 else
1244 This->flags &= ~REG_FOLD;
1245 return S_OK;
1248 static HRESULT WINAPI RegExp2_get_Global(IRegExp2 *iface, VARIANT_BOOL *pGlobal)
1250 RegExp2 *This = impl_from_IRegExp2(iface);
1252 TRACE("(%p)->(%p)\n", This, pGlobal);
1254 if(!pGlobal)
1255 return E_POINTER;
1257 *pGlobal = This->flags & REG_GLOB ? VARIANT_TRUE : VARIANT_FALSE;
1258 return S_OK;
1261 static HRESULT WINAPI RegExp2_put_Global(IRegExp2 *iface, VARIANT_BOOL global)
1263 RegExp2 *This = impl_from_IRegExp2(iface);
1265 TRACE("(%p)->(%s)\n", This, global ? "true" : "false");
1267 if(global)
1268 This->flags |= REG_GLOB;
1269 else
1270 This->flags &= ~REG_GLOB;
1271 return S_OK;
1274 static HRESULT WINAPI RegExp2_get_Multiline(IRegExp2 *iface, VARIANT_BOOL *pMultiline)
1276 RegExp2 *This = impl_from_IRegExp2(iface);
1278 TRACE("(%p)->(%p)\n", This, pMultiline);
1280 if(!pMultiline)
1281 return E_POINTER;
1283 *pMultiline = This->flags & REG_MULTILINE ? VARIANT_TRUE : VARIANT_FALSE;
1284 return S_OK;
1287 static HRESULT WINAPI RegExp2_put_Multiline(IRegExp2 *iface, VARIANT_BOOL multiline)
1289 RegExp2 *This = impl_from_IRegExp2(iface);
1291 TRACE("(%p)->(%s)\n", This, multiline ? "true" : "false");
1293 if(multiline)
1294 This->flags |= REG_MULTILINE;
1295 else
1296 This->flags &= ~REG_MULTILINE;
1297 return S_OK;
1300 static HRESULT WINAPI RegExp2_Execute(IRegExp2 *iface,
1301 BSTR sourceString, IDispatch **ppMatches)
1303 RegExp2 *This = impl_from_IRegExp2(iface);
1304 match_state_t *result;
1305 const WCHAR *pos;
1306 IMatchCollection2 *match_collection;
1307 IMatch2 *add = NULL;
1308 HRESULT hres;
1310 TRACE("(%p)->(%s %p)\n", This, debugstr_w(sourceString), ppMatches);
1312 if(!This->pattern) {
1313 DWORD i, len = SysStringLen(sourceString);
1315 hres = create_match_collection2(&match_collection);
1316 if(FAILED(hres))
1317 return hres;
1319 for(i=0; i<=len; i++) {
1320 hres = create_match2(i, NULL, &add);
1321 if(FAILED(hres))
1322 break;
1324 hres = add_match(match_collection, add);
1325 if(FAILED(hres))
1326 break;
1327 IMatch2_Release(add);
1329 if(!(This->flags & REG_GLOB))
1330 break;
1333 if(FAILED(hres)) {
1334 IMatchCollection2_Release(match_collection);
1335 return hres;
1338 *ppMatches = (IDispatch*)match_collection;
1339 return S_OK;
1342 if(!This->regexp) {
1343 This->regexp = regexp_new(NULL, &This->pool, This->pattern,
1344 strlenW(This->pattern), This->flags, FALSE);
1345 if(!This->regexp)
1346 return E_FAIL;
1347 }else {
1348 hres = regexp_set_flags(&This->regexp, NULL, &This->pool, This->flags);
1349 if(FAILED(hres))
1350 return hres;
1353 hres = create_match_collection2(&match_collection);
1354 if(FAILED(hres))
1355 return hres;
1357 pos = sourceString;
1358 while(1) {
1359 result = alloc_match_state(This->regexp, NULL, pos);
1360 if(!result) {
1361 hres = E_OUTOFMEMORY;
1362 break;
1365 hres = regexp_execute(This->regexp, NULL, &This->pool,
1366 sourceString, SysStringLen(sourceString), result);
1367 if(hres != S_OK) {
1368 heap_free(result);
1369 break;
1371 pos = result->cp;
1373 hres = create_match2(result->cp-result->match_len-sourceString, &result, &add);
1374 heap_free(result);
1375 if(FAILED(hres))
1376 break;
1377 hres = add_match(match_collection, add);
1378 IMatch2_Release(add);
1379 if(FAILED(hres))
1380 break;
1382 if(!(This->flags & REG_GLOB))
1383 break;
1386 if(FAILED(hres)) {
1387 IMatchCollection2_Release(match_collection);
1388 return hres;
1391 *ppMatches = (IDispatch*)match_collection;
1392 return S_OK;
1395 static HRESULT WINAPI RegExp2_Test(IRegExp2 *iface, BSTR sourceString, VARIANT_BOOL *pMatch)
1397 RegExp2 *This = impl_from_IRegExp2(iface);
1398 match_state_t *result;
1399 heap_pool_t *mark;
1400 HRESULT hres;
1402 TRACE("(%p)->(%s %p)\n", This, debugstr_w(sourceString), pMatch);
1404 if(!This->pattern) {
1405 *pMatch = VARIANT_TRUE;
1406 return S_OK;
1409 if(!This->regexp) {
1410 This->regexp = regexp_new(NULL, &This->pool, This->pattern,
1411 strlenW(This->pattern), This->flags, FALSE);
1412 if(!This->regexp)
1413 return E_FAIL;
1414 }else {
1415 hres = regexp_set_flags(&This->regexp, NULL, &This->pool, This->flags);
1416 if(FAILED(hres))
1417 return hres;
1420 mark = heap_pool_mark(&This->pool);
1421 result = alloc_match_state(This->regexp, &This->pool, sourceString);
1422 if(!result) {
1423 heap_pool_clear(mark);
1424 return E_OUTOFMEMORY;
1427 hres = regexp_execute(This->regexp, NULL, &This->pool,
1428 sourceString, SysStringLen(sourceString), result);
1430 heap_pool_clear(mark);
1432 if(hres == S_OK) {
1433 *pMatch = VARIANT_TRUE;
1434 }else if(hres == S_FALSE) {
1435 *pMatch = VARIANT_FALSE;
1436 hres = S_OK;
1438 return hres;
1441 static HRESULT WINAPI RegExp2_Replace(IRegExp2 *iface, BSTR sourceString,
1442 VARIANT replaceVar, BSTR *pDestString)
1444 RegExp2 *This = impl_from_IRegExp2(iface);
1445 FIXME("(%p)->(%s %s %p)\n", This, debugstr_w(sourceString),
1446 debugstr_variant(&replaceVar), pDestString);
1447 return E_NOTIMPL;
1450 static const IRegExp2Vtbl RegExp2Vtbl = {
1451 RegExp2_QueryInterface,
1452 RegExp2_AddRef,
1453 RegExp2_Release,
1454 RegExp2_GetTypeInfoCount,
1455 RegExp2_GetTypeInfo,
1456 RegExp2_GetIDsOfNames,
1457 RegExp2_Invoke,
1458 RegExp2_get_Pattern,
1459 RegExp2_put_Pattern,
1460 RegExp2_get_IgnoreCase,
1461 RegExp2_put_IgnoreCase,
1462 RegExp2_get_Global,
1463 RegExp2_put_Global,
1464 RegExp2_get_Multiline,
1465 RegExp2_put_Multiline,
1466 RegExp2_Execute,
1467 RegExp2_Test,
1468 RegExp2_Replace
1471 static inline RegExp2 *impl_from_IRegExp(IRegExp *iface)
1473 return CONTAINING_RECORD(iface, RegExp2, IRegExp_iface);
1476 static HRESULT WINAPI RegExp_QueryInterface(IRegExp *iface, REFIID riid, void **ppv)
1478 RegExp2 *This = impl_from_IRegExp(iface);
1479 return IRegExp2_QueryInterface(&This->IRegExp2_iface, riid, ppv);
1482 static ULONG WINAPI RegExp_AddRef(IRegExp *iface)
1484 RegExp2 *This = impl_from_IRegExp(iface);
1485 return IRegExp2_AddRef(&This->IRegExp2_iface);
1488 static ULONG WINAPI RegExp_Release(IRegExp *iface)
1490 RegExp2 *This = impl_from_IRegExp(iface);
1491 return IRegExp2_Release(&This->IRegExp2_iface);
1494 static HRESULT WINAPI RegExp_GetTypeInfoCount(IRegExp *iface, UINT *pctinfo)
1496 RegExp2 *This = impl_from_IRegExp(iface);
1497 return IRegExp2_GetTypeInfoCount(&This->IRegExp2_iface, pctinfo);
1500 static HRESULT WINAPI RegExp_GetTypeInfo(IRegExp *iface,
1501 UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
1503 RegExp2 *This = impl_from_IRegExp(iface);
1504 return IRegExp2_GetTypeInfo(&This->IRegExp2_iface, iTInfo, lcid, ppTInfo);
1507 static HRESULT WINAPI RegExp_GetIDsOfNames(IRegExp *iface, REFIID riid,
1508 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1510 RegExp2 *This = impl_from_IRegExp(iface);
1511 return IRegExp2_GetIDsOfNames(&This->IRegExp2_iface, riid, rgszNames, cNames, lcid, rgDispId);
1514 static HRESULT WINAPI RegExp_Invoke(IRegExp *iface, DISPID dispIdMember,
1515 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1516 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1518 RegExp2 *This = impl_from_IRegExp(iface);
1519 return IRegExp2_Invoke(&This->IRegExp2_iface, dispIdMember, riid, lcid,
1520 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1523 static HRESULT WINAPI RegExp_get_Pattern(IRegExp *iface, BSTR *pPattern)
1525 RegExp2 *This = impl_from_IRegExp(iface);
1526 return IRegExp2_get_Pattern(&This->IRegExp2_iface, pPattern);
1529 static HRESULT WINAPI RegExp_put_Pattern(IRegExp *iface, BSTR pPattern)
1531 RegExp2 *This = impl_from_IRegExp(iface);
1532 return IRegExp2_put_Pattern(&This->IRegExp2_iface, pPattern);
1535 static HRESULT WINAPI RegExp_get_IgnoreCase(IRegExp *iface, VARIANT_BOOL *pIgnoreCase)
1537 RegExp2 *This = impl_from_IRegExp(iface);
1538 return IRegExp2_get_IgnoreCase(&This->IRegExp2_iface, pIgnoreCase);
1541 static HRESULT WINAPI RegExp_put_IgnoreCase(IRegExp *iface, VARIANT_BOOL pIgnoreCase)
1543 RegExp2 *This = impl_from_IRegExp(iface);
1544 return IRegExp2_put_IgnoreCase(&This->IRegExp2_iface, pIgnoreCase);
1547 static HRESULT WINAPI RegExp_get_Global(IRegExp *iface, VARIANT_BOOL *pGlobal)
1549 RegExp2 *This = impl_from_IRegExp(iface);
1550 return IRegExp2_get_Global(&This->IRegExp2_iface, pGlobal);
1553 static HRESULT WINAPI RegExp_put_Global(IRegExp *iface, VARIANT_BOOL pGlobal)
1555 RegExp2 *This = impl_from_IRegExp(iface);
1556 return IRegExp2_put_Global(&This->IRegExp2_iface, pGlobal);
1559 static HRESULT WINAPI RegExp_Execute(IRegExp *iface,
1560 BSTR sourceString, IDispatch **ppMatches)
1562 RegExp2 *This = impl_from_IRegExp(iface);
1563 return IRegExp2_Execute(&This->IRegExp2_iface, sourceString, ppMatches);
1566 static HRESULT WINAPI RegExp_Test(IRegExp *iface, BSTR sourceString, VARIANT_BOOL *pMatch)
1568 RegExp2 *This = impl_from_IRegExp(iface);
1569 return IRegExp2_Test(&This->IRegExp2_iface, sourceString, pMatch);
1572 static HRESULT WINAPI RegExp_Replace(IRegExp *iface, BSTR sourceString,
1573 BSTR replaceString, BSTR *pDestString)
1575 RegExp2 *This = impl_from_IRegExp(iface);
1576 VARIANT replace;
1578 V_VT(&replace) = VT_BSTR;
1579 V_BSTR(&replace) = replaceString;
1580 return IRegExp2_Replace(&This->IRegExp2_iface, sourceString, replace, pDestString);
1583 static IRegExpVtbl RegExpVtbl = {
1584 RegExp_QueryInterface,
1585 RegExp_AddRef,
1586 RegExp_Release,
1587 RegExp_GetTypeInfoCount,
1588 RegExp_GetTypeInfo,
1589 RegExp_GetIDsOfNames,
1590 RegExp_Invoke,
1591 RegExp_get_Pattern,
1592 RegExp_put_Pattern,
1593 RegExp_get_IgnoreCase,
1594 RegExp_put_IgnoreCase,
1595 RegExp_get_Global,
1596 RegExp_put_Global,
1597 RegExp_Execute,
1598 RegExp_Test,
1599 RegExp_Replace
1602 HRESULT create_regexp(IDispatch **ret)
1604 RegExp2 *regexp;
1605 HRESULT hres;
1607 hres = init_regexp_typeinfo(RegExp2_tid);
1608 if(FAILED(hres))
1609 return hres;
1611 regexp = heap_alloc_zero(sizeof(*regexp));
1612 if(!regexp)
1613 return E_OUTOFMEMORY;
1615 regexp->IRegExp2_iface.lpVtbl = &RegExp2Vtbl;
1616 regexp->IRegExp_iface.lpVtbl = &RegExpVtbl;
1617 regexp->ref = 1;
1618 heap_pool_init(&regexp->pool);
1620 *ret = (IDispatch*)&regexp->IRegExp2_iface;
1621 return S_OK;
1624 HRESULT WINAPI VBScriptRegExpFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter, REFIID riid, void **ppv)
1626 IDispatch *regexp;
1627 HRESULT hres;
1629 TRACE("(%p %s %p)\n", pUnkOuter, debugstr_guid(riid), ppv);
1631 hres = create_regexp(&regexp);
1632 if(FAILED(hres))
1633 return hres;
1635 hres = IDispatch_QueryInterface(regexp, riid, ppv);
1636 IDispatch_Release(regexp);
1637 return hres;
1640 void release_regexp_typelib(void)
1642 DWORD i;
1644 for(i=0; i<REGEXP_LAST_tid; i++) {
1645 if(typeinfos[i])
1646 ITypeInfo_Release(typeinfos[i]);
1648 if(typelib)
1649 ITypeLib_Release(typelib);