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

📄 vutils.cpp

📁 这个是微软WINCE的OBEX协议例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		return 0;
	}
	
	
	//now make sure that is safe to place into a VCard (check out escape chars)
	char *tmpPtr = retBuf;
	UINT escapes = 0;
	
	//count the # of escape characters
	while(*tmpPtr != 0)
	{
		switch(*tmpPtr)
		{
		case ':':
		case ';':
		case ',':
			escapes ++;
			break;
		}
		tmpPtr ++;
	}
	
	if(escapes)
	{
		//allocate a new buffer to hold the data
		char *newBuf = new char[requiredSize + escapes+10];
		char *tmpNew = newBuf;
		tmpPtr = retBuf;
		
		while(*tmpPtr != 0)
		{
			if(*tmpPtr == ':' || *tmpPtr == ';' || *tmpPtr == ',')
			{
				*tmpNew = '\\';
				tmpNew ++;
			}
			
			*tmpNew = *tmpPtr;
			tmpPtr++;
			tmpNew++;
		}
		
		*tmpNew = NULL;
		delete [] retBuf;
		return newBuf;
	}
	
	else
		return retBuf;
}



/*****************************************************************************/
/*   Function: ParseFields       							                 */
/*       Parse out individual components of a vCard entry                    */
/*                                                                           */
/*   WARNING: this function ALLOCATES MEMORY that MUST BE FREED BY CALLER!!  */
/*       the memory is attached to the linked list passed in pCL             */
/*                                                                           */
/*   NOTE: the ORDINAL is described above in the LOOKUP TABLE declaration    */
/*                                                                           */
/*   Begin looking at the field for a seperator character (';') if found,    */
/*       loop through all the fields (assigning a ordinal to each)           */
/*       and then search the main LOOKUP TABLE for entries that match        */
/*       if there is a match, call the INPUT FUNCTION that is specified for  */
/*       the particular entry in the LOOKUP TABLE.  The INPUT FUNCTION       */
/*       returns a CEPROPVAL that must be chained to the CEPROP_LIST that is */
/*       sent as a argument                                                  */
/*****************************************************************************/
void ParseFields(CEPROP_LIST *pCL, OID_STRUCT *pOID, char *pID, char *pFields, void *pState)
{
	ASSERT(pCL);
	
	INT iOrd = 1;
	//find each ordinal
	while(pFields)
	{
		char *pFieldEnd = strchr(pFields, ';');
		char *pField = NULL;
		
		if(pFieldEnd)
		{
			//make sure we dont trigger on a encoded var
			while(pFieldEnd && *(pFieldEnd - 1) == '\\') 
				pFieldEnd = strchr(pFields, ';');
		}
		
		UINT uiFieldLen = 0;
		if(pFieldEnd)
			uiFieldLen = pFieldEnd - pFields;
		else
			uiFieldLen = strlen(pFields);
		
		//make a new field value
		pField = new char[uiFieldLen + 1];
		memcpy(pField, pFields, uiFieldLen);
		pField[uiFieldLen] = 0;
		
		//now we know the ordinal and the var type... 
        //  hunt down the OID (from the master table
		for(UINT i=0; i<pOID->uiNumElements; i++)
		{
			if(iOrd == pOID->pTrans[i].iOrd && 
               _stricmp(pID, pOID->pTrans[i].line_name)==0)
			{		
				//pass the data off to the inputFunction (found in the 
                //table)....  and insert it into the DB if instructed to
				CEPROPVAL *prop = 
                    pOID->pTrans[i].inputFunct(pOID->pTrans[i].oid_id, 
                                                          pID, 
                                                          pField,
                                                          pState);
				
				//if the input function created a new data record,
                //  chain it in
				if(prop)
				{
					if(!pCL->prop)
					{
						pCL->prop = prop;
					}
					else
					{
						CEPROP_LIST *newHolder = new CEPROP_LIST();
						memset(newHolder, 0, sizeof(CEPROP_LIST));
						newHolder->prop = prop;
						newHolder->pNext = pCL->pNext;
						pCL->pNext = newHolder;
					}
				}
				break;
			}
		}
		
        //cleanup
		if(pField)
		{
			delete [] pField;
		}

        //move to the next field and repeat (using a different ORDINAL)
		if(pFieldEnd)
			pFields = pFieldEnd + 1;
		else
			pFields = NULL;
		
		iOrd ++;
	}
}


/*****************************************************************************/
/*   Function: ParseLine       							                     */
/*       Given a partially parsed line, break the params of the list into    */
/*          a form more easily parsed by  ParseFields                        */                                                                 
/*                                                                           */
/*   Begin by looking for a TYPE= field.  if found its okay for there to be  */
/*       comma delimited params.                                             */
/*****************************************************************************/
void ParseLine(CEPROP_LIST *pCL, OID_STRUCT *pOID, char *pType, char *pParams, char *pFields, void *pState)
{
	UINT uiTypeLen = strlen(pType);
	
	if(pParams)
	{
		//see if the pParams has TYPE info, if so remove it
		if(strstr(pParams, "TYPE=") == pParams)
			pParams += 5;
		//now loop through all the params
		while(pParams)
		{
			char *pSep = strchr(pParams, ',');
			
			if(pSep)
			{
				UINT uiParamLen = pSep - pParams;
				char *pParam = new char[uiTypeLen + 6 + uiParamLen + 2];	
			
				memcpy(pParam, pType, uiTypeLen);
				memcpy(pParam+uiTypeLen, ";TYPE=", 6);
				memcpy(pParam+uiTypeLen+6, pParams, uiParamLen);
				pParam[uiParamLen+uiTypeLen+6] = ':';
				pParam[uiParamLen+uiTypeLen+7] = 0;
				
				ParseFields(pCL, pOID, pParam, pFields, pState);
				
				delete [] pParam;	
				pParams = pSep + 1;
			}
			else 
			{
				UINT uiParamLen = strlen(pParams);
				char *pParam = new char[uiTypeLen + 6 + uiParamLen + 2];
				
				memcpy(pParam, pType, uiTypeLen);
				memcpy(pParam+uiTypeLen, ";TYPE=", 6);
				memcpy(pParam+uiTypeLen+6, pParams, uiParamLen);
				pParam[uiParamLen+uiTypeLen+6] = ':';
				pParam[uiParamLen+uiTypeLen+7] = 0;
				
				//use pParams here
				ParseFields(pCL, pOID, pParam, pFields, pState);
				
				delete [] pParam;
				break;
			}
		}
	}
	else
	{
		char *pParam = new char[uiTypeLen + 2];
		
		sprintf(pParam, "%s:", pType);
		
		//use pParams here
		ParseFields(pCL, pOID, pParam, pFields, pState);
		
		delete [] pParam;
	}
	
	
	
}



/*****************************************************************************/
/*   Function: ParseMajorComponents       	                                 */
/*      Given a line from ParseVCard, break it apart and send results to     */
/*          ParseLine for further parsing                                    */
/*                                                                           */
/*       Given a parsed out LINE (just a line) parse out the FIELD NAME      */
/*       and EACH PARAMETER.  (note: since TYPE=<PARAM>,<PARAM>... is valid  */
/*       this is handled by the function ParseLine) send the results as a    */
/*       partially parsed line to ParseLine for further parsing.             */
/*****************************************************************************/
void ParseMajorComponents(CEPROP_LIST *pCL, OID_STRUCT *pOID, char *line, void *pState)
{
	char *pTypeSep = strchr(line, ';');
	char *pParamEnd = strchr(line, ':');

	if(!pParamEnd) return;
	
	//figure out the BASIC command type (meaning, just the first part)
	//  note that I'm looking for TYPE= ... but the PalmPilot doesnt
	//  do this so its not strictly required
	if(pTypeSep && pTypeSep < pParamEnd)
	{	
		UINT uiTypeLen = pTypeSep - line; 
		
		//record the line type
		line[uiTypeLen] = 0;
		
		//advance pTypeSep beyond the ';' and move on
		pTypeSep ++;
		for(;;)
		{
			//loop grabbing parameters
			char *pTypeSepEnd = strchr(pTypeSep, ';');
			
			if(!pTypeSepEnd)
				pTypeSepEnd = pParamEnd;
			
			char *pSepValue	= new char[pTypeSepEnd - pTypeSep + 1];
			memcpy(pSepValue, pTypeSep, pTypeSepEnd - pTypeSep);
			pSepValue[pTypeSepEnd - pTypeSep] = 0;
			
			//check it out
			ParseLine(pCL, pOID, line, pSepValue, pParamEnd + 1, pState);
			
			delete [] pSepValue;
			pTypeSep = pTypeSepEnd + 1;
			
			if(pTypeSepEnd >= pParamEnd) 
				break;
		}
	}
	else
	{
		UINT uiParamSize = pParamEnd - line;
        line[uiParamSize] = 0;
		
		//check it out
		ParseLine(pCL, pOID, line, 0, pParamEnd + 1, pState);
	}
}



/*****************************************************************************/
/*   Function: ParseVCard       	                                         */
/*      The main (ie first) function called in parsing the values from a     */
/*          vCard.  The fuction takes a pointer to a vCard (located in mem)  */
/*          and a CEPROP_LIST where data will be chained                     */
/*                                                                           */
/*      RETURN: the fuction returns the location of the next vCard entry,    */
/*          OR 0 indicating there are no more vCard entries                  */
/*                                                                           */
/*      PURPOSE:  Find a vCard line and send it off to be parsed             */
/*          Take care though tat BEGIN and END values are honored to         */
/*          prevent merging card entries                                     */
/*                                                                           */
/*****************************************************************************/
char *ParseVCard(CEPROP_LIST *pCL, OID_STRUCT *pOID, char *vCard, void *pState)
{
	if(!vCard)
		return 0;

	//MAIN PROCESSING LOOP:
	for(;;)
	{		
		//  grab a line from the buffer, and send it off to the parser
		char *pLineEnd= strstr(vCard, "\r\n");
		if(!pLineEnd)
			pLineEnd = strchr(vCard, '\n');
		
		//check for END, if found advance vCard and return
		if(strstr(vCard, "END:") == vCard)
		{
			char *retVal = strchr(vCard, '\n');
			if(retVal) 
				return retVal + 1;
			else 
				return retVal;
		}

		if(pLineEnd)
		{
			//copy the line, and null terminate it
			char *pLine = new char[pLineEnd - vCard + 1];
			memcpy(pLine, vCard, pLineEnd - vCard);
			pLine[pLineEnd - vCard] = 0;
			
			ParseMajorComponents(pCL, pOID, pLine, pState);
			
			delete [] pLine;
		}
		else
			break;
		
		//move to the next item
		vCard = pLineEnd + 1;
		if(*vCard == '\n') vCard++;
	}

    return 0;
}




/*****************************************************************************/
/*   Function: _stricmp       	                                             */
/*                                                                           */
/*   PURPOSE: to give _stricmp functionality on HPC's --- nothing            */
/*      special... just _stricmp                                             */
/*****************************************************************************/
int _stricmp( const char *string1, const char *string2) {
  
	while(*string1) {
		int a = toupper(*string1);
		int b = toupper(*string2);

		if(a - b != 0)
            return a - b;

        string1++;
		string2++;		
	}

	int a = toupper(*string1);
	int b = toupper(*string2);

    return a - b;
}


⌨️ 快捷键说明

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