Browse code

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.

Miklos Tirpak authored on 03/06/2011 11:38:32
Showing 2 changed files
... ...
@@ -1169,3 +1169,68 @@ int cfg_reset(cfg_group_t *group)
1169 1169
 			group->name_len, group->name);
1170 1170
 	return 0;
1171 1171
 }
1172
+
1173
+/* Move the group handle to the first group instance.
1174
+ * This function together with cfg_select_next() can be used
1175
+ * to iterate though the list of instances.
1176
+ *
1177
+ * Return value:
1178
+ *	-1: no group instance found
1179
+ *	 0: first group instance is successfully selected.
1180
+ */
1181
+int cfg_select_first(cfg_group_t *group)
1182
+{
1183
+	cfg_group_meta_t	*meta;
1184
+	cfg_group_inst_t	*ginst;
1185
+
1186
+	meta = CFG_GROUP_META(cfg_local, group);
1187
+	if (!meta || (meta->num == 0))
1188
+		return -1;
1189
+
1190
+	ginst = (cfg_group_inst_t *)meta->array;
1191
+	cfg_move_handle(group,
1192
+			CFG_HANDLE_TO_GINST(*(group->handle)), /* the active group instance */
1193
+			ginst);
1194
+
1195
+	LOG(L_DBG, "DEBUG: cfg_select_first(): group instance '%.*s[%u]' has been selected\n",
1196
+			group->name_len, group->name, ginst->id);
1197
+	return 0;
1198
+}
1199
+
1200
+/* Move the group handle to the next group instance.
1201
+ * This function together with cfg_select_first() can be used
1202
+ * to iterate though the list of instances.
1203
+ *
1204
+ * Return value:
1205
+ *	-1: no more group instance found. Note, that the active group
1206
+ *		instance is not changed in this case.
1207
+ *	 0: the next group instance is successfully selected.
1208
+ */
1209
+int cfg_select_next(cfg_group_t *group)
1210
+{
1211
+	cfg_group_meta_t	*meta;
1212
+	cfg_group_inst_t	*old_ginst, *new_ginst;
1213
+	int	size;
1214
+
1215
+	if (!(meta = CFG_GROUP_META(cfg_local, group)))
1216
+		return -1;
1217
+
1218
+	if (!(old_ginst = CFG_HANDLE_TO_GINST(*(group->handle)) /* the active group instance */)) {
1219
+		LOG(L_ERR, "ERROR: cfg_select_next(): No group instance is set currently. Forgot to call cfg_select_first()?\n");
1220
+		return -1;
1221
+	}
1222
+
1223
+	size = sizeof(cfg_group_inst_t) + group->size - 1;
1224
+	if (((char *)old_ginst - (char *)meta->array)/size + 1 >= meta->num)
1225
+		return -1; /* this is the last group instance */
1226
+
1227
+	new_ginst = (cfg_group_inst_t *)((char *)old_ginst + size);
1228
+	cfg_move_handle(group,
1229
+			old_ginst, /* the active group instance */
1230
+			new_ginst);
1231
+
1232
+	LOG(L_DBG, "DEBUG: cfg_select_next(): group instance '%.*s[%u]' has been selected\n",
1233
+			group->name_len, group->name, new_ginst->id);
1234
+	return 0;
1235
+}
1236
+
... ...
@@ -513,4 +513,25 @@ int cfg_select(cfg_group_t *group, unsigned int id);
513 513
 /* Reset the group handle to the default, local configuration */
514 514
 int cfg_reset(cfg_group_t *group);
515 515
 
516
+/* Move the group handle to the first group instance.
517
+ * This function together with cfg_select_next() can be used
518
+ * to iterate though the list of instances.
519
+ *
520
+ * Return value:
521
+ *	-1: no group instance found
522
+ *	 0: first group instance is successfully selected.
523
+ */
524
+int cfg_select_first(cfg_group_t *group);
525
+
526
+/* Move the group handle to the next group instance.
527
+ * This function together with cfg_select_first() can be used
528
+ * to iterate though the list of instances.
529
+ *
530
+ * Return value:
531
+ *	-1: no more group instance found. Note, that the active group
532
+ *		instance is not changed in this case.
533
+ *	 0: the next group instance is successfully selected.
534
+ */
535
+int cfg_select_next(cfg_group_t *group);
536
+
516 537
 #endif /* _CFG_STRUCT_H */