我想在我存储的字符串里进行上述的读取
时间: 2024-02-28 22:53:56 浏览: 16
您可以使用 GStreamer 的 gstring 库来实现类似的字符串读取。具体来说,您可以使用 g_string_new() 函数创建一个新的 GString 对象,然后使用 g_string_append() 函数将您的字符串追加到该对象中。然后,您可以使用 GRegex 库来搜索和提取您需要的信息。
以下是一个参考示例,假设您的字符串存储在 char *input 中:
```c
#include <gst/gst.h>
#include <glib.h>
#include <regex.h>
int main(int argc, char *argv[]) {
GstElement *pipeline;
GstBus *bus;
GstMessage *msg;
GError *error = NULL;
// Create a new GString object
GString *input_str = g_string_new(input);
// Define regular expressions for matching the desired information
regex_t ice_ufrag_regex, ice_pwd_regex, ice_candidate_regex;
regcomp(&ice_ufrag_regex, "a=ice-ufrag:(.*)", REG_EXTENDED);
regcomp(&ice_pwd_regex, "a=ice-pwd:(.*)", REG_EXTENDED);
regcomp(&ice_candidate_regex, "a=candidate:.*udp.*host.*", REG_EXTENDED);
// Search for ice-ufrag
regmatch_t matches[2];
if (regexec(&ice_ufrag_regex, input_str->str, 2, matches, 0) == 0) {
gchar *ufrag = g_strndup(input_str->str + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so);
g_print("Found ice-ufrag: %s\n", ufrag);
g_free(ufrag);
}
// Search for ice-pwd
if (regexec(&ice_pwd_regex, input_str->str, 2, matches, 0) == 0) {
gchar *pwd = g_strndup(input_str->str + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so);
g_print("Found ice-pwd: %s\n", pwd);
g_free(pwd);
}
// Search for ice-candidate
if (regexec(&ice_candidate_regex, input_str->str, 0, NULL, 0) == 0) {
g_print("Found ice-candidate\n");
}
// Free resources
g_string_free(input_str, TRUE);
regfree(&ice_ufrag_regex);
regfree(&ice_pwd_regex);
regfree(&ice_candidate_regex);
return 0;
}
```
这个示例使用 GRegex 库来搜索包含 "a=ice-ufrag:"、"a=ice-pwd:" 和 "a=candidate:" 的行,并从中提取相关信息。请注意,这只是一个参考示例,您需要根据您的具体需求进行相应修改。