[sr-dev] git:master: cfg framework: functions to iterate though the group instances
Miklos Tirpak
miklos at iptel.org
Fri Jun 24 15:14:50 CEST 2011
Module: sip-router
Branch: master
Commit: 62ed44d1cf6f5f2c824604bcafe54619c298ceaf
URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=62ed44d1cf6f5f2c824604bcafe54619c298ceaf
Author: Miklos Tirpak <miklos at iptel.org>
Committer: Miklos Tirpak <miklos at iptel.org>
Date: Fri Jun 3 13:38:32 2011 +0200
cfg framework: functions to iterate though the group instances
Two functions, cfg_select_first and cfg_select_next, are added
which can be used to iterate though the instances of a cfg group.
---
cfg/cfg_struct.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
cfg/cfg_struct.h | 21 +++++++++++++++++
2 files changed, 86 insertions(+), 0 deletions(-)
diff --git a/cfg/cfg_struct.c b/cfg/cfg_struct.c
index 4e247cd..6e93ca0 100644
--- a/cfg/cfg_struct.c
+++ b/cfg/cfg_struct.c
@@ -1169,3 +1169,68 @@ int cfg_reset(cfg_group_t *group)
group->name_len, group->name);
return 0;
}
+
+/* Move the group handle to the first group instance.
+ * This function together with cfg_select_next() can be used
+ * to iterate though the list of instances.
+ *
+ * Return value:
+ * -1: no group instance found
+ * 0: first group instance is successfully selected.
+ */
+int cfg_select_first(cfg_group_t *group)
+{
+ cfg_group_meta_t *meta;
+ cfg_group_inst_t *ginst;
+
+ meta = CFG_GROUP_META(cfg_local, group);
+ if (!meta || (meta->num == 0))
+ return -1;
+
+ ginst = (cfg_group_inst_t *)meta->array;
+ cfg_move_handle(group,
+ CFG_HANDLE_TO_GINST(*(group->handle)), /* the active group instance */
+ ginst);
+
+ LOG(L_DBG, "DEBUG: cfg_select_first(): group instance '%.*s[%u]' has been selected\n",
+ group->name_len, group->name, ginst->id);
+ return 0;
+}
+
+/* Move the group handle to the next group instance.
+ * This function together with cfg_select_first() can be used
+ * to iterate though the list of instances.
+ *
+ * Return value:
+ * -1: no more group instance found. Note, that the active group
+ * instance is not changed in this case.
+ * 0: the next group instance is successfully selected.
+ */
+int cfg_select_next(cfg_group_t *group)
+{
+ cfg_group_meta_t *meta;
+ cfg_group_inst_t *old_ginst, *new_ginst;
+ int size;
+
+ if (!(meta = CFG_GROUP_META(cfg_local, group)))
+ return -1;
+
+ if (!(old_ginst = CFG_HANDLE_TO_GINST(*(group->handle)) /* the active group instance */)) {
+ LOG(L_ERR, "ERROR: cfg_select_next(): No group instance is set currently. Forgot to call cfg_select_first()?\n");
+ return -1;
+ }
+
+ size = sizeof(cfg_group_inst_t) + group->size - 1;
+ if (((char *)old_ginst - (char *)meta->array)/size + 1 >= meta->num)
+ return -1; /* this is the last group instance */
+
+ new_ginst = (cfg_group_inst_t *)((char *)old_ginst + size);
+ cfg_move_handle(group,
+ old_ginst, /* the active group instance */
+ new_ginst);
+
+ LOG(L_DBG, "DEBUG: cfg_select_next(): group instance '%.*s[%u]' has been selected\n",
+ group->name_len, group->name, new_ginst->id);
+ return 0;
+}
+
diff --git a/cfg/cfg_struct.h b/cfg/cfg_struct.h
index f835a7e..2e91691 100644
--- a/cfg/cfg_struct.h
+++ b/cfg/cfg_struct.h
@@ -513,4 +513,25 @@ int cfg_select(cfg_group_t *group, unsigned int id);
/* Reset the group handle to the default, local configuration */
int cfg_reset(cfg_group_t *group);
+/* Move the group handle to the first group instance.
+ * This function together with cfg_select_next() can be used
+ * to iterate though the list of instances.
+ *
+ * Return value:
+ * -1: no group instance found
+ * 0: first group instance is successfully selected.
+ */
+int cfg_select_first(cfg_group_t *group);
+
+/* Move the group handle to the next group instance.
+ * This function together with cfg_select_first() can be used
+ * to iterate though the list of instances.
+ *
+ * Return value:
+ * -1: no more group instance found. Note, that the active group
+ * instance is not changed in this case.
+ * 0: the next group instance is successfully selected.
+ */
+int cfg_select_next(cfg_group_t *group);
+
#endif /* _CFG_STRUCT_H */
More information about the sr-dev
mailing list