From 608046c09c66797c9068f2f741df7c4d0100c2d3 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Mon, 11 May 2009 17:18:59 +0200 Subject: [PATCH] added a Dumm template enumerator --- src/dumm/dumm.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ src/dumm/dumm.h | 7 +++++++ 2 files changed, 61 insertions(+) diff --git a/src/dumm/dumm.c b/src/dumm/dumm.c index 70f5dfb41..78b61b2a1 100644 --- a/src/dumm/dumm.c +++ b/src/dumm/dumm.c @@ -193,6 +193,59 @@ static bool load_template(private_dumm_t *this, char *dir) return TRUE; } +/** + * Template directory enumerator + */ +typedef struct { + /** implements enumerator_t */ + enumerator_t public; + /** directory enumerator */ + enumerator_t *inner; +} template_enumerator_t; + +/** + * Implementation of template_enumerator_t.enumerate + */ +static bool template_enumerate(template_enumerator_t *this, char **template) +{ + struct stat st; + char *rel; + + while (this->inner->enumerate(this->inner, &rel, NULL, &st)) + { + if (S_ISDIR(st.st_mode) && *rel != '.') + { + *template = rel; + return TRUE; + } + } + return FALSE; +} + +/** + * Implementation of template_enumerator_t.destroy + */ +static void template_enumerator_destroy(template_enumerator_t *this) +{ + this->inner->destroy(this->inner); + free(this); +} + +/** + * Implementation of dumm_t.create_template_enumerator + */ +static enumerator_t* create_template_enumerator(private_dumm_t *this) +{ + template_enumerator_t *enumerator; + + enumerator = malloc_thing(template_enumerator_t); + enumerator->public.enumerate = (void*)template_enumerate; + enumerator->public.destroy = (void*)template_enumerator_destroy; + enumerator->inner = enumerator_create_directory(TEMPLATE_DIR); + + return &enumerator->public; +} + /** * Implementation of dumm_t.destroy */ @@ -270,6 +323,7 @@ dumm_t *dumm_create(char *dir) this->public.create_bridge_enumerator = (enumerator_t*(*)(dumm_t*))create_bridge_enumerator; this->public.delete_bridge = (void(*)(dumm_t*,bridge_t*))delete_bridge; this->public.load_template = (bool(*)(dumm_t*, char *name))load_template; + this->public.create_template_enumerator = (enumerator_t*(*)(dumm_t*))create_template_enumerator; this->public.destroy = (void(*)(dumm_t*))destroy; if (dir && *dir == '/') diff --git a/src/dumm/dumm.h b/src/dumm/dumm.h index f5db0e45b..b3682595e 100644 --- a/src/dumm/dumm.h +++ b/src/dumm/dumm.h @@ -90,6 +90,13 @@ struct dumm_t { */ bool (*load_template)(dumm_t *this, char *dir); + /** + * @brief Create an enumerator over all available templates. + * + * @return enumerator over char* + */ + enumerator_t* (*create_template_enumerator)(dumm_t *this); + /** * @brief stop all guests and destroy the modeler */