用于解析查询字符串的 C 库?

发布于 2024-10-10 09:51:51 字数 86 浏览 0 评论 0原文

我正在编写一个 C/CGI Web 应用程序。是否有一个库可以将查询字符串解析为 GHashTable 之类的内容?我可以自己编写,但似乎不值得重新发明轮子。

I'm writing a C/CGI web application. Is there a library to parse a query string into something like a GHashTable? I could write my own but it certainly doesn't seem to be worth the effort to reinvent the wheel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过气美图社 2024-10-17 09:51:51

uriparser 库可以解析 查询字符串 转换为键值对。

The uriparser library can parse query strings into key-value pairs.

走走停停 2024-10-17 09:51:51

如果您确实正在编写 C 并且键集是已知的,那么您最好将值存储在 struct 中,而不是存储在一些臃肿的哈希表中。有一个static const表:

  • 键名称
  • 类型(整数/字符串可能就足够了)
  • 结构中的偏移量(使用offsetof宏)

并使用它来解析查询字符串并填充在结构中。

If you're really writing C and the set of keys is known, you'd do much better to store the values in a struct instead of some bloated hash table. Have a static const table of:

  • key name
  • type (integer/string is probably sufficient)
  • offset in struct (using offsetof macro)

and use that to parse the query string and fill in the struct.

芸娘子的小脾气 2024-10-17 09:51:51

您可能会发现 Apache Portable Runtime (apr) 库中的 ap_getword 函数系列很有用。

大多数字符串解析例程都属于 ap_getword* 系列,它们共同提供类似于 Perl split() 函数的功能。该家族的每个成员都能够从字符串中提取单词,并按空格或逗号等分隔符分割文本。与 Perl split() 不同,在 Perl split() 中,整个字符串被一次分割,并且各个片段以列表的形式返回,而 ap_getword* 函数一次只对一个单词进行操作。该函数每次调用时都会返回下一个单词,并通过向上移动指针来跟踪它所在的位置。

你可以在google上搜索"ap_getword(r->pool" "httpd.h",找到一些相关的源代码来学习。

You may find usefull the ap_getword functions family from the Apache Portable Runtime (apr) library.

Most of the string parsing routines belong to the ap_getword* family, which together provide functionality similar to the Perl split() function. Each member of this family is able to extract a word from a string, splitting the text on delimiters such as whitespace or commas. Unlike Perl split(), in which the entire string is split at once and the pieces are returned in a list, the ap_getword* functions operate on one word at a time. The function returns the next word each time it's called and keeps track of where it's been by bumping up a pointer.

You may search google for "ap_getword(r->pool" "httpd.h" and find some relevant source code to learn from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文