//////////////////////////////////////////////////////////////////////////
int parseDetailRTSPURL(char const* url, char* &username, char* &password, char* address,int* portNum)
{
do {
// Parse the URL as "rtsp://[<username>[:<password>]@]<server-address-or-name>[:<port>][/<stream-name>]"
char const* prefix = "rtsp://";
unsigned const prefixLength = 7;
unsigned const parseBufferSize = 100;
char const* from = &url[prefixLength];
// Check whether "<username>[:<password>]@" occurs next.
// We do this by checking whether '@' appears before the end of the URL, or before the first '/'.
username = password = NULL; // default return values
char const* colonPasswordStart = NULL;
char const* p;
for (p = from; *p != '\0' && *p != '/'; ++p)
{
if (*p == ':' && colonPasswordStart == NULL)
{
colonPasswordStart = p;
}
else if (*p == '@')
{
// We found <username> (and perhaps <password>). Copy them into newly-allocated result strings:
if (colonPasswordStart == NULL) colonPasswordStart = p;
char const* usernameStart = from;
unsigned usernameLen = colonPasswordStart - usernameStart;
username = new char[usernameLen + 1] ; // allow for the trailing '\0'
for (unsigned i = 0; i < usernameLen; ++i) username[i] = usernameStart[i];
username[usernameLen] = '\0';
char const* passwordStart = colonPasswordStart;
if (passwordStart < p) ++passwordStart; // skip over the ':'
unsigned passwordLen = p - passwordStart;
password = new char[passwordLen + 1]; // allow for the trailing '\0'
for (unsigned j = 0; j < passwordLen; ++j) password[j] = passwordStart[j];
password[passwordLen] = '\0';
from = p + 1; // skip over the '@'
break;
}
}
// Next, parse <server-address-or-name>
char* to = &address[0];//parseBuffer[0];
unsigned i;
for (i = 0; i < parseBufferSize; ++i)
{
if (*from == '\0' || *from == ':' || *from == '/')
{
// We've completed parsing the address
*to = '\0';
break;
}
*to++ = *from++;
}
if (i == parseBufferSize) {
printf("URL is too long");
break;
}
*portNum = 554; // default value
char nextChar = *from;
if (nextChar == ':') {
int portNumInt;
if (sscanf(++from, "%d", &portNumInt) != 1)
{
fprintf(stderr,"No port number follows :%d\n",portNumInt);
return -1;
}
if (portNumInt < 1 || portNumInt > 65535)
{
fprintf(stderr,"Bad port number\n");
return -1;
}
*portNum = portNumInt;
}
return 1;
} while (0);
return -1;
}
使用方法:
char* url = "rtsp://admin:12345@192.168.0.64";
char* address = new char[100];
char* username = new char[100];
char* password = new char[100];
memset(address,0,100);
int destPortNum;
parseDetailRTSPURL(url,username,password,address,&destPortNum) ;