From: Timo Sirainen Date: Mon, 26 Jul 2004 16:39:18 +0000 (+0300) Subject: Added support for %offset.width format in variables. X-Git-Tag: 1.1.alpha1~3738 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=55de1a4d11765e795ec96fddd4858b188be4b892;p=thirdparty%2Fdovecot%2Fcore.git Added support for %offset.width format in variables. --HG-- branch : HEAD --- diff --git a/doc/variables.txt b/doc/variables.txt index 105e4886f0..9af2d633e8 100644 --- a/doc/variables.txt +++ b/doc/variables.txt @@ -22,6 +22,7 @@ You can apply a modifiers for each variable (eg. %Lp = pop3): %U - uppercase %E - escape '"', "'" and '\' characters by inserting '\' before them. -You can also limit a width of string by giving the number of max. characters -after the '%' character. For example %1u gives the first character of -username. +You can take a substring of the variable by giving optional offset followed +by '.' and width after the '%' character. For example %2u gives first two +characters of the username. %2.1u gives third character of the username. If +offset points outside the value, empty string is returned. diff --git a/src/lib/var-expand.c b/src/lib/var-expand.c index 996bc31382..ed4e0e1bff 100644 --- a/src/lib/var-expand.c +++ b/src/lib/var-expand.c @@ -23,7 +23,7 @@ void var_expand(string_t *dest, const char *str, const struct var_expand_modifier *m; const struct var_expand_table *t; const char *var; - unsigned int width; + unsigned int offset, width; const char *(*modifier)(const char *); for (; *str != '\0'; str++) { @@ -31,12 +31,26 @@ void var_expand(string_t *dest, const char *str, str_append_c(dest, *str); else { str++; + + /* [.][] */ width = 0; while (*str >= '0' && *str <= '9') { width = width*10 + (*str - '0'); str++; } + if (*str != '.') + offset = 0; + else { + offset = width; + width = 0; + str++; + while (*str >= '0' && *str <= '9') { + width = width*10 + (*str - '0'); + str++; + } + } + modifier = NULL; for (m = modifiers; m->key != '\0'; m++) { if (m->key == *str) { @@ -64,6 +78,8 @@ void var_expand(string_t *dest, const char *str, } if (var != NULL) { + for (; *var != '\0' && offset > 0; offset--) + var++; if (modifier != NULL) var = modifier(var); if (width == 0)