From f8ee5bf0e4dd424d2b6b55b8b4334a26a4186472 Mon Sep 17 00:00:00 2001 From: Brenden Blanco Date: Wed, 12 Aug 2015 08:55:09 -0700 Subject: [PATCH] Add sscanf C api for parsing key/leaf ascii to binary Expose an individual API of the what is done in table_update, that lets the caller use the module to parse keys for it. Signed-off-by: Brenden Blanco --- src/cc/bpf_common.cc | 11 +++++++++++ src/cc/bpf_common.h | 4 ++-- src/cc/bpf_module.cc | 36 ++++++++++++++++++++++++++++++++++++ src/cc/bpf_module.h | 2 ++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/cc/bpf_common.cc b/src/cc/bpf_common.cc index 212e1ef..19f6eb1 100644 --- a/src/cc/bpf_common.cc +++ b/src/cc/bpf_common.cc @@ -193,4 +193,15 @@ int bpf_table_leaf_snprintf(void *program, size_t id, char *buf, size_t buflen, return mod->table_key_printf(id, buf, buflen, leaf); } +int bpf_table_key_sscanf(void *program, size_t id, const char *buf, void *key) { + auto mod = static_cast(program); + if (!mod) return 0; + return mod->table_key_scanf(id, buf, key); +} +int bpf_table_leaf_sscanf(void *program, size_t id, const char *buf, void *leaf) { + auto mod = static_cast(program); + if (!mod) return 0; + return mod->table_key_scanf(id, buf, leaf); +} + } diff --git a/src/cc/bpf_common.h b/src/cc/bpf_common.h index c483f38..f791036 100644 --- a/src/cc/bpf_common.h +++ b/src/cc/bpf_common.h @@ -50,8 +50,8 @@ size_t bpf_table_leaf_size(void *program, const char *table_name); size_t bpf_table_leaf_size_id(void *program, size_t id); int bpf_table_key_snprintf(void *program, size_t id, char *buf, size_t buflen, const void *key); int bpf_table_leaf_snprintf(void *program, size_t id, char *buf, size_t buflen, const void *leaf); -//int bpf_table_key_sscanf(void *program, size_t id, const char *buf, void *key); -//int bpf_table_leaf_sscanf(void *program, size_t id, const char *buf, void *leaf); +int bpf_table_key_sscanf(void *program, size_t id, const char *buf, void *key); +int bpf_table_leaf_sscanf(void *program, size_t id, const char *buf, void *leaf); int bpf_table_update(void *program, const char *table_name, const char *key, const char *leaf); int bpf_table_update_id(void *program, size_t id, const char *key, const char *leaf); diff --git a/src/cc/bpf_module.cc b/src/cc/bpf_module.cc index 0b6e02c..fa237c0 100644 --- a/src/cc/bpf_module.cc +++ b/src/cc/bpf_module.cc @@ -627,6 +627,42 @@ int BPFModule::table_leaf_printf(size_t id, char *buf, size_t buflen, const void return 0; } +int BPFModule::table_key_scanf(size_t id, const char *key_str, void *key) { + if (id >= tables_->size()) return -1; + + const TableDesc &desc = (*tables_)[id]; + if (desc.fd < 0) return -1; + + if (!rw_engine_ || !desc.key_reader) { + fprintf(stderr, "Table sscanf not available\n"); + return -1; + } + + vector args({GenericValue(), GenericValue((void *)key_str), GenericValue(key)}); + GenericValue rc = rw_engine_->runFunction(desc.key_reader, args); + if (rc.IntVal != 0) + return -1; + return 0; +} + +int BPFModule::table_leaf_scanf(size_t id, const char *leaf_str, void *leaf) { + if (id >= tables_->size()) return -1; + + const TableDesc &desc = (*tables_)[id]; + if (desc.fd < 0) return -1; + + if (!rw_engine_ || !desc.leaf_reader) { + fprintf(stderr, "Table sscanf not available\n"); + return -1; + } + + vector args({GenericValue(), GenericValue((void *)leaf_str), GenericValue(leaf)}); + GenericValue rc = rw_engine_->runFunction(desc.leaf_reader, args); + if (rc.IntVal != 0) + return -1; + return 0; +} + // load a B file, which comes in two parts int BPFModule::load_b(const string &filename, const string &proto_filename) { if (!sections_.empty()) { diff --git a/src/cc/bpf_module.h b/src/cc/bpf_module.h index 6b11ea0..ef5c2e4 100644 --- a/src/cc/bpf_module.h +++ b/src/cc/bpf_module.h @@ -72,11 +72,13 @@ class BPFModule { size_t table_key_size(size_t id) const; size_t table_key_size(const std::string &name) const; int table_key_printf(size_t id, char *buf, size_t buflen, const void *key); + int table_key_scanf(size_t id, const char *buf, void *key); const char * table_leaf_desc(size_t id) const; const char * table_leaf_desc(const std::string &name) const; size_t table_leaf_size(size_t id) const; size_t table_leaf_size(const std::string &name) const; int table_leaf_printf(size_t id, char *buf, size_t buflen, const void *leaf); + int table_leaf_scanf(size_t id, const char *buf, void *leaf); int table_update(size_t id, const char *key, const char *leaf); int table_update(const std::string &name, const char *key, const char *leaf); char * license() const; -- 2.7.4