Skip to content

Commit

Permalink
Add sscanf C api for parsing key/leaf ascii to binary
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Brenden Blanco committed Aug 12, 2015
1 parent f04003e commit f8ee5bf
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/cc/bpf_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
4 changes: 2 additions & 2 deletions src/cc/bpf_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
36 changes: 36 additions & 0 deletions src/cc/bpf_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
2 changes: 2 additions & 0 deletions src/cc/bpf_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f8ee5bf

Please sign in to comment.