解析RTSP地址(URL)

本文介绍了一个函数parseDetailRTSPURL,用于解析RTSP URL并提取用户名、密码、地址和端口号等详细信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//////////////////////////////////////////////////////////////////////////
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) ;





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值