Commit f8ee5bf0 authored by Brenden Blanco's avatar Brenden Blanco

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: default avatarBrenden Blanco <bblanco@plumgrid.com>
parent f04003e1
......@@ -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<ebpf::BPFModule *>(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<ebpf::BPFModule *>(program);
if (!mod) return 0;
return mod->table_key_scanf(id, buf, leaf);
}
}
......@@ -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);
......
......@@ -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<GenericValue> 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<GenericValue> 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()) {
......
......@@ -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;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment