fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. void replace_callID ( char* msg, char* newVal);
  6. main()
  7. {
  8. char msg[100];
  9. strcpy(msg, "test\r\nCall-Id: 1234\r\ntest");
  10. replace_callID (msg, "1112458254");
  11. puts(msg);
  12. }
  13.  
  14. void strupr(char *string)
  15. {
  16. while(*string)
  17. {
  18. if ( *string >= 'a' && *string <= 'z' )
  19. {
  20. *string = *string - 32;
  21. }
  22. string++;
  23. }
  24. }
  25.  
  26. void replace_callID ( char* msg, char* newVal)
  27. {
  28. // assuming that the msg will allow to hold the bigger buffer if the newVal is greater than the existing Call-ID value
  29. const char* callIdToken = "CALL-ID: ";
  30. char *upper = strdup(msg);
  31. strupr(upper);
  32. char *callId = strstr(upper, callIdToken);
  33. // find the CALL-ID (using the UPPER CASE message to find a case insensitive string)
  34. if(callId)
  35. {
  36.  
  37. char *callIdEol = strstr(callId, "\r\n");
  38. if(callIdEol)
  39. {
  40. // store the remaining part of the message in a variable
  41. char *theRest = strdup(msg + (callIdEol - upper));
  42. // terminate the msg string straight after the Call-ID
  43. msg[(callId - upper) + strlen(callIdToken)] = 0;
  44. // append new value
  45. strcat(msg, newVal);
  46. //append the remaining message
  47. strcat(msg, theRest);
  48. // free memory
  49. free(theRest);
  50. }
  51. }
  52. // free memory
  53. free(upper);
  54. }
  55.  
Runtime error #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
test
Call-Id: 1112458254
test