bachra04
asked on
replace substring in c
I have the following
void replace_callID ( char* msg, char* newVal)
{
// The msg is like this
REGISTER sip:xxxxxxxxxx SIP/2.0\r\n
Via: SIP/2.0/TCP [22222222222222222222222]: 5060;branc h=z9hG4bKd ebc3b0671f 2;rport\r\ n
Max-Forwards: 70\r\n
Contact: <sip:3333333333333333@[222 2222222222 2222222222 22]:5060>\ r\n
To: <sip:555555555555555@rally e>\r\n
From: <sip:222222222222222>;tag= 493d93b081 015e61-959 93bb7.0\r\ n
Call-ID: 111119e0a61-114650f8-c7c29 8767685\r\ n
CSeq: 1 REGISTER\r\n
Expires: 600000\r\n
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n
}
// I want to replace the Call-ID value with new value e.g.
msg before calling replace_callID :
REGISTER sip:xxxxxxxxxx SIP/2.0\r\n
Via: SIP/2.0/TCP [22222222222222222222222]: 5060;branc h=z9hG4bKd ebc3b0671f 2;rport\r\ n
Max-Forwards: 70\r\n
Contact: <sip:3333333333333333@[222 2222222222 2222222222 22]:5060>\ r\n
To: <sip:555555555555555@rally e>\r\n
From: <sip:222222222222222>;tag= 493d93b081 015e61-959 93bb7.0\r\ n
Call-ID: 111119e0a61-114650f8-c7c29 8767685\r\ n
CSeq: 1 REGISTER\r\n
Expires: 600000\r\n
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n
replace_callID ( msg, "0202020202020202020202020 202")
Then msg becomes
REGISTER sip:xxxxxxxxxx SIP/2.0\r\n
Via: SIP/2.0/TCP [22222222222222222222222]: 5060;branc h=z9hG4bKd ebc3b0671f 2;rport\r\ n
Max-Forwards: 70\r\n
Contact: <sip:3333333333333333@[222 2222222222 2222222222 22]:5060>\ r\n
To: <sip:555555555555555@rally e>\r\n
From: <sip:222222222222222>;tag= 493d93b081 015e61-959 93bb7.0\r\ n
Call-ID: 02020202020202020202020202 02\r\n
CSeq: 1 REGISTER\r\n
Expires: 600000\r\n
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n
Call_ID can be CALL_ID or call_id etc ..
void replace_callID ( char* msg, char* newVal)
{
// The msg is like this
REGISTER sip:xxxxxxxxxx SIP/2.0\r\n
Via: SIP/2.0/TCP [22222222222222222222222]:
Max-Forwards: 70\r\n
Contact: <sip:3333333333333333@[222
To: <sip:555555555555555@rally
From: <sip:222222222222222>;tag=
Call-ID: 111119e0a61-114650f8-c7c29
CSeq: 1 REGISTER\r\n
Expires: 600000\r\n
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n
}
// I want to replace the Call-ID value with new value e.g.
msg before calling replace_callID :
REGISTER sip:xxxxxxxxxx SIP/2.0\r\n
Via: SIP/2.0/TCP [22222222222222222222222]:
Max-Forwards: 70\r\n
Contact: <sip:3333333333333333@[222
To: <sip:555555555555555@rally
From: <sip:222222222222222>;tag=
Call-ID: 111119e0a61-114650f8-c7c29
CSeq: 1 REGISTER\r\n
Expires: 600000\r\n
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n
replace_callID ( msg, "0202020202020202020202020
Then msg becomes
REGISTER sip:xxxxxxxxxx SIP/2.0\r\n
Via: SIP/2.0/TCP [22222222222222222222222]:
Max-Forwards: 70\r\n
Contact: <sip:3333333333333333@[222
To: <sip:555555555555555@rally
From: <sip:222222222222222>;tag=
Call-ID: 02020202020202020202020202
CSeq: 1 REGISTER\r\n
Expires: 600000\r\n
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n
Call_ID can be CALL_ID or call_id etc ..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
BTW, in C++ this code will look like this:
void replace_callID ( char* msg, char* newVal)
{
const string callIdToken = "CALL-ID: ";
string s(msg);
string u(s);
transform(u.begin(), u.end(), u.begin(), ::toupper);
size_t pos, pos1;
if((pos = u.find(callIdToken)) != string::npos &&
(pos1 = u.find("\r\n", pos)) != string::npos)
{
s.replace(pos + callIdToken.length(), pos1 - pos - callIdToken.length(), newVal);
strcpy(msg, s.c_str());
}
}
Open in new window