mirror of
https://github.com/strongswan/strongswan.git
synced 2025-12-15 00:00:26 -05:00
implementation of a simple "token enumerator"
This commit is contained in:
parent
2f2b99282a
commit
fca4d3ee03
@ -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)
|
||||
|
||||
@ -16,8 +16,6 @@
|
||||
#include <library.h>
|
||||
#include <daemon.h>
|
||||
|
||||
#define countof(array) (sizeof(array)/sizeof(typeof(array[0])))
|
||||
|
||||
/*******************************************************************************
|
||||
* Base64 encoding/decoding test
|
||||
******************************************************************************/
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -89,6 +89,11 @@
|
||||
*/
|
||||
#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
|
||||
*/
|
||||
|
||||
@ -153,6 +153,145 @@ enumerator_t* enumerator_create_directory(char *path)
|
||||
return &this->public;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
@ -97,6 +97,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 :-).
|
||||
*
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user