编辑代码

#include <stdio.h>
#include <stdlib.h>

// int* generateRange(char* code) {
//     int start, end;
//     sscanf(code, "%d-%d", &start, &end); // 解析字符串获取起始和结束数值

//     int size = end - start + 1;
//     int* result = (int*)malloc(size * sizeof(int)); // 动态分配数组内存

//     for (int i = 0; i < size; i++) {
//         result[i] = start + i; // 将起始和结束之间的整数存入数组
//     }

//     return result;
// }

// char *strReplace(char *str, char *oldstr, char *newstr)
// {
//   int j = 0;
//   char bstr[strlen(str)+10];  // 转换缓冲区

//   memset(bstr, 0, sizeof(bstr));
//   for (j = 0; j < strlen(str); j++)
//   {
//     if (!strncmp(str + j, oldstr, strlen(oldstr)))
//     {  // 查找目标字符串
//       strcat(bstr, newstr);
//       j += strlen(oldstr) - 1;
//     }
//     else
//     {
//       strncat(bstr, str + j, 1);  // 保存一字节进缓冲区
//     }
//   }

//   strcpy(str, bstr);
//   return str;
// }
// int GetAimNum(char* str, char aim)
// {
// 	int str_len = strlen(str);
// 	int count = 0;
// 	for (int i = 0; i < str_len; i++)
// 	{
// 		if (*str == aim)
// 			count++;
// 		str++;
// 	}
// 	return count;
// }

// void Replace(char* str, char *oldchar, char* newstr)
// {
// 	int i = strlen(str);//i指向有效字符串的位置即'\0'字符前
// 	int newstr_len = strlen(newstr);//求取 替换字符串 的有效长度
// 	int count = GetAimNum(str, oldchar);//获取字符串中的替换字符长度
// 	int j = i + count * (newstr_len - 1);
// 	//j指向末尾字符加要替换字符数乘(替换字符串长度-1)
// 	while (i >= 0)//如果i小于0说明已经遍历整个字符串
// 	{
// 		if (str[i] != oldchar)//如果i下标对于的字符不是要替换的字符就将它向后移
// 		{
// 			str[j--] = str[i--];
// 		}
// 		else//如果是就用替换字符串替换要替换字符
// 		{
// 			for (int z = newstr_len - 1; z >= 0; z--)
// 			{
// 				str[j--] = newstr[z];
// 			}
// 			i--;
// 		}
// 	}
// }

static char * strReplace(char const * const original,
	char const * const pattern, char const * const replacement)
{
	size_t const replen = strlen(replacement);
	size_t const patlen = strlen(pattern);
	size_t const orilen = strlen(original);
	size_t patcnt = 0;
	const char * oriptr;
	const char * patloc;

	// find how many times the pattern occurs in the originaL string
	for (oriptr = original; (patloc = strstr(oriptr, pattern)); oriptr = patloc + patlen)
		patcnt++;

	// allocate memory for the new string
	const size_t retlen = orilen + patcnt * (replen - patlen);
	char * returned = (char *)malloc(sizeof(char) * (retlen + 1));

	if (returned != NULL) {
		// copy the original string,
		// repLacing all the instances of the pattern
		char * retptr = returned;
		for (oriptr = original;(patloc = strstr(oriptr, pattern)); oriptr = patloc + patlen) {
			size_t const skplen = patloc - oriptr;
			// copy the section until the occurence of the pattern
			strncpy(retptr, oriptr, skplen);
			retptr += skplen;
			// copy the replacement
			strncpy(retptr, replacement, replen);
			retptr += replen;
		}

		// copy the rest of the string.
		strcpy(retptr, oriptr);
	}
	return returned;
}

syslog(LOG_NOTICE, "[WEB]: Config string:%s", string); // ghm add syslog

syslog(LOG_NOTICE, "[WEB]: Config int:%d", int); // ghm add syslog

int main() {
    char code[64] = "1234%567%%89";
    printf("code=%s\n", code);
    char *code2 = NULL;
    char *test3 = "http://mudule=getwlanChannel&country_code=CN&channel_width=3";
    char *ptest3 = NULL;
    char test4[3];
    char test5;
    char *countryList[][] = {"CN","EN","ID"};
    printf("countryList[0][]=%d\n",countryList[0][1]);
    ptest3 = strstr(test3, "channel_width");
    if(ptest3 != NULL){
        ptest3 += 14;
        strncpy(test4, ptest3, 1);
        // test4[3] = '\0';
        int a = atoi(test4);
        printf("ptest3=%s\n",ptest3);
        printf("ptest4=%s\n",test4);
        printf("a=%d\n",a);
    }

    unsigned int test1 = 15;
    int test2 = 0;
    test2 = test1 & (1 << 4);
    printf("test2=%d\n",test2);


    // int* range = generateRange(code);
    code2 = strReplace(code, "%", "%%");
    printf("code2=%s\n", code2);
    free(code2);

    return 0;
}