Add sscanf C api for parsing key/leaf ascii to binary
authorBrenden Blanco <bblanco@plumgrid.com>
Wed, 12 Aug 2015 15:55:09 +0000 (08:55 -0700)
committerBrenden Blanco <bblanco@plumgrid.com>
Wed, 12 Aug 2015 19:08:35 +0000 (12:08 -0700)
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 <bblanco@plumgrid.com>
src/cc/bpf_common.cc
src/cc/bpf_common.h
src/cc/bpf_module.cc
src/cc/bpf_module.h

index 212e1ef..19f6eb1 100644 (file)
@@ -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);
+}
+
 }
index c483f38..f791036 100644 (file)
@@ -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);
 
index 0b6e02c..fa237c0 100644 (file)
@@ -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()) {
index 6b11ea0..ef5c2e4 100644 (file)
@@ -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;