Guest User

Untitled

a guest
Oct 6th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. char* ConvertToQueryString(struct listKeyValuePair* node)
  2. {
  3. static bool isStrQueryStringLocaled = false;
  4. if(isStrQueryStringLocaled)
  5. {
  6. free(strQueryString);
  7. isStrQueryStringLocaled = false;
  8. }
  9. puts("into ConvertToQueryString()");
  10. static char* strQueryString = (char*)calloc(sizeof(char), 1);
  11. puts("debug mark");
  12. if(NULL == strQueryString)
  13. {
  14. puts("calloc failed!");
  15. fflush(stdout);
  16. }
  17. else
  18. {
  19. isStrQueryStringLocaled = true;
  20. }
  21.  
  22. while(NULL != node)
  23. {//一個接一個按照QueryString格式串上去
  24. size_t newLength = strlen(strQueryString)+strlen(node->strKey)+strlen(node->strValue)+3; puts("while line 1");fflush(stdout);
  25. strQueryString = (char*)realloc(strQueryString, newLength); puts("while line 2");fflush(stdout); //有時會出錯
  26.  
  27. if(NULL == node->perv)
  28. {//如果這是第一個node
  29. //snprintf(strQueryString, newLength, "%s=%s",node->strKey, node->strValue);
  30. //sprintf會發生記憶體錯誤
  31. //snprintf無法把dest變數當作src變數,會把舊內容洗掉
  32. strncpy(strQueryString, node->strKey, strlen(node->strKey));
  33. strncat(strQueryString, "=", strlen("="));
  34. strncat(strQueryString, node->strValue, strlen(node->strValue));
  35. }
  36. else
  37. {//如果這是第二個以後node
  38. //snprintf(strQueryString, newLength, "%s&%s=%s", strQueryString, node->strKey, node->strValue);
  39. //sprintf會發生記憶體錯誤
  40. //snprintf無法把dest變數當作src變數,會把舊內容洗掉
  41. strncat(strQueryString, "&", strlen("&"));
  42. strncat(strQueryString, node->strKey, strlen(node->strKey));
  43. strncat(strQueryString, "=", strlen("="));
  44. strncat(strQueryString, node->strValue, strlen(node->strValue));
  45. }
  46. printf("strQueryString=%s\n",strQueryString);
  47. node = node->next;
  48. }
  49. //puts(strQueryString);
  50.  
  51. puts("out ConvertToQueryString()");
  52. return strQueryString;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment