]>
git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSWindows/DLLX/TXTRecord.cpp
1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2009 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 Change History (most recent first):
19 $Log: TXTRecord.cpp,v $
20 Revision 1.1 2009/05/26 04:43:54 herscher
21 <rdar://problem/3948252> COM component that can be used with any .NET language and VB.
27 #include "TXTRecord.h"
28 #include "StringServices.h"
29 #include <DebugServices.h>
35 STDMETHODIMP
CTXTRecord::SetValue(BSTR key
, VARIANT value
)
40 DNSServiceErrorType err
;
45 TXTRecordCreate( &m_tref
, 0, NULL
);
49 ok
= BSTRToUTF8( key
, keyUTF8
);
50 require_action( ok
, exit
, hr
= S_FALSE
);
52 ok
= VariantToByteArray( &value
, valueArray
);
53 require_action( ok
, exit
, hr
= S_FALSE
);
55 err
= TXTRecordSetValue( &m_tref
, keyUTF8
.c_str(), ( uint8_t ) valueArray
.size(), &valueArray
[ 0 ] );
56 require_action( !err
, exit
, hr
= S_FALSE
);
63 STDMETHODIMP
CTXTRecord::RemoveValue(BSTR key
)
71 DNSServiceErrorType err
;
73 ok
= BSTRToUTF8( key
, keyUTF8
);
74 require_action( ok
, exit
, hr
= S_FALSE
);
76 err
= TXTRecordRemoveValue( &m_tref
, keyUTF8
.c_str() );
77 require_action( !err
, exit
, hr
= S_FALSE
);
85 STDMETHODIMP
CTXTRecord::ContainsKey(BSTR key
, VARIANT_BOOL
* retval
)
91 if ( m_byteArray
.size() > 0 )
95 ok
= BSTRToUTF8( key
, keyUTF8
);
96 require_action( ok
, exit
, err
= S_FALSE
);
98 ret
= TXTRecordContainsKey( ( uint16_t ) m_byteArray
.size(), &m_byteArray
[ 0 ], keyUTF8
.c_str() );
101 *retval
= ( ret
) ? VARIANT_TRUE
: VARIANT_FALSE
;
108 STDMETHODIMP
CTXTRecord::GetValueForKey(BSTR key
, VARIANT
* value
)
111 const void * rawValue
;
116 VariantClear( value
);
118 if ( m_byteArray
.size() > 0 )
120 ok
= BSTRToUTF8( key
, keyUTF8
);
121 require_action( ok
, exit
, hr
= S_FALSE
);
123 rawValue
= TXTRecordGetValuePtr( ( uint16_t ) m_byteArray
.size(), &m_byteArray
[ 0 ], keyUTF8
.c_str(), &rawValueLen
);
127 ok
= ByteArrayToVariant( rawValue
, rawValueLen
, value
);
128 require_action( ok
, exit
, hr
= S_FALSE
);
137 STDMETHODIMP
CTXTRecord::GetCount(ULONG
* count
)
141 if ( m_byteArray
.size() > 0 )
143 *count
= TXTRecordGetCount( ( uint16_t ) m_byteArray
.size(), &m_byteArray
[ 0 ] );
149 STDMETHODIMP
CTXTRecord::GetKeyAtIndex(ULONG index
, BSTR
* retval
)
153 const void * rawValue
;
155 DNSServiceErrorType err
;
159 err
= TXTRecordGetItemAtIndex( ( uint16_t ) m_byteArray
.size(), &m_byteArray
[ 0 ], ( uint16_t ) index
, sizeof( keyBuf
), keyBuf
, &rawValueLen
, &rawValue
);
160 require_action( !err
, exit
, hr
= S_FALSE
);
162 ok
= UTF8ToBSTR( keyBuf
, temp
);
163 require_action( ok
, exit
, hr
= S_FALSE
);
172 STDMETHODIMP
CTXTRecord::GetValueAtIndex(ULONG index
, VARIANT
* retval
)
176 const void * rawValue
;
178 DNSServiceErrorType err
;
182 err
= TXTRecordGetItemAtIndex( ( uint16_t ) m_byteArray
.size(), &m_byteArray
[ 0 ], ( uint16_t ) index
, sizeof( keyBuf
), keyBuf
, &rawValueLen
, &rawValue
);
183 require_action( !err
, exit
, hr
= S_FALSE
);
185 ok
= ByteArrayToVariant( rawValue
, rawValueLen
, retval
);
186 require_action( ok
, exit
, hr
= S_FALSE
);
197 const unsigned char * bytes
,
201 check ( bytes
!= NULL
);
204 m_byteArray
.reserve( len
);
205 m_byteArray
.assign( bytes
, bytes
+ len
);