bool parseVideoPort(char* buf, struct parseParameter* mPara) { uint16_t RtpVideoPort = 0; uint16_t RtcpVideoPort = 0; std::string rBuf = buf; std::size_t pos = rBuf.find("Transport"); if (pos != NULL) { if ((pos = rBuf.find("RTP/AVP")) != std::string::npos) { pos = rBuf.find("server_port", pos); if (pos != std::string::npos) { sscanf(rBuf.c_str() + pos, "%*[^=]=%hu-%hu", &RtpVideoPort, &RtcpVideoPort); mPara->mRtpVideoPort = RtpVideoPort; mPara->mRtcpVideoPort = RtcpVideoPort; return true; } } } else { return false; } }C语言怎么实现相同的效果
时间: 2024-04-16 16:26:02 浏览: 19
要在 C 语言中实现与上述代码相同的效果,您可以使用 C 字符串库和字符串处理函数来完成。下面是一个类似的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
uint16_t mRtpVideoPort;
uint16_t mRtcpVideoPort;
} parseParameter;
int parseVideoPort(char* buf, parseParameter* mPara) {
uint16_t RtpVideoPort = 0;
uint16_t RtcpVideoPort = 0;
char* pos = strstr(buf, "Transport");
if (pos != NULL) {
if ((pos = strstr(pos, "RTP/AVP")) != NULL) {
pos = strstr(pos, "server_port");
if (pos != NULL) {
sscanf(pos, "%*[^=]=%hu-%hu",
&RtpVideoPort, &RtcpVideoPort);
mPara->mRtpVideoPort = RtpVideoPort;
mPara->mRtcpVideoPort = RtcpVideoPort;
return 1;
}
}
}
return 0;
}
```
在这个示例中,我们使用了 `sscanf` 函数来解析字符串,并将结果存储在相应的变量中。`strstr` 函数用于查找子字符串的位置。
请注意,在 C 语言中,我们需要使用指针来操作字符串,并使用相应的指针运算来处理字符串的位置。
希望这个示例能帮助您理解如何在 C 语言中实现相同的效果。如果还有其他问题,请随时提问。