atoll实现与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 #include <stdio.h> long long atoll(const char* str) { long long e = 0; long long tmpKeep = 0; int i = 0; const int len = strlen(str); if (len <= 0) { goto EXIT_FUN; } if (str[0] == '-') { e = atoll(str + 1); e = (-e); goto EXIT_FUN; } for (i = 0; i < len; i++) { if (str[i] > '9' || str[i] < '0') { break; } tmpKeep = e; e *= 10; e += (((long long)(str[i] - '0')) & 0x00000000000000FF); if (tmpKeep > e) { /* 越界 */ e = 0; goto EXIT_FUN; } } EXIT_FUN: return e; } 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 GetKeyLong(char *pparam,char *pkey,long long *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 = atoll(keyvalue); printf("*pvalue=%lld, keyvalue=%s\n", *pvalue, keyvalue); } return iRet; } int GetKeyStr(char *pparam,char *pkey,char *pvalue,int maxlen) { int iRet = -1; int len = 0; char keyvalue[CGI_MAX_LEN]; memset( keyvalue, 0x00, CGI_MAX_LEN ); // printf("pparam:%s pkey:%s keyvalue:%s\n",pparam,pkey,keyvalue); iRet = GetKeyValue(pparam, pkey, keyvalue); if (iRet == 0x00){ len = strlen(keyvalue); memset(pvalue,0x00,maxlen); if (len > maxlen){ len = maxlen; } memcpy(pvalue,keyvalue,len); iRet = 0x00; } 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; }
【推荐】国内首个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播放类