implementation of a simple "token enumerator"
authorMartin Willi <martin@strongswan.org>
Wed, 2 Jul 2008 08:09:07 +0000 (08:09 -0000)
committerMartin Willi <martin@strongswan.org>
Wed, 2 Jul 2008 08:09:07 +0000 (08:09 -0000)
src/charon/plugins/unit_tester/tests.h
src/charon/plugins/unit_tester/tests/test_chunk.c
src/charon/plugins/unit_tester/tests/test_enumerator.c
src/libstrongswan/utils.h
src/libstrongswan/utils/enumerator.c
src/libstrongswan/utils/enumerator.h

index dd3878c..6826fab 100644 (file)
@@ -24,6 +24,7 @@ DEFINE_TEST("linked_list_t->remove()", test_list_remove, FALSE)
 DEFINE_TEST("simple enumerator", test_enumerate, FALSE)
 DEFINE_TEST("nested enumerator", test_enumerate_nested, FALSE)
 DEFINE_TEST("filtered enumerator", test_enumerate_filtered, FALSE)
+DEFINE_TEST("token enumerator", test_enumerate_token, TRUE)
 DEFINE_TEST("auth info", test_auth_info, FALSE)
 DEFINE_TEST("FIPS PRF", fips_prf_test, FALSE)
 DEFINE_TEST("CURL get", test_curl_get, FALSE)
@@ -34,5 +35,5 @@ DEFINE_TEST("RSA key generation", test_rsa_gen, FALSE)
 DEFINE_TEST("RSA subjectPublicKeyInfo loading", test_rsa_load_any, FALSE)
 DEFINE_TEST("Mediation database key fetch", test_med_db, FALSE)
 DEFINE_TEST("AES-128 encryption", test_aes128, FALSE)
-DEFINE_TEST("AES-XCBC", test_aes_xcbc, TRUE)
+DEFINE_TEST("AES-XCBC", test_aes_xcbc, FALSE)
 DEFINE_TEST("Base64 converter", test_chunk_base64, FALSE)
index e7a8586..5356c1d 100644 (file)
@@ -16,8 +16,6 @@
 #include <library.h>
 #include <daemon.h>
 
-#define countof(array) (sizeof(array)/sizeof(typeof(array[0])))
-
 /*******************************************************************************
  * Base64 encoding/decoding test
  ******************************************************************************/
index d17d62b..a7f3dd8 100644 (file)
@@ -212,3 +212,53 @@ bool test_enumerate_filtered()
        list->destroy(list);
        return !bad_data;
 }
+
+/*******************************************************************************
+ * token parser test
+ ******************************************************************************/
+bool test_enumerate_token()
+{
+       enumerator_t *enumerator;
+       char *token;
+       int i, num;
+       struct {
+               char *string;
+               char *sep;
+               char *trim;
+       } tests[] = {
+               {"abc, cde, efg", ",", " "},
+               {" abc 1:2 cde;3  4efg5.  ", ":;.,", " 12345"},
+               {"abc.cde,efg", ",.", ""},
+               {"  abc   cde  efg  ", " ", " "},
+       };
+       
+       for (num = 0; num < countof(tests); num++)
+       {
+               i = 0;
+               enumerator = enumerator_create_token(
+                                                       tests[num].string, tests[num].sep, tests[num].trim);
+               while (enumerator->enumerate(enumerator, &token))
+               {
+                       switch (i)
+                       {
+                               case 0:
+                                       if (!streq(token, "abc")) return FALSE;
+                                       break;
+                               case 1:
+                                       if (!streq(token, "cde")) return FALSE;
+                                       break;
+                               case 2:
+                                       if (!streq(token, "efg")) return FALSE;
+                                       break;
+                               default:
+                                       return FALSE;
+                       }
+                       i++;
+               }
+               enumerator->destroy(enumerator);
+       }
+
+       return TRUE;
+}
+
index 8b1a8aa..2669be6 100644 (file)
 #define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
 
 /**
+ * Get the number of elements in an array
+ */
+#define countof(array) (sizeof(array)/sizeof(array[0]))
+
+/**
  * Assign a function as a class method
  */
 #define ASSIGN(method, function) (method = (typeof(method))function)
