diff options
-rw-r--r-- | fhp.c | 47 | ||||
-rw-r--r-- | include/fhp/fhp.h | 29 |
2 files changed, 63 insertions, 13 deletions
@@ -131,6 +131,53 @@ fhp_strerror( return FHP_OK; } +static const char * +fhp_tokens[] = { + "METHOD_START", + "METHOD_FRAGMENT", + "METHOD_END", + + "URL_START", + "URL_FRAGMENT", + "URL_END", + + "VERSION_START", + "VERSION_FRAGMENT", + "VERSION_END", + + "HEADER_NAME_START", + "HEADER_NAME_FRAGMENT", + "HEADER_NAME_END", + + "HEADER_VALUE_START", + "HEADER_VALUE_FRAGMENT", + "HEADER_VALUE_END", + + "LAST" +}; + +fhp_err_t +fhp_strtoken( + fhp_token_t token, + char * const buf, + size_t len +) { + // check token code + if (token >= FHP_TOKEN_LAST) + return FHP_ERR_INVALID_ERROR; + + // check buffer size + size_t str_len = strlen(fhp_tokens[token]) + 1; + if (len < str_len) + return FHP_ERR_BUFFER_TOO_SMALL; + + // copy string + memcpy(buf, fhp_tokens[token], str_len); + + // return success + return FHP_OK; +} + static fhp_t DEFAULT_CONTEXT = { .state = FHP_STATE_INIT, .user_data = NULL, diff --git a/include/fhp/fhp.h b/include/fhp/fhp.h index 4a54e3c..3b7e280 100644 --- a/include/fhp/fhp.h +++ b/include/fhp/fhp.h @@ -6,6 +6,19 @@ #include <stdbool.h> // for size_t typedef enum { + FHP_OK, + FHP_ERR_CB, + FHP_ERR_BAD_STATE, + FHP_ERR_INVALID_CHAR, + FHP_ERR_INVALID_ERROR, + FHP_ERR_BUFFER_TOO_SMALL, + FHP_ERR_LAST +} fhp_err_t; + +fhp_err_t +fhp_strerror(fhp_err_t, char * const, size_t); + +typedef enum { FHP_TOKEN_METHOD_START, FHP_TOKEN_METHOD_FRAGMENT, FHP_TOKEN_METHOD_END, @@ -29,6 +42,9 @@ typedef enum { FHP_TOKEN_LAST } fhp_token_t; +fhp_err_t +fhp_strtoken(fhp_token_t, char * const, size_t); + typedef struct fhp_t_ fhp_t; typedef bool (*fhp_cb_t)( @@ -61,19 +77,6 @@ typedef enum { FHP_STATE_LAST } fhp_state_t; -typedef enum { - FHP_OK, - FHP_ERR_CB, - FHP_ERR_BAD_STATE, - FHP_ERR_INVALID_CHAR, - FHP_ERR_INVALID_ERROR, - FHP_ERR_BUFFER_TOO_SMALL, - FHP_ERR_LAST -} fhp_err_t; - -fhp_err_t -fhp_strerror(fhp_err_t, char * const, size_t); - #define FHP_BUF_SIZE 1024 struct fhp_t_ { |