added a Dumm template enumerator
authorMartin Willi <martin@strongswan.org>
Mon, 11 May 2009 15:18:59 +0000 (17:18 +0200)
committerMartin Willi <martin@strongswan.org>
Mon, 11 May 2009 15:18:59 +0000 (17:18 +0200)
src/dumm/dumm.c
src/dumm/dumm.h

index 70f5dfb..78b61b2 100644 (file)
@@ -194,6 +194,59 @@ static bool load_template(private_dumm_t *this, char *dir)
 }
 
 /**
+ * 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
  */
 static void destroy(private_dumm_t *this)
@@ -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 == '/')
index f5db0e4..b368259 100644 (file)
@@ -91,6 +91,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
         */
        void (*destroy) (dumm_t *this);