From 4ff648a9d66acdd7cecdc061a40cca8df8ef4c98 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Sun, 1 Feb 2009 21:43:04 +0000 Subject: [PATCH] + new way to retrieve integers from templates (bstr, pref...) + add logging to GetTemplateTokenNumber and GetTemplateTokenString + iterate now has three optional parameters: StartAt, StepWidth, StopAt; defaults: 0, 0, -1 * move inetconf parameters so they don't interfere with the new iterator params --- webcit/inetconf.c | 4 +- webcit/static/t/aide_inet_aliases.html | 2 +- webcit/static/t/aide_inet_clamav.html | 2 +- webcit/static/t/aide_inet_dirnames.html | 2 +- webcit/static/t/aide_inet_masqdomains.html | 2 +- webcit/static/t/aide_inet_rbldns.html | 2 +- webcit/static/t/aide_inet_smarthosts.html | 2 +- webcit/static/t/aide_inet_spamass.html | 2 +- webcit/subst.c | 129 +++++++++++++++++---- webcit/subst.h | 24 +++- 10 files changed, 137 insertions(+), 34 deletions(-) diff --git a/webcit/inetconf.c b/webcit/inetconf.c index 5e0bd5b11..1d3c68beb 100644 --- a/webcit/inetconf.c +++ b/webcit/inetconf.c @@ -197,8 +197,8 @@ HashList *GetInetConfHash(StrBuf *Target, WCTemplputParams *TP) if (WCC->InetCfg == NULL) load_inetconf(); - GetHash(WCC->InetCfg, TKEY(2), &vHash); - svprintf(HKEY("SERVCFG:INET:TYPE"), WCS_STRING, TP->Tokens->Params[2]->Start); + GetHash(WCC->InetCfg, TKEY(5), &vHash); + svprintf(HKEY("SERVCFG:INET:TYPE"), WCS_STRING, TP->Tokens->Params[5]->Start); return vHash; } diff --git a/webcit/static/t/aide_inet_aliases.html b/webcit/static/t/aide_inet_aliases.html index 3028cca30..76a2be25c 100644 --- a/webcit/static/t/aide_inet_aliases.html +++ b/webcit/static/t/aide_inet_aliases.html @@ -2,7 +2,7 @@
- +
diff --git a/webcit/static/t/aide_inet_clamav.html b/webcit/static/t/aide_inet_clamav.html index dceee25b9..2dc2aceed 100644 --- a/webcit/static/t/aide_inet_clamav.html +++ b/webcit/static/t/aide_inet_clamav.html @@ -2,7 +2,7 @@
- +
diff --git a/webcit/static/t/aide_inet_dirnames.html b/webcit/static/t/aide_inet_dirnames.html index 28470c9c8..04d28b917 100644 --- a/webcit/static/t/aide_inet_dirnames.html +++ b/webcit/static/t/aide_inet_dirnames.html @@ -2,7 +2,7 @@
- +
diff --git a/webcit/static/t/aide_inet_masqdomains.html b/webcit/static/t/aide_inet_masqdomains.html index b98f697d3..08390f4b2 100644 --- a/webcit/static/t/aide_inet_masqdomains.html +++ b/webcit/static/t/aide_inet_masqdomains.html @@ -2,7 +2,7 @@
- +
diff --git a/webcit/static/t/aide_inet_rbldns.html b/webcit/static/t/aide_inet_rbldns.html index 447dccdd9..b1f5b5fc3 100644 --- a/webcit/static/t/aide_inet_rbldns.html +++ b/webcit/static/t/aide_inet_rbldns.html @@ -2,7 +2,7 @@
- +
diff --git a/webcit/static/t/aide_inet_smarthosts.html b/webcit/static/t/aide_inet_smarthosts.html index 898067530..786d33459 100644 --- a/webcit/static/t/aide_inet_smarthosts.html +++ b/webcit/static/t/aide_inet_smarthosts.html @@ -2,7 +2,7 @@
- +
diff --git a/webcit/static/t/aide_inet_spamass.html b/webcit/static/t/aide_inet_spamass.html index e4dc5bbab..eecf25081 100644 --- a/webcit/static/t/aide_inet_spamass.html +++ b/webcit/static/t/aide_inet_spamass.html @@ -2,7 +2,7 @@
- +
diff --git a/webcit/subst.c b/webcit/subst.c index ff4afb521..394af9ab8 100644 --- a/webcit/subst.c +++ b/webcit/subst.c @@ -690,7 +690,8 @@ void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) { } } -void GetTemplateTokenString(WCTemplputParams *TP, +void GetTemplateTokenString(StrBuf *Target, + WCTemplputParams *TP, int N, const char **Value, long *len) @@ -699,8 +700,9 @@ void GetTemplateTokenString(WCTemplputParams *TP, WCTemplputParams SubTP; if (TP->Tokens->nParameters < N) { - lprintf(1, "invalid token. this shouldn't have come till here.\n"); - wc_backtrace(); + LogTemplateError(Target, + "TokenParameter", N, TP, + "invalid token %d. this shouldn't have come till here.\n", N); *Value = ""; *len = 0; return; @@ -723,8 +725,15 @@ void GetTemplateTokenString(WCTemplputParams *TP, *len = StrLength(Buf); break; case TYPE_LONG: + LogTemplateError(Target, + "TokenParameter", N, TP, + "Requesting parameter %d; of type LONG, want string.", N); + break; case TYPE_PREFINT: - break; /* todo: string to text? */ + LogTemplateError(Target, + "TokenParameter", N, TP, + "Requesting parameter %d; of type PREFINT, want string.", N); + break; case TYPE_GETTEXT: *Value = _(TP->Tokens->Params[N]->Start); *len = strlen(*Value); @@ -742,8 +751,60 @@ void GetTemplateTokenString(WCTemplputParams *TP, break; default: + LogTemplateError(Target, + "TokenParameter", N, TP, + "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type); break; -/*/todo log error */ + } +} + +long GetTemplateTokenNumber(StrBuf *Target, WCTemplputParams *TP, int N, long dflt) +{ + long Ret; + if (TP->Tokens->nParameters < N) { + LogTemplateError(Target, + "TokenParameter", N, TP, + "invalid token %d. this shouldn't have come till here.\n", N); + wc_backtrace(); + return 0; + } + + switch (TP->Tokens->Params[N]->Type) { + + case TYPE_STR: + return atol(TP->Tokens->Params[N]->Start); + break; + case TYPE_BSTR: + return LBstr(TKEY(N)); + break; + case TYPE_PREFSTR: + LogTemplateError(Target, + "TokenParameter", N, TP, + "requesting a prefstring in param %d want a number", N); + if (get_PREF_LONG(TKEY(N), &Ret, dflt)) + return Ret; + return 0; + case TYPE_LONG: + return TP->Tokens->Params[N]->lvalue; + case TYPE_PREFINT: + if (get_PREF_LONG(TKEY(N), &Ret, dflt)) + return Ret; + return 0; + case TYPE_GETTEXT: + LogTemplateError(Target, + "TokenParameter", N, TP, + "requesting a I18N string in param %d; want a number", N); + return 0; + case TYPE_SUBTEMPLATE: + LogTemplateError(Target, + "TokenParameter", N, TP, + "requesting a subtemplate in param %d; not supported for numbers", N); + return 0; + default: + LogTemplateError(Target, + "TokenParameter", N, TP, + "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type); + return 0; } } @@ -1461,14 +1522,14 @@ int EvaluateToken(StrBuf *Target, int state, WCTemplputParams *TP) case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */ if (TP->Tokens->nParameters >= 6) { if (EvaluateConditional(Target, 0, state, TP)) { - GetTemplateTokenString(TP, 5, &AppendMe, &AppendMeLen); + GetTemplateTokenString(Target, TP, 5, &AppendMe, &AppendMeLen); StrBufAppendBufPlain(Target, AppendMe, AppendMeLen, 0); } else{ - GetTemplateTokenString(TP, 4, &AppendMe, &AppendMeLen); + GetTemplateTokenString(Target, TP, 4, &AppendMe, &AppendMeLen); StrBufAppendBufPlain(Target, AppendMe, AppendMeLen, @@ -1701,6 +1762,10 @@ void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP) WCTemplputParams SubTP; IterateStruct Status; + long StartAt = 0; + long StepWidth = 0; + long StopAt = -1; + memset(&Status, 0, sizeof(IterateStruct)); memcpy (&SubTP, &TP, sizeof(WCTemplputParams)); @@ -1760,21 +1825,37 @@ void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP) SubTP.Filter.ContextType = It->ContextType; SubTP.Filter.ControlContextType = CTX_ITERATE; SubTP.ControlContext = &Status; - it = GetNewHashPos(List, 0); + + if (HAVE_PARAM(3)) { + StartAt = GetTemplateTokenNumber(Target, TP, 3, 0); + } + if (HAVE_PARAM(4)) { + StepWidth = GetTemplateTokenNumber(Target, TP, 4, 0); + } + if (HAVE_PARAM(5)) { + StopAt = GetTemplateTokenNumber(Target, TP, 5, -1); + } + if (StopAt < 0) { + StopAt = GetCount(List) + 1; + } + it = GetNewHashPos(List, StepWidth); while (GetNextHashPos(List, it, &Status.KeyLen, &Status.Key, &vContext)) { - if (DetectGroupChange && Status.n > 0) { - Status.GroupChange = (SortBy->GroupChange(vContext, vLastContext))? 1:0; - } - Status.LastN = ++Status.LastN == nMembersUsed; - SubTP.Context = vContext; - if (It->DoSubTemplate != NULL) - It->DoSubTemplate(SubBuf, &SubTP); - DoTemplate(TKEY(1), SubBuf, &SubTP); + if ((Status.n > StartAt) && (Status.n < StopAt)) { + if (DetectGroupChange && Status.n > 0) { + Status.GroupChange = (SortBy->GroupChange(vContext, vLastContext))? 1:0; + } + Status.LastN = (Status.n + 1) == nMembersUsed; + SubTP.Context = vContext; + if (It->DoSubTemplate != NULL) + It->DoSubTemplate(SubBuf, &SubTP); + DoTemplate(TKEY(1), SubBuf, &SubTP); - StrBufAppendBuf(Target, SubBuf, 0); - FlushStrBuf(SubBuf); - Status.oddeven = ! Status.oddeven; - vLastContext = vContext; + StrBufAppendBuf(Target, SubBuf, 0); + FlushStrBuf(SubBuf); + Status.oddeven = ! Status.oddeven; + vLastContext = vContext; + } + Status.n++; } FreeStrBuf(&SubBuf); DeleteHashPos(&it); @@ -1924,7 +2005,7 @@ int ConditionalContextStr(StrBuf *Target, WCTemplputParams *TP) const char *CompareToken; long len; - GetTemplateTokenString(TP, 2, &CompareToken, &len); + GetTemplateTokenString(Target, TP, 2, &CompareToken, &len); return strcmp(ChrPtr(TokenText), CompareToken) == 0; } @@ -1945,7 +2026,8 @@ void tmpl_do_boxed(StrBuf *Target, WCTemplputParams *TP) else { const char *Ch; long len; - GetTemplateTokenString(TP, + GetTemplateTokenString(Target, + TP, 1, &Ch, &len); @@ -1982,7 +2064,8 @@ void tmpl_do_tabbed(StrBuf *Target, WCTemplputParams *TP) else if (TP->Tokens->Params[i * 2]->Type == TYPE_GETTEXT) { const char *Ch; long len; - GetTemplateTokenString(TP, + GetTemplateTokenString(Target, + TP, i * 2, &Ch, &len); diff --git a/webcit/subst.h b/webcit/subst.h index 4f53cc372..cc620e14f 100644 --- a/webcit/subst.h +++ b/webcit/subst.h @@ -182,7 +182,7 @@ typedef void (*HashDestructorFunc) (HashList **KillMe); extern WCTemplputParams NoCtx; - +#define HAVE_PARAM(a) (TP->Tokens->nParameters > a) #define ERR_NAME 0 @@ -214,11 +214,31 @@ void LogTemplateError (StrBuf *Target, * \param Value reference to the string of the token; don't free me. * \param len the length of Value */ -void GetTemplateTokenString(WCTemplputParams *TP, +void GetTemplateTokenString(StrBuf *Target, + WCTemplputParams *TP, int N, const char **Value, long *len); + + +/** + * \Brief get the actual integer value of a token parameter + * in your tmplputs or conditionals use this function to access parameters that can also be + * retrieved from dynamic facilities: + * _ -> Gettext; retrieve this token from the i18n facilities + * : -> lookup a setting of that name + * B -> bstr; an URL-Parameter + * = -> subtemplate; parse a template by this name, and treat its content as this tokens value + * + * \param N which token do you want to lookup? + * \param dflt default value to be retrieved if not found in preferences + * \returns the long value + */ +long GetTemplateTokenNumber(StrBuf *Target, + WCTemplputParams *TP, + int N, long dflt); + /** * \Brief put a token value into the template * use this function to append your strings into a Template. -- 2.30.2