[core] array_get_int_ptr()

This commit is contained in:
Glenn Strauss 2018-09-22 20:10:51 -04:00
parent 66ff05db8f
commit 002a4c524d
8 changed files with 43 additions and 17 deletions

View File

@ -690,6 +690,7 @@ add_executable(test_configfile
buffer.c
array.c
data_config.c
data_integer.c
data_string.c
http_header.c
http_kv.c
@ -704,6 +705,7 @@ add_executable(test_request
request.c
buffer.c
array.c
data_integer.c
data_string.c
http_header.c
http_kv.c

View File

@ -548,10 +548,10 @@ t_test_base64_LDADD = $(LIBUNWIND_LIBS)
t_test_burl_SOURCES = t/test_burl.c burl.c buffer.c base64.c
t_test_burl_LDADD = $(LIBUNWIND_LIBS)
t_test_configfile_SOURCES = t/test_configfile.c buffer.c array.c data_config.c data_string.c http_header.c http_kv.c vector.c log.c sock_addr.c
t_test_configfile_SOURCES = t/test_configfile.c buffer.c array.c data_config.c data_integer.c data_string.c http_header.c http_kv.c vector.c log.c sock_addr.c
t_test_configfile_LDADD = $(PCRE_LIB) $(LIBUNWIND_LIBS)
t_test_request_SOURCES = t/test_request.c request.c buffer.c array.c data_string.c http_header.c http_kv.c log.c sock_addr.c
t_test_request_SOURCES = t/test_request.c request.c buffer.c array.c data_integer.c data_string.c http_header.c http_kv.c log.c sock_addr.c
t_test_request_LDADD = $(LIBUNWIND_LIBS)
noinst_HEADERS = $(hdr)

View File

@ -223,6 +223,19 @@ void array_insert_value(array *hdrs, const char *value, size_t val_len) {
array_insert_unique(hdrs, (data_unset *)ds);
}
int * array_get_int_ptr(array *a, const char *k, size_t klen) {
data_integer *di = (data_integer *)array_get_element_klen(a, k, klen);
if (NULL == di) {
di = (data_integer *)array_get_unused_element(a, TYPE_INTEGER);
if (NULL == di) di = data_integer_init();
buffer_copy_string_len(di->key, k, klen);
array_insert_unique(a, (data_unset *)di);
}
return &di->value;
}
/* if entry already exists return pointer to existing entry, otherwise insert entry and return NULL */
static data_unset **array_find_or_insert(array *a, data_unset *entry) {
size_t ndx, pos, j;

View File

@ -71,6 +71,7 @@ data_unset *array_extract_element_klen(array *a, const char *key, size_t klen);
void array_set_key_value(array *hdrs, const char *key, size_t key_len, const char *value, size_t val_len);
void array_insert_key_value(array *hdrs, const char *key, size_t key_len, const char *value, size_t val_len);
void array_insert_value(array *hdrs, const char *value, size_t val_len);
int * array_get_int_ptr(array *a, const char *k, size_t klen);
void array_replace(array *a, data_unset *entry);
void array_print_indent(int depth);
size_t array_get_max_key_length(array *a);

View File

@ -1499,7 +1499,6 @@ static void context_free(config_t *context) {
int config_read(server *srv, const char *fn) {
config_t context;
data_config *dc;
data_integer *dpid;
buffer *dcwd;
int ret;
char *pos;
@ -1526,10 +1525,7 @@ int config_read(server *srv, const char *fn) {
context.current = dc;
/* default context */
dpid = data_integer_init();
dpid->value = getpid();
buffer_copy_string_len(dpid->key, CONST_STR_LEN("var.PID"));
array_insert_unique(dc->value, (data_unset *)dpid);
*array_get_int_ptr(dc->value, CONST_STR_LEN("var.PID")) = getpid();
dcwd = srv->tmp_buf;
buffer_string_prepare_copy(dcwd, 4095);

View File

@ -733,6 +733,7 @@ test('test_configfile', executable('test_configfile',
'buffer.c',
'array.c',
'data_config.c',
'data_integer.c',
'data_string.c',
'http_header.c',
'http_kv.c',
@ -750,6 +751,7 @@ test('test_request', executable('test_request',
'request.c',
'buffer.c',
'array.c',
'data_integer.c',
'data_string.c',
'http_header.c',
'http_kv.c',

View File

@ -18,16 +18,7 @@
*/
int *status_counter_get_counter(server *srv, const char *s, size_t len) {
data_integer *di;
if (NULL == (di = (data_integer *)array_get_element_klen(srv->status, s, len))) {
/* not found, create it */
di = data_integer_init();
buffer_copy_string_len(di->key, s, len);
di->value = 0;
array_insert_unique(srv->status, (data_unset *)di);
}
return &di->value;
return array_get_int_ptr(srv->status, s, len);
}
/* dummies of the statistic framework functions

View File

@ -7,6 +7,26 @@
#include "array.h"
#include "buffer.h"
static void test_array_get_int_ptr (void) {
data_integer *di;
int *i;
array *a = array_init();
i = array_get_int_ptr(a, CONST_STR_LEN("abc"));
assert(NULL != i);
*i = 4;
i = array_get_int_ptr(a, CONST_STR_LEN("abc"));
assert(NULL != i);
assert(*i == 4);
di = (data_integer *)array_get_element_klen(a, CONST_STR_LEN("does-not-exist"));
assert(NULL == di);
di = (data_integer *)array_get_element_klen(a, CONST_STR_LEN("abc"));
assert(NULL != di);
assert(di->value == 4);
array_free(a);
}
static void test_array_insert_value (void) {
data_string *ds;
array *a = array_init();
@ -74,6 +94,7 @@ static void test_array_set_key_value (void) {
}
int main() {
test_array_get_int_ptr();
test_array_insert_value();
test_array_insert_key_value();
test_array_set_key_value();