]> git.sesse.net Git - cubemap/blob - parse.h
Write our own HTTP client instead of using curl. Not finished yet (missing URL parsin...
[cubemap] / parse.h
1 #ifndef _PARSE_H
2 #define _PARSE_H
3
4 // Various routines that deal with parsing; both configuration files and HTTP requests.
5
6 #include <map>
7 #include <vector>
8 #include <string>
9
10 struct ConfigLine {
11         std::string keyword;
12         std::vector<std::string> arguments;
13         std::map<std::string, std::string> parameters;
14 };
15
16 // Split a line on whitespace, e.g. "foo  bar baz" -> {"foo", "bar", "baz"}.
17 std::vector<std::string> split_tokens(const std::string &line);
18
19 // Split a string on \n or \r\n, e.g. "foo\nbar\r\n\nbaz\r\n\r\n" -> {"foo", "bar", "baz"}.
20 std::vector<std::string> split_lines(const std::string &str);
21
22 // Parse the configuration file.
23 std::vector<ConfigLine> parse_config(const std::string &filename);
24
25 // Note: Limits are inclusive.
26 int fetch_config_int(const std::vector<ConfigLine> &config, const std::string &keyword, int min_limit, int max_limit);
27
28 // Add the new data to an existing string, looking for \r\n\r\n
29 // (typical of HTTP requests and/or responses). Will return one
30 // of the given statuses.
31 //
32 // Note that if you give too much data in new_data_size, you could
33 // get an RP_OUT_OF_SPACE even if you expected RP_EXTRA_DATA.
34 // Be careful about how large reads you give in.
35 enum RequestParseStatus {
36         RP_OUT_OF_SPACE,       // If larger than 16 kB.
37         RP_NOT_FINISHED_YET,   // Did not get \r\n\r\n yet. 
38         RP_EXTRA_DATA,         // Got \r\n\r\n, but there was extra data behind it.
39         RP_FINISHED,           // Ended exactly in \r\n\r\n.
40 };
41 RequestParseStatus wait_for_double_newline(std::string *existing_data, const char *new_data, size_t new_data_size);
42
43 #endif  // !defined(_PARSE_H)