goahead内部url中键值数字读取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | // 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...
分类:
源码
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2022-03-02 spdx许可
2022-03-02 mtk ralink
2022-03-02 GCT Semiconductor
2022-03-02 mtk
2022-03-02 qt vs配置
2022-03-02 工业4g 5g 互联网
2022-03-02 QMovie gif播放类