⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.cpp

📁 wince用EVC编译的蓝牙扫描,文件传输,耳机控制软件.完完整软件包!MIPS,ARM编译通过 obexparser.lib filebrowser.cpp devicechooser.cpp
💻 CPP
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
#include "Parser.h"

#if defined (HPC_BUILD)
#undef isspace
#define isspace ___isspace

int ___isspace (char c) {
	return iswspace ((WCHAR)c);
}

int _strnicmp (const char *p, const char *q, int ct) {
	int c = 0;
	while ((c == 0) && (ct > 0)) {
		c = toupper (*p++) - toupper(*q++);
		--ct;
	}

	return c;
}


#endif

//extern SVSHandleSystem *g_pHandles;

char *obapi_getprop (char *p, char *prop) {
	int c = strlen (prop);
	while (*p) {
		if (_strnicmp (p, prop, c) == 0)
			break;
		++p;
	}

	if (! *p)
		return NULL;

	p += c;

 	while (isspace (*p))
		++p;

	if (*p++ != '=')
		return FALSE;

	while (isspace (*p))
		++p;

	if (*p++ != '"')
		return FALSE;

	return p;
}

int obapi_gettime (char *p, FILETIME *pft) {
	SYSTEMTIME st;
	memset (&st, 0, sizeof(st));

	for (int i = 0 ; i < 8 + 1 + 6 ; ++i) {
		if ((p[i] < '0') || (p[i] > '9')) {
			if (i != 8)
				return FALSE;
			if ((p[i] != 'T') && (p[i] != 't'))
				return FALSE;
		}
	}

	st.wYear = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + (p[3] - '0');
	st.wMonth = 10 * (p[4] - '0') + (p[5] - '0');
	st.wDay = 10 * (p[6] - '0') + (p[7] - '0');
	st.wHour = 10 * (p[9] - '0') + (p[10] - '0');
	st.wMinute = 10 * (p[11] - '0') + (p[12] - '0');
	st.wSecond = 10 * (p[13] - '0') + (p[14] - '0');

	return SystemTimeToFileTime (&st, pft);
}


static int obapi_GetWFD (char *p, WIN32_FIND_DATA *pwfd, DWORD dwAttr) {
	memset (pwfd, 0, sizeof(*pwfd));
	char *pName = obapi_getprop (p, "name");
	if (! pName)
		return FALSE;
	char *pNameEnd = strchr (pName, '"');
	if (! pNameEnd)
		return FALSE;

	*pNameEnd = '\0';

	int iRes = MultiByteToWideChar (CP_ACP, 0, pName, -1, pwfd->cFileName, MAX_PATH);

	if ((iRes <= 0) || (iRes > MAX_PATH))
		return FALSE;

	*pNameEnd = '"';

	char *ptime = obapi_getprop (p, "created");
	if (ptime && (! obapi_gettime (ptime, &pwfd->ftCreationTime)))
		return FALSE;

	char *pmodtime = obapi_getprop (p, "modified");
	if (pmodtime && (! obapi_gettime (pmodtime, &pwfd->ftLastWriteTime)))
		return FALSE;

	if (! pmodtime) {
		if (ptime)
			pwfd->ftLastWriteTime = pwfd->ftLastAccessTime = pwfd->ftCreationTime;
	} else
		pwfd->ftLastAccessTime = pwfd->ftLastWriteTime;

	if (! ptime) {
		if (pmodtime)
			pwfd->ftCreationTime = pwfd->ftLastAccessTime = pwfd->ftLastWriteTime;
	}

	char *psize = obapi_getprop (p, "size");
	if (psize) {
		char *pSizeEnd = strchr (psize, '"');
		if (! pSizeEnd)
			return FALSE;
		*pSizeEnd = '\0';
		pwfd->nFileSizeLow = atoi (psize);
		*pSizeEnd = '"';
	}

	pwfd->dwFileAttributes = dwAttr;

	return TRUE;
}


ObexFileSearch *ObexFindFileFirst (IStream *myStream, WCHAR *szPattern, WIN32_FIND_DATA *pwfd) 
{
    //suck in the directory information
    HRESULT hr;
    char *pBuffer = 0;
    UINT pDirSize = 0;
    char inBuf[2000];  
    do
    {
        ULONG cbJustRead;
        hr = myStream->Read(inBuf, sizeof(inBuf), &cbJustRead);
       
        if(SUCCEEDED(hr))
        {
            char *pTemp = new char[pDirSize + cbJustRead + 1];
            if(pBuffer)
                memcpy(pTemp, pBuffer, pDirSize);
            memcpy(pTemp+pDirSize, inBuf, cbJustRead);
            
            if(pBuffer)
                delete [] pBuffer;

            pBuffer = pTemp;
            pDirSize += cbJustRead; 
        }
    } while(SUCCEEDED(hr));


    if(!pBuffer)
        return FALSE;

	pBuffer[pDirSize] = '\0';

	int fError = FALSE;

	char *p = (char *)pBuffer;

	int cFiles = 0;
	int cFolders = 0;

	while (! fError) {
		p = strchr (p, '<');

		if (! p)
			break;

		char *pEnd = strchr (p, '>');

		if (! pEnd) {
			fError = TRUE;
			break;
		}

		*pEnd = '\0';

		WIN32_FIND_DATA wfd;

		if ((_strnicmp (p, "<folder", 7) == 0) && isspace (p[7])) {
			if (! obapi_GetWFD (p + 7, &wfd, FILE_ATTRIBUTE_DIRECTORY)) {
				fError = TRUE;
				break;
			}
			++cFolders;
		} else if ((_strnicmp (p, "<file", 5) == 0) && isspace (p[5])) {
			if (! obapi_GetWFD (p + 5, &wfd, FILE_ATTRIBUTE_NORMAL)) {
				fError = TRUE;
				break;
			}

			++cFiles;
		}
		*pEnd = '>';
		p = pEnd + 1;
	}

	if (fError) {
		delete [] pBuffer;
		SetLastError (ERROR_UNEXP_NET_ERR);
		return FALSE;
	}
	if (cFiles + cFolders <= 0) {
		delete [] pBuffer;
		SetLastError (ERROR_FILE_NOT_FOUND);
		return FALSE;
	}

	int cTotalSize = offsetof (ObexFileSearch, wfd) + sizeof(WIN32_FIND_DATA) * (cFiles + cFolders);

	ObexFileSearch *pofs = (ObexFileSearch *)LocalAlloc (LMEM_FIXED, cTotalSize);
	pofs->cCurrent = 1;
	pofs->cTotal   = cFiles + cFolders;
	pofs->fWhat    = OBEX_HANDLE_SEARCH;

	p = (char *)pBuffer;
	int i = 0;

	while (! fError) {
		p = strchr (p, '<');

		if (! p)
			break;

		char *pEnd = strchr (p, '>');
		*pEnd = '\0';

		if ((_strnicmp (p, "<folder", 7) == 0) && isspace (p[7]))
			obapi_GetWFD (p + 7, &pofs->wfd[i++], FILE_ATTRIBUTE_DIRECTORY);
		else if ((_strnicmp (p, "<file", 5) == 0) && isspace (p[5]))
			obapi_GetWFD (p + 5, &pofs->wfd[i++], FILE_ATTRIBUTE_NORMAL);

		*pEnd = '>';
		p = pEnd + 1;
	}

	delete [] pBuffer;

	*pwfd = pofs->wfd[0];

    return pofs;
}





BOOL ObexFindFileNext (ObexFileSearch *pSearch, WIN32_FIND_DATA *pwfd) 
{
	if (pSearch->cCurrent >= pSearch->cTotal)
		return FALSE;

	*pwfd = pSearch->wfd[pSearch->cCurrent++];
	return TRUE;
}


BOOL ObexCloseSearch (ObexFileSearch *pSearch) 
{
	LocalFree (pSearch);
	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -