Delan Azabani
Delan Azabani

Reputation: 81384

C library for parsing query strings?

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.

Upvotes: 5

Views: 4537

Answers (3)

vgfeit
vgfeit

Reputation: 61

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.

Upvotes: 0

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215261

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.

Upvotes: 2

Alex Reynolds
Alex Reynolds

Reputation: 96937

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

Upvotes: 3

Related Questions