index 67d0c8d..1d1c5e9 100644 (file)
@@ -154,6 +154,145 @@ enumerator_t* enumerator_create_directory(char *path)
 }
 
 /**
+ * Enumerator implementation for directory enumerator
+ */
+typedef struct {
+       /** implements enumerator_t */
+       enumerator_t public;
+       /** string to parse */
+       char *string;
+       /** current position */
+       char *pos;
+       /** separater chars */
+       char *sep;
+       /** trim chars */
+       char *trim;
+} token_enum_t;
+
+/**
+ * Implementation of enumerator_create_token().destroy
+ */
+static void destroy_token_enum(token_enum_t *this)
+{
+       free(this->string);
+       free(this);
+}
+
+/**
+ * Implementation of enumerator_create_token().enumerate
+ */
+static bool enumerate_token_enum(token_enum_t *this, char **token)
+{
+       char *pos = NULL, *tmp, *sep, *trim;
+       bool last = FALSE;
+       
+       /* trim leading characters/separators */
+       while (*this->pos)
+       {
+               trim = this->trim;
+               while (*trim)
+               {
+                       if (*trim == *this->pos)
+                       {
+                               this->pos++;
+                               break;
+                       }
+                       trim++;
+               }
+               sep = this->sep;
+               while (*sep)
+               {
+                       if (*sep == *this->pos)
+                       {
+                               this->pos++;
+                               break;
+                       }
+                       sep++;
+               }
+               if (!*trim && !*sep)
+               {
+                       break;
+               }
+       }
+       
+       /* find separators */
+       sep = this->sep;
+       while (*sep)
+       {
+               tmp = strchr(this->pos, *sep);
+               if (tmp && (pos == NULL || tmp < pos))
+               {
+                       pos = tmp;
+               }
+               sep++;
+       }
+       *token = this->pos;
+       if (pos)
+       {
+               *pos = '\0';
+               this->pos = pos + 1;
+       }
+       else
+       {
+               last = TRUE;
+               pos = this->pos = strchr(this->pos, '\0');
+       }
+       
+       /* trim trailing characters/separators */
+       pos--;
+       while (pos >= *token)
+       {
+               trim = this->trim;
+               while (*trim)
+               {
+                       if (*trim == *pos)
+                       {
+                               *(pos--) = '\0';
+                               break;
+                       }
+                       trim++;
+               }
+               sep = this->sep;
+               while (*sep)
+               {
+                       if (*sep == *pos)
+                       {
+                               *(pos--) = '\0';
+                               break;
+                       }
+                       sep++;
+               }
+               if (!*trim && !*sep)
+               {
+                       break;
+               }
+       }
+       
+       if (!last || pos > *token)
+       {
+               return TRUE;
+       }
+       return FALSE;
+}
+
+/**
+ * See header
+ */
+enumerator_t* enumerator_create_token(char *string, char *sep, char *trim)
+{
+       token_enum_t *enumerator = malloc_thing(token_enum_t);
+       
+       enumerator->public.enumerate = (void*)enumerate_token_enum;
+       enumerator->public.destroy = (void*)destroy_token_enum;
+       enumerator->string = strdup(string);
+       enumerator->pos = enumerator->string;
+       enumerator->sep = sep;
+       enumerator->trim = trim;
+       
+       return &enumerator->public;
+}
+
+/**
  * enumerator for nested enumerations
  */
 typedef struct {
index 4de2878..ee749d3 100644 (file)
@@ -98,6 +98,19 @@ enumerator_t *enumerator_create_single(void *item, void (*cleanup)(void *item));
 enumerator_t* enumerator_create_directory(char *path);
 
 /**
+ * Create an enumerator over tokens of a string.
+ *
+ * Tokens are separated by one of the characters in sep and trimmed by the
+ * characters in trim.
+ *
+ * @param string       string to parse
+ * @param sep          separator characters
+ * @param trim         characters to trim from tokens
+ * @return                     enumerator over char* tokens
+ */
+enumerator_t* enumerator_create_token(char *string, char *sep, char *trim);
+
+/**
  * Creates an enumerator which enumerates over enumerated enumerators :-).
  * 
  * The variable argument list of enumeration values is limit to 5.