Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char* ConvertToQueryString(struct listKeyValuePair* node)
- {
- static bool isStrQueryStringLocaled = false;
- if(isStrQueryStringLocaled)
- {
- free(strQueryString);
- isStrQueryStringLocaled = false;
- }
- puts("into ConvertToQueryString()");
- static char* strQueryString = (char*)calloc(sizeof(char), 1);
- puts("debug mark");
- if(NULL == strQueryString)
- {
- puts("calloc failed!");
- fflush(stdout);
- }
- else
- {
- isStrQueryStringLocaled = true;
- }
- while(NULL != node)
- {//一個接一個按照QueryString格式串上去
- size_t newLength = strlen(strQueryString)+strlen(node->strKey)+strlen(node->strValue)+3; puts("while line 1");fflush(stdout);
- strQueryString = (char*)realloc(strQueryString, newLength); puts("while line 2");fflush(stdout); //有時會出錯
- if(NULL == node->perv)
- {//如果這是第一個node
- //snprintf(strQueryString, newLength, "%s=%s",node->strKey, node->strValue);
- //sprintf會發生記憶體錯誤
- //snprintf無法把dest變數當作src變數,會把舊內容洗掉
- strncpy(strQueryString, node->strKey, strlen(node->strKey));
- strncat(strQueryString, "=", strlen("="));
- strncat(strQueryString, node->strValue, strlen(node->strValue));
- }
- else
- {//如果這是第二個以後node
- //snprintf(strQueryString, newLength, "%s&%s=%s", strQueryString, node->strKey, node->strValue);
- //sprintf會發生記憶體錯誤
- //snprintf無法把dest變數當作src變數,會把舊內容洗掉
- strncat(strQueryString, "&", strlen("&"));
- strncat(strQueryString, node->strKey, strlen(node->strKey));
- strncat(strQueryString, "=", strlen("="));
- strncat(strQueryString, node->strValue, strlen(node->strValue));
- }
- printf("strQueryString=%s\n",strQueryString);
- node = node->next;
- }
- //puts(strQueryString);
- puts("out ConvertToQueryString()");
- return strQueryString;
- }
Advertisement
Add Comment
Please, Sign In to add comment