// 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
typedef int (*WebCallback)(char *output,char *input,unsigned char auth);
typedef struct _CGISOCKET{
int socket;
unsigned char buf[CGI_MAX_BUF];
char auth;
}CGISOCKET,*PCGISOCKET;
typedef struct _CGIWEBCMD{
int cmdindex; //cgi command index
char key[32]; //cgi command string
WebCallback callback;
short p2pcmd;
short other;
}CGIWEBCMD,*PCGIWEBCMD;
typedef struct _CGISETPARAM{
int cmdindex; //cgi command index
char key[32]; //cgi command string
int (*callback)(char *output,char *input,unsigned char auth); //cgi calllback
short p2pcmd; //cgi command index
short other; //cgi command index
}CGISETPARAM,*PCGISETPARAM;
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 _tmain(int argc, _TCHAR* argv[])
{
char value[100];
// GetStrParamValue("https://www.baidu.com/s?wd=error%20C2440%3A%20","wd",value,100);
GetKeyValue("https://www.baidu.com/s?wd=error%20C2440%3A%20","wd",value);
cout<<value<<endl;
system("PAUSE");
return 0;
}
输出:error C2440: