goahead内部url中键值数字读取

// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <iostream>
using namespace std;

#define CGI_MAX_SOCKET  256
#define CGI_MAX_BUF  	1024
#define CGI_MAX_LEN 	256


static int GetStrParamValue(const char *purl, const char *pkey, char *pkeyvalue,int maxlen)
{
	const char 		*phead;
	const char 		*pdst = purl;
	const char		*pend = purl;
	char 			*presult = NULL;
	int 				len = 0;

	//find key
	//	printf("purl:%s pkey:%s\n",purl,pkey);
	while(1){
		phead = strstr(pdst, pkey);
		if (!phead)
		{
			return -1;
		}
		presult = (char*)(phead - 1);		//split chart
		//		printf("presult %x\n",presult[0]);
		if ((presult[0] == 0x3f) || (presult[0] == 0x26))	//0x3f->? 0x26->&
		{
			break;
		}
		pdst += strlen(pkey);
	}
	//	printf("phead0:%s\n",phead);
	//move phead to start of value,eg:value=12345;,then move phead=12345;
	phead = phead + strlen(pkey) ;
	//find key end
	pend = strstr(phead, "&");
	//if find end of key
	if (pend)
	{	//find end of key
		len = pend - phead;
		if (len > maxlen){
			len = maxlen;
		}
		memcpy(pkeyvalue, phead, len);
		//printf("phead1:%s pend:%s===len=%d\n",phead,pend,len);
		return 0;		
	}
	//not find end of key,this is cgi end
	len = strlen(phead);
	if (len > maxlen){
		len = maxlen;
	}
	memcpy(pkeyvalue, phead, len);
	//printf("keyvalue:%s phead2:%s\n",pkey,phead);
	return 0;
}



#define 	NON_NUM 	'0'
static int hex2num(char c)
{
	if (c>='0' && c<='9') return c - '0';
	if (c>='a' && c<='z') return c - 'a' + 10;
	if (c>='A' && c<='Z') return c - 'A' + 10;
	return NON_NUM;
}

static int URLDecode(const char* str, const int strSize, char* result, const int resultSize)
{
	char ch,ch1,ch2;
	int i;
	int j = 0;//record result index

	if ((str==NULL) || (result==NULL) || (strSize<=0) || (resultSize<=0)) {
		return 0;
	}

	for ( i=0; (i<strSize) && (j<resultSize); ++i) {
		ch = str[i];
		switch (ch) {
			case '+':
				result[j++] = ' ';
				break;
			case '%':
				if (i+2<strSize) {
					ch1 = hex2num(str[i+1]);//high 4 bit
					ch2 = hex2num(str[i+2]);//low 4 bit
					if ((ch1!=NON_NUM) && (ch2!=NON_NUM))
						result[j++] = (char)((ch1<<4) | ch2);
					i += 2;
					break;
				} else {
					break;
				}
			default:
				result[j++] = ch;
				break;
		}
	}
	result[j] = 0;
	return j;
}

int GetKeyValue(char *pparam,char *pkey,char *pvalue)
{
	int					iRet = -1;
	int					len = strlen(pkey);
	char					keyname[CGI_MAX_LEN];
	char					keyvalue[CGI_MAX_LEN];
	char                            decoderbuf[CGI_MAX_LEN];

	//	printf("get key value=%d value:%s\n",len,pkey);
	//	printf("pparam:%s pkey:%s pvalue:%s\n",pparam,pkey,pvalue);
	//check maxlen
	if( (!pparam) || (!pkey) || (!pvalue)){
		return iRet;
	}
	memset(keyname,0x00,CGI_MAX_LEN);
	strcpy(keyname,pkey);
	keyname[len]='=';
	//	 printf("get key value1 pkey=%s\n",keyname);
	memset(keyvalue,0x00,CGI_MAX_LEN);
	iRet = GetStrParamValue(pparam, keyname, keyvalue,CGI_MAX_LEN - 1);
	if (iRet == 0x00){
		memset(decoderbuf,0x00,CGI_MAX_LEN);
		URLDecode(keyvalue,CGI_MAX_LEN,decoderbuf,CGI_MAX_LEN-1);
		strcpy(pvalue,decoderbuf);
		//		 printf("pvalue:%s\n",pvalue);
	}
	//	printf("get key value2=%d\n",iRet);
	return iRet;
}
int  GetKeyInt(char *pparam,char *pkey,int *pvalue)
{
	int 					iRet = -1;
	char                            keyvalue[CGI_MAX_LEN];

	memset( keyvalue, 0x00, CGI_MAX_LEN );
	iRet = GetKeyValue(pparam, pkey, keyvalue);
	//printf("iRet %d pkey:%s keyvalue:%s\n",iRet,pkey,keyvalue);
	if (iRet == 0x00){
		*pvalue = atoi(keyvalue);
		//printf("*pvalue=%d\n", *pvalue);
	}
	return iRet;
}
int _tmain(int argc, _TCHAR* argv[])
{


    //char value[100];
	int value = 0;
	int *pvalue = &value;
//	GetStrParamValue("https://www.baidu.com/s?wd=error%20C2440%3A%20","wd",value,100);
    GetKeyInt("https://www.baidu.com/s?wd=error%20C2440%3A%20&abc=123","abc",pvalue);
	cout<<value<<endl;
	system("PAUSE");

	return 0;
}

  输出123...

posted on 2023-03-02 06:25  lydstory  阅读(24)  评论(0编辑  收藏  举报

导航