开发者

C library for parsing query strings?

开发者 https://www.devze.com 2023-02-01 23:24 出处:网络
I\'m writing a C/CGI web application. Is there a li开发者_C百科brary 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 effor

I'm writing a C/CGI web application. Is there a li开发者_C百科brary 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.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消