📄 sdptest.cxx
字号:
u.uuid16 = uuid16;
}
NDW(ULONG uuid32, UCHAR d)
{
type=SDP_TYPE_UUID;
specificType=SDP_ST_UUID32;
u.uuid32 = uuid32;
}
void Uuid32(ULONG uuid32)
{
Z();
type=SDP_TYPE_UUID;
specificType=SDP_ST_UUID32;
u.uuid32 = uuid32;
}
NDW(GUID *g)
{
type=SDP_TYPE_UUID;
specificType=SDP_ST_UUID128;
u.uuid128 = *g;
}
void Uuid128(GUID& g)
{
Z();
type=SDP_TYPE_UUID;
specificType=SDP_ST_UUID128;
u.uuid128 = g;
}
};
// typedef std::stack<ISdpNodeContainer *> ContainerStack;
typedef CHAR ContainerStack;
typedef void (*PFNPARSE)(FILE*, USHORT attribVal, ISdpRecord *pRecord, ContainerStack& Stack, ISdpNodeContainer *pCont);
struct ParseEntry {
PFNPARSE pfnParse;
char *name;
};
void AddUint8(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddUint16(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddUint32(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddUint64(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddUint128(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddInt8(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddInt16(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddInt32(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddInt64(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddInt128(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddUuid16(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddUuid32(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddUuid128(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddBoolean(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddNil(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddString(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddUrl(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddSequence(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void AddAlternative(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
void ContainerEnd(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont);
ParseEntry entries[] = {
{AddUint8, "UINT8"},
{AddUint16, "UINT16"},
{AddUint32, "UINT32"},
{AddUint64, "UINT64"},
{AddUint128, "UINT128"},
{AddInt8, "INT8"},
{AddInt16, "INT16"},
{AddInt32, "INT32"},
{AddInt64, "INT64"},
{AddInt128, "INT128"},
{AddUuid16, "UUID16"},
{AddUuid32, "UUID32"},
{AddUuid128, "UUID128"},
{AddBoolean, "BOOLEAN"},
{AddString, "STRING"},
{AddUrl, "URL"},
{AddSequence, "SEQUENCE"},
{AddAlternative, "ALTERNATIVE"},
{AddNil, "Nil"},
{ContainerEnd, "END" },
{NULL, NULL},
};
void AddUint8(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
int i;
fscanf(pf, "%x\n", &i);
NDW ndw;
ndw.Uint8((UCHAR) (i & 0xFF));
ADD_ENTRY();
}
void AddUint16(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack,ISdpNodeContainer *pCont)
{
int i;
fscanf(pf, "%x\n", &i);
NDW ndw;
ndw.Uint16((USHORT) (i & 0xFFFF));
ADD_ENTRY();
}
void AddUint32(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
int i;
fscanf(pf, "%x\n", &i);
NDW ndw;
ndw.Uint32((UINT) i);
ADD_ENTRY();
}
void AddUint64(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
ULONGLONG ull;
fscanf(pf, "%I64x\n", &ull);
NDW ndw;
ndw.Uint64(ull);
ADD_ENTRY();
}
void AddUint128(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
SDP_ULARGE_INTEGER_16 u128;
fscanf(pf, "%I64x,",&u128.HighPart);
fscanf(pf, "%I64x\n",&u128.LowPart);
NDW ndw(&u128);
ADD_ENTRY();
}
void AddInt8(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
int i;
fscanf(pf, "%x\n", &i);
NDW ndw((CHAR) (i & 0xFF));
ADD_ENTRY();
}
void AddInt16(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
int i;
fscanf(pf, "%x\n", &i);
NDW ndw((SHORT) (i & 0xFFFF));
ADD_ENTRY();
}
void AddInt32(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
int i;
fscanf(pf, "%x\n", &i);
NDW ndw((ULONG) i);
ADD_ENTRY();
}
void AddInt64(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
LONGLONG ll;
fscanf(pf, "%I64x\n", &ll);
NDW ndw(ll);
ADD_ENTRY();
}
void AddInt128(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
SDP_LARGE_INTEGER_16 u128;
fscanf(pf, "%I64x,",&u128.HighPart);
fscanf(pf, "%I64x\n",&u128.LowPart);
NDW ndw(&u128);
ADD_ENTRY();
}
void AddUuid16(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
int i;
fscanf(pf, "%x\n", &i);
NDW ndw;
ndw.Uuid16((USHORT) (i & 0xFFFF));
ADD_ENTRY();
}
void AddUuid32(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
int i;
fscanf(pf, "%x\n", &i);
NDW ndw;
ndw.Uuid32(i);
ADD_ENTRY();
}
void AddUuid128(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
GUID guid[2];
memset(guid,0,sizeof(guid));
fscanf(pf,GUID_FORMAT,GUID_ELEMENTS((&guid[0])));
NDW ndw(&guid[0]);
ADD_ENTRY();
}
void AddBoolean(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
int i;
fscanf(pf, "%i\n", &i);
NDW ndw;
ndw.Boolean(i);
ADD_ENTRY();
}
void AddNil(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
NDW ndw;
ADD_ENTRY();
}
void AddString(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
CHAR buffer[2048];
int i;
for (i = 0; i < sizeof(buffer); i++) {
if (EOF == fscanf(pf, "%c", &buffer[i])) {
buffer[i] = 0;
break;
}
if (buffer[i] == '\r' || buffer[i] == '\n') {
if (buffer[i] == '\r')
fscanf(pf, "%c", &buffer[i]);
buffer[i] = 0;
break;
}
}
NDW ndw(buffer, strlen(buffer), TRUE);
ADD_ENTRY();
}
void AddUrl(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
CHAR buffer[2048];
fscanf(pf, "%s\n", buffer);
NDW ndw(buffer, strlen(buffer), FALSE);
ADD_ENTRY();
}
void AddSeqAlternative(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont, BOOL fSequence)
{
ISdpNodeContainer *pContainer = NULL;
ContainerStack stack;
HRESULT hr = pCFContainer->CreateInstance(NULL,
__uuidof(ISdpNodeContainer),
(PVOID*) &pContainer);
if (!pContainer) {
wprintf(L"CoCreateInstance ISdpNodeContainer failed, hr=0x%08x\n",hr);
return;
}
if (fSequence)
pContainer->SetType(NodeContainerTypeSequence);
else
pContainer->SetType(NodeContainerTypeAlternative);
CHAR buffer[2048];
fscanf(pf, "\n");
while (1) {
fscanf(pf, "%s", buffer);
if (ParseHandleComment(pf,buffer,sizeof(buffer)))
continue;
if (_stricmp(buffer,"END") == 0)
break;
for (int i = 0; entries[i].name != NULL; i++) {
if (_stricmp(buffer, entries[i].name) == 0) {
entries[i].pfnParse(pf,0, 0,stack,pContainer);
break;
}
}
if (entries[i].name == NULL) {
wprintf(L"Read sequence: %S unknown op code\n",buffer);
break;
}
}
NDW ndw(pContainer);
ADD_ENTRY();
pContainer->Release();
}
void AddSequence(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
AddSeqAlternative(pf, attribVal, pRecord, Stack, pCont, TRUE);
}
void AddAlternative(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
AddSeqAlternative(pf, attribVal, pRecord, Stack, pCont, FALSE);
}
void ContainerEnd(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ContainerStack &Stack, ISdpNodeContainer *pCont)
{
wprintf(L"CointainerEnd not support \n");
}
BOOL CreateSdpRecord(WCHAR *szFile, UCHAR **ppStream, ULONG *pSize)
{
BOOL fSuccess = FALSE;
ContainerStack stack;
ISdpRecord *record = NULL;
FILE *pf = NULL;
CoInitializeEx(NULL,COINIT_MULTITHREADED);
HRESULT hr = CoGetClassObject(__uuidof(SdpRecord),
CLSCTX_INPROC_SERVER,
NULL,
IID_IClassFactory,
(PVOID*)&pCFRecord);
if (pCFRecord == NULL) {
wprintf(L"CoGetClassObject(uuidof(SdpRecord) fails, err=0x%08x\n",hr);
goto Finished;
}
hr = CoGetClassObject(__uuidof(SdpNodeContainer),
CLSCTX_INPROC_SERVER,
NULL,
IID_IClassFactory,
(PVOID*)&pCFContainer);
if (pCFContainer == NULL) {
wprintf(L"CoGetClassObject(uuidof(SdpNodeContainer) fails, err=0x%08x\n",hr);
goto Finished;
}
hr = pCFRecord->CreateInstance(NULL,
__uuidof(ISdpRecord),
(PVOID*) &record);
if (record == NULL) {
wprintf(L"CreateInstance on ISdpRecord fails, err = 0x%08x\n",hr);
goto Finished;
}
pf = _wfopen(szFile, L"r");
if (!pf) {
wprintf(L"cannot open %s\n", szFile);
goto Finished;
}
int attribId;
CHAR buffer[2048];
while (fscanf(pf, "%s", buffer) != EOF) {
if (ParseHandleComment(pf,buffer,sizeof(buffer)))
continue;
// Otherwise the first element on line is the Attribute ID
if (! sscanf(buffer, "%x", &attribId)) {
wprintf(L"*** ERROR ***: buffer does not contain attribute ID, buffer = %S\n",buffer);
goto Finished;
}
fscanf(pf, "%s\n", buffer);
for (int i = 0; entries[i].name != NULL; i++) {
if (_stricmp(buffer, entries[i].name) == 0) {
entries[i].pfnParse(pf,attribId, record, stack,NULL);
break;
}
}
if (entries[i].name == NULL) {
wprintf(L"unknown item %S in %s, quitting\n", buffer,szFile);
goto Finished;
}
}
fclose(pf);
UCHAR *stream;
ULONG size;
if (SUCCEEDED(record->WriteToStream(&stream, &size,0,0))) {
fSuccess = TRUE;
}
Finished:
#if ! (defined (UNDER_CE) || defined (WINCE_EMULATION))
while (!stack.empty()) {
stack.top()->Release();
stack.pop();
}
#endif // UNDER_CE
if (pCFRecord) {
pCFRecord->Release();
pCFRecord = NULL;
}
if (pCFContainer) {
pCFContainer->Release();
pCFContainer = NULL;
}
if (record) {
record->Release();
}
if (fSuccess) {
*ppStream = stream;
*pSize = size;
}
CoUninitialize();
return fSuccess;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -