diff options
Diffstat (limited to 'gawkapi.h')
-rw-r--r-- | gawkapi.h | 88 |
1 files changed, 59 insertions, 29 deletions
@@ -309,7 +309,8 @@ typedef enum { AWK_ARRAY, AWK_SCALAR, /* opaque access to a variable */ AWK_VALUE_COOKIE, /* for updating a previously created value */ - AWK_REGEX /* last for binary compatibility */ + AWK_REGEX, + AWK_STRNUM } awk_valtype_t; /* @@ -326,6 +327,7 @@ typedef struct awk_value { awk_value_cookie_t vc; } u; #define str_value u.s +#define strnum_value str_value #define regex_value str_value #define num_value u.d #define array_cookie u.a @@ -349,7 +351,7 @@ typedef struct awk_element { AWK_ELEMENT_DELETE = 1 /* set by extension if should be deleted */ } flags; - awk_value_t index; /* guaranteed to be a string! */ + awk_value_t index; awk_value_t value; } awk_element_t; @@ -495,29 +497,28 @@ typedef struct gawk_api { Table entry is type returned: - +-------------------------------------------------------------+ - | Type of Actual Value: | - +------------+------------+-----------+-----------+-----------+ - | String | Number | Regex | Array | Undefined | - +-----------+-----------+------------+------------+-----------+-----------+-----------+ - | | String | String | String | String | false | false | - | +-----------+------------+------------+-----------+-----------+-----------+ - | | Number | Number if | Number | false | false | false | - | | | can be | | | | | - | | | converted, | | | | | - | | | else false | | | | | - | +-----------+------------+------------+-----------+-----------+-----------+ - | | Regex | false | false | Regex | false | false | - | +-----------+------------+------------+-----------+-----------+-----------+ - | Type | Array | false | false | false | Array | false | - | Requested +-----------+------------+------------+-----------+-----------+-----------+ - | | Scalar | Scalar | Scalar | Scalar | false | false | - | +-----------+------------+------------+-----------+-----------+-----------+ - | | Undefined | String | Number | Regex | Array | Undefined | - | +-----------+------------+------------+-----------+-----------+-----------+ - | | Value | false | false | false | false | false | - | | Cookie | | | | | | - +-----------+-----------+------------+------------+-----------+-----------+-----------+ + +-------------------------------------------------------+ + | Type of Actual Value: | + +--------+--------+--------+--------+-------+-----------+ + | String | Strnum | Number | Regex | Array | Undefined | + +-----------+-----------+--------+--------+--------+--------+-------+-----------+ + | | String | String | String | String | String | false | false | + | +-----------+--------+--------+--------+--------+-------+-----------+ + | | Strnum | false | Strnum | Strnum | false | false | false | + | +-----------+--------+--------+--------+--------+-------+-----------+ + | | Number | Number | Number | Number | false | false | false | + | +-----------+--------+--------+--------+--------+-------+-----------+ + | | Regex | false | false | false | Regex | false | false | + | +-----------+--------+--------+--------+--------+-------+-----------+ + | Type | Array | false | false | false | false | Array | false | + | Requested +-----------+--------+--------+--------+--------+-------+-----------+ + | | Scalar | Scalar | Scalar | Scalar | Scalar | false | false | + | +-----------+--------+--------+--------+--------+-------+-----------+ + | | Undefined | String | Strnum | Number | Regex | Array | Undefined | + | +-----------+--------+--------+--------+--------+-------+-----------+ + | | Value | false | false | false | false | false | false | + | | Cookie | | | | | | | + +-----------+-----------+--------+--------+--------+--------+-------+-----------+ */ /* Functions to handle parameters passed to the extension. */ @@ -684,7 +685,13 @@ typedef struct gawk_api { /* Clear out an array */ awk_bool_t (*api_clear_array)(awk_ext_id_t id, awk_array_t a_cookie); - /* Flatten out an array so that it can be looped over easily. */ + /* + * Flatten out an array so that it can be looped over easily. + * This function returns all indices as strings and values as + * the native type one would get from an AWK_UNDEFINED request. + * Please use api_flatten_array_typed for more control over the + * type conversions. + */ awk_bool_t (*api_flatten_array)(awk_ext_id_t id, awk_array_t a_cookie, awk_flat_array_t **data); @@ -740,6 +747,16 @@ typedef struct gawk_api { /* Print nonfatal error message */ void (*api_nonfatal)(awk_ext_id_t id, const char *format, ...); + /* + * Flatten out an array with type conversions as requested. + * This supersedes the api_flatten_array function that did not allow + * the caller to specify the requested types. + */ + awk_bool_t (*api_flatten_array_typed)(awk_ext_id_t id, + awk_array_t a_cookie, + awk_flat_array_t **data, + awk_valtype_t index_type, awk_valtype_t value_type); + } gawk_api_t; #ifndef GAWK /* these are not for the gawk code itself! */ @@ -806,8 +823,11 @@ typedef struct gawk_api { #define clear_array(array) (api->api_clear_array(ext_id, array)) +#define flatten_array_typed(array, data, index_type, value_type) \ + (api->api_flatten_array_typed(ext_id, array, data, index_type, value_type)) + #define flatten_array(array, data) \ - (api->api_flatten_array(ext_id, array, data)) + flatten_array_typed(array, data, AWK_STRING, AWK_UNDEFINED) #define release_flattened_array(array, data) \ (api->api_release_flattened_array(ext_id, array, data)) @@ -843,7 +863,7 @@ typedef struct gawk_api { /* Constructor functions */ -/* r_make_string_type --- make a string or regexp value in result from the passed-in string */ +/* r_make_string_type --- make a string or strnum or regexp value in result from the passed-in string */ static inline awk_value_t * r_make_string_type(const gawk_api_t *api, /* needed for emalloc */ @@ -888,7 +908,17 @@ r_make_string(const gawk_api_t *api, /* needed for emalloc */ #define make_const_string(str, len, result) r_make_string(api, ext_id, str, len, 1, result) #define make_malloced_string(str, len, result) r_make_string(api, ext_id, str, len, 0, result) -#define make_regex(str, len, result) r_make_string_type(api, ext_id, str, len, 1, result, AWK_REGEX) + +#define make_const_regex(str, len, result) r_make_string_type(api, ext_id, str, len, 1, result, AWK_REGEX) +#define make_malloced_regex(str, len, result) r_make_string_type(api, ext_id, str, len, 0, result, AWK_REGEX) + +/* + * Note: The caller may not create a Strnum, but it can create a string that is + * flagged as user input that MAY be a Strnum. Gawk will decide whether it's a + * Strnum or a String by checking whether the string is numeric. + */ +#define make_const_user_input(str, len, result) r_make_string_type(api, ext_id, str, len, 1, result, AWK_STRNUM) +#define make_malloced_user_input(str, len, result) r_make_string_type(api, ext_id, str, len, 0, result, AWK_STRNUM) /* make_null_string --- make a null string value */ |