* correct all GetNextHashPos() calls to have const chars
authorWilfried Göesgens <willi@citadel.org>
Sun, 17 Aug 2008 15:21:42 +0000 (15:21 +0000)
committerWilfried Göesgens <willi@citadel.org>
Sun, 17 Aug 2008 15:21:42 +0000 (15:21 +0000)
* new template callback: ITERATE("iterator", "subtemplate",...); its to iterate over registered hashlists
* new RegisterIterator() call; use this to register your hashlist with the ITERATE functionality

webcit/addressbook_popup.c
webcit/auth.c
webcit/calendar_view.c
webcit/preferences.c
webcit/siteconfig.c
webcit/subst.c
webcit/webcit.c
webcit/webcit.h
webcit/webserver.c
webcit/who.c

index 9ad632ecaa9104e7e9375016f85b7f61a8e539b3..5609c68aef980557dbe7d5c25523a6d9d4aa90ff 100644 (file)
@@ -28,6 +28,7 @@ void display_address_book_middle_div(void) {
        char buf[256];
        long len;
        char *Name;
+       const char *VCName;
        void *Namee;
        StrBuf *DefAddrBook;
        HashList *List;
@@ -67,7 +68,7 @@ void display_address_book_middle_div(void) {
 
        SortByHashKey(List, 1);
        it = GetNewHashPos();
-       while (GetNextHashPos(List, it, &len, &Name, &Namee)) {
+       while (GetNextHashPos(List, it, &len, &VCName, &Namee)) {
                wprintf("<option value=\"");
                urlescputs((char*)Namee);
                if (strcmp(ChrPtr(DefAddrBook), Namee) == 0)
@@ -108,6 +109,7 @@ void display_address_book_inner_div() {
        char target_label[64];
        long len;
        char *Name;
+       const char *VCName;
        void *Namee;
        HashList *List;
        HashPos  *it;
@@ -135,7 +137,7 @@ void display_address_book_inner_div() {
                }
                SortByHashKey(List, 1);
                it = GetNewHashPos();
-               while (GetNextHashPos(List, it, &len, &Name, &Namee)) {
+               while (GetNextHashPos(List, it, &len, &VCName, &Namee)) {
                        wprintf("<option value=\"");
                        escputs((char*)Namee);
                        wprintf("\">");
@@ -160,7 +162,7 @@ void display_address_book_inner_div() {
                }
                SortByHashKey(List, 1);
                it = GetNewHashPos();
-               while (GetNextHashPos(List, it, &len, &Name, (void**)&Namee)) {
+               while (GetNextHashPos(List, it, &len, &VCName, (void**)&Namee)) {
                        wprintf("<option value=\"");
                        escputs((char*)Namee);
                        wprintf("\">");
index 0b01a16b8ebea184aa3fec6277dbd84e832abab4..d5b77ab86f63802fc89ccdd9775ca3d445f2274d 100644 (file)
@@ -405,7 +405,7 @@ void finalize_openid_login(void)
                                urlcontent *u;
                                void *U;
                                long HKLen;
-                               char *HKey;
+                               const char *HKey;
                                HashPos *Cursor;
                                
                                Cursor = GetNewHashPos ();
index 3dcd91652654db03e65d716cdf45314580a3519d..f4de52a960e7b306663295b9e25d0bc4d199d8a4 100644 (file)
@@ -169,7 +169,7 @@ void ajax_mini_calendar(void) {
 void calendar_month_view_display_events(int year, int month, int day)
 {
        long hklen;
-       char *HashKey;
+       const char *HashKey;
        void *vCal;
        HashPos *Pos;
        disp_cal *Cal;
@@ -359,7 +359,7 @@ void calendar_month_view_display_events(int year, int month, int day)
  */
 void calendar_month_view_brief_events(time_t thetime, const char *daycolor) {
        long hklen;
-       char *HashKey;
+       const char *HashKey;
        void *vCal;
        HashPos *Pos;
        time_t event_tt;
@@ -779,7 +779,7 @@ void calendar_day_view_display_events(time_t thetime,
        int dend)
 {
        long hklen;
-       char *HashKey;
+       const char *HashKey;
        void *vCal;
        HashPos *Pos;
        icalproperty *p = NULL;
@@ -1253,7 +1253,7 @@ void calendar_day_view(int year, int month, int day) {
  */
 void calendar_summary_view(void) {
        long hklen;
-       char *HashKey;
+       const char *HashKey;
        void *vCal;
        HashPos *Pos;
        disp_cal *Cal;
@@ -1442,7 +1442,7 @@ int task_completed_cmp(const void *vtask1, const void *vtask2) {
  */
 void do_tasks_view(void) {
        long hklen;
-       char *HashKey;
+       const char *HashKey;
        void *vCal;
        disp_cal *Cal;
        HashPos *Pos;
index 0d4d4b3b34cc653c72769cb3c674accebfa27a49..ecec9e05db178afc52315bdc9d6832eb4fe5eda0 100644 (file)
@@ -167,7 +167,7 @@ void save_preferences(void) {
                HashPos *HashPos;
                HashList *Hash;
                void *Value;
-               char *Key;
+               const char *Key;
                StrBuf *Buf;
                StrBuf *SubBuf = NULL;
                
@@ -707,7 +707,7 @@ InitModule_PREFERENCES
        RegisterPreference("default_header_charset", _("Default character set for email headers:") ,PRF_STRING);
        RegisterPreference("emptyfloors", _("Show empty floors"), PRF_YESNO);
        
-       RegisterNS(HKEY("PREF:VALUE"), 1, 1, tmplput_CFG_Value);
-       RegisterNS(HKEY("PREF:DESCR"), 1, 1, tmplput_CFG_Descr);
+       RegisterNamespace("PREF:VALUE", 1, 1, tmplput_CFG_Value);
+       RegisterNamespace("PREF:DESCR", 1, 1, tmplput_CFG_Descr);
 }
 /*@}*/
index de3e640debed5661e5e81c32a5905d0030e89174..1a32942eaecb66f947e6e0ca303add2901298224 100644 (file)
@@ -16,6 +16,7 @@ void display_siteconfig(void)
        char buf[SIZ];
        int i, j;
        struct wcsession *WCC = WC;
+       const char *VCZname;
 
        char general[65536];
        char access[SIZ];
@@ -521,7 +522,7 @@ void display_siteconfig(void)
                        }
                        SortByHashKey(List, 0);
                        it = GetNewHashPos();
-                       while (GetNextHashPos(List, it, &len, &ZName, &ZNamee)) {
+                       while (GetNextHashPos(List, it, &len, &VCZname, &ZNamee)) {
                                sprintf(&general[strlen(general)], "<option %s value=\"%s\">%s</option>\n",
                                        (!strcasecmp((char*)ZName, buf) ? "selected" : ""),
                                        ZName, ZName
index d52fc4f0ac50276917594487ceb6ed14a7c91c57..cd0afa979543d81917988a297c1564d5cb18a283 100644 (file)
@@ -26,6 +26,7 @@ HashList *TemplateCache;
 HashList *LocalTemplateCache;
 
 HashList *GlobalNS;
+HashList *Iterators;
 
 typedef struct _WCTemplate {
        StrBuf *Data;
@@ -316,7 +317,7 @@ void print_value_of(const char *keyname, size_t keylen) {
 
        /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
        if (keyname[0] == '=') {
-               DoTemplate(keyname+1, keylen - 1, NULL);
+               DoTemplate(keyname+1, keylen - 1, NULL, NULL);
        }
        /** Page-local variables */
        if ((WCC->vars!= NULL) && GetHash(WCC->vars, keyname, keylen, &vVar)) {
@@ -633,7 +634,7 @@ void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
  * \brief Display a variable-substituted template
  * \param templatename template file to load
  */
-void DoTemplate(const char *templatename, long len, void *Context) 
+void DoTemplate(const char *templatename, long len, void *Context, StrBuf *Target
 {
        HashList *Static;
        HashList *StaticLocal;
@@ -769,19 +770,80 @@ void tmplput_current_room(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, vo
        escputs(WC->wc_roomname);////TODO: respcect Target
 }
 
+
+typedef struct _HashIterator {
+       HashList *StaticList;
+       RetrieveHashlistFunc GetHash;
+       HashDestructorFunc Destructor;
+       SubTemplFunc DoSubTemplate;
+} HashIterator;
+
+void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+{
+       void *vIt;
+       HashIterator *It;
+       HashList *List;
+       HashPos  *it;
+       long len; 
+       const char *Key;
+       void *vContext;
+       StrBuf *SubBuf;
+       
+       if (!GetHash(Iterators, 
+                    Tokens->Params[0]->Start,
+                    Tokens->Params[0]->len,
+                    &vIt))
+               return;
+       It = (HashIterator*) vIt;
+       if (It->StaticList == NULL)
+               List = It->GetHash();
+       else
+               List = It->StaticList;
+
+       SubBuf = NewStrBuf();
+       it = GetNewHashPos();
+       while (GetNextHashPos(List, it, &len, &Key, &vContext)) {
+               It->DoSubTemplate(SubBuf, vContext);
+               DoTemplate(Tokens->Params[0]->Start,
+                          Tokens->Params[0]->len,
+                          vContext, SubBuf);
+                       
+               StrBufAppendBuf(Target, SubBuf, 0);
+               FlushStrBuf(SubBuf);
+       }
+       DeleteHashPos(&it);
+       It->Destructor(List);
+}
+
+
+void RegisterITERATOR(const char *Name, long len, 
+                     HashList *StaticList, 
+                     RetrieveHashlistFunc GetHash, 
+                     SubTemplFunc DoSubTempl,
+                     HashDestructorFunc Destructor)
+{
+       HashIterator *It = (HashIterator*)malloc(sizeof(HashIterator));
+       It->StaticList = StaticList;
+       It->GetHash = GetHash;
+       It->DoSubTemplate = DoSubTempl;
+       It->Destructor = Destructor;
+       Put(Iterators, Name, len, It, NULL);
+}
+
 void 
 InitModule_SUBST
 (void)
 {
-       RegisterNS(HKEY("SERV_PID"), 0, 0, tmplput_serv_ip);
-       RegisterNS(HKEY("SERV_NODENAME"), 0, 0, tmplput_serv_nodename);
-       RegisterNS(HKEY("SERV_HUMANNODE"), 0, 0, tmplput_serv_humannode);
-       RegisterNS(HKEY("SERV_FQDN"), 0, 0, tmplput_serv_fqdn);
-       RegisterNS(HKEY("SERV_SOFTWARE"), 0, 0, tmmplput_serv_software);
-       RegisterNS(HKEY("SERV_REV_LEVEL"), 0, 0, tmplput_serv_rev_level);
-       RegisterNS(HKEY("SERV_BBS_CITY"), 0, 0, tmmplput_serv_bbs_city);
-       RegisterNS(HKEY("CURRENT_USER"), 0, 0, tmplput_current_user);
-       RegisterNS(HKEY("CURRENT_ROOM"), 0, 0, tmplput_current_room);
+       RegisterNamespace("SERV_PID", 0, 0, tmplput_serv_ip);
+       RegisterNamespace("SERV_NODENAME", 0, 0, tmplput_serv_nodename);
+       RegisterNamespace("SERV_HUMANNODE", 0, 0, tmplput_serv_humannode);
+       RegisterNamespace("SERV_FQDN", 0, 0, tmplput_serv_fqdn);
+       RegisterNamespace("SERV_SOFTWARE", 0, 0, tmmplput_serv_software);
+       RegisterNamespace("SERV_REV_LEVEL", 0, 0, tmplput_serv_rev_level);
+       RegisterNamespace("SERV_BBS_CITY", 0, 0, tmmplput_serv_bbs_city);
+       RegisterNamespace("CURRENT_USER", 0, 0, tmplput_current_user);
+       RegisterNamespace("CURRENT_ROOM", 0, 0, tmplput_current_room);
+       RegisterNamespace("ITERATE", 2, 4, tmpl_iterate_subtmpl);
 }
 
 /*@}*/
index 723bc0db9113acb70e403c13f887a5d24d656a7a..4f488f392a44b74e07f6cfdbd262fd6f496b2b83 100644 (file)
@@ -173,7 +173,7 @@ void dump_vars(void)
        urlcontent *u;
        void *U;
        long HKLen;
-       char *HKey;
+       const char *HKey;
        HashPos *Cursor;
        
        Cursor = GetNewHashPos ();
index 3923d5aca84ed37609c727a1471c1d5e57f4ac0b..bee4cf009d07ede55f1ad8d8ebcd625b27c29042 100644 (file)
@@ -317,8 +317,23 @@ typedef struct _TemplateToken {
 } WCTemplateToken;
 
 typedef void (*WCHandlerFunc)(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context);
+void RegisterNS(const char *NSName, long len, 
+               int nMinArgs, 
+               int nMaxArgs, 
+               WCHandlerFunc HandlerFunc);
+#define RegisterNamespace(a, b, c, d) RegisterNS(a, sizeof(a)-1, b, c, d)
+
+
+typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, void *Context);
+typedef HashList *(*RetrieveHashlistFunc)(void);
+typedef void (*HashDestructorFunc) (HashList *KillMe);
+void RegisterITERATOR(const char *Name, long len, 
+                     HashList *StaticList, 
+                     RetrieveHashlistFunc GetHash, 
+                     SubTemplFunc DoSubTempl,
+                     HashDestructorFunc Destructor);
+#define RegisterIterator(a, b, c, d) RegisterITERATOR(a, sizeof(a)-1, b, c, d)
 
-void RegisterNS(const char *NSName, long len, int nMinArgs, int nMaxArgs, WCHandlerFunc HandlerFunc);
 
 /**
  * \brief Values for wcs_type
@@ -504,6 +519,7 @@ extern HashList *WirelessLocalTemplateCache;
 extern HashList *TemplateCache;
 extern HashList *LocalTemplateCache;
 extern HashList *GlobalNS;
+extern HashList *Iterators;
 
 void InitialiseSemaphores(void);
 void begin_critical_section(int which_one);
@@ -646,8 +662,8 @@ void SVCALLBACK(char *keyname, var_callback_fptr fcn_ptr);
 void SVCallback(char *keyname, size_t keylen,  var_callback_fptr fcn_ptr);
 #define svcallback(a, b) SVCallback(a, sizeof(a) - 1, b)
 
-void DoTemplate(const char *templatename, long len, void *Context);
-#define do_template(a, b) DoTemplate(a, sizeof(a) -1, b);
+void DoTemplate(const char *templatename, long len, void *Context, StrBuf *Target);
+#define do_template(a, b) DoTemplate(a, sizeof(a) -1, b, NULL);
 
 
 int lingering_close(int fd);
index 5bb9e878181c7ab5f8741857f788f05cc1eaf2ce..32887f47c642a0a0bd0421431bb751bde7aa1c07 100644 (file)
@@ -623,6 +623,7 @@ int main(int argc, char **argv)
        LocalTemplateCache = NewHash(1, NULL);
        TemplateCache = NewHash(1, NULL);
        GlobalNS = NewHash(1, NULL);
+       Iterators = NewHash(1, NULL);
 
 
 #ifdef DBG_PRINNT_HOOKS_AT_START
@@ -953,6 +954,7 @@ void worker_entry(void)
                                DeleteHash(&WirelessLocalTemplateCache);
                                DeleteHash(&TemplateCache);
                                DeleteHash(&LocalTemplateCache);
+                               DeleteHash(&Iterators);
 #ifdef ENABLE_NLS
                                void ShutdownLocale(void);
 #endif
index d37105c2b252c525a701980cd3d466952682e80e..00a06fdb0164581a0f71301b4ef6b2f3a737e1c5 100644 (file)
@@ -117,7 +117,7 @@ void who_inner_div(void) {
        struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
        HashList *List;
        HashPos  *it;
-       char *UserName;
+       const char *UserName;
        long len;
        time_t now;
        int bg = 0;
@@ -394,7 +394,7 @@ void wholist_section(void) {
        void *VUser;
        HashList *List;
        HashPos  *it;
-       char *UserName;
+       const char *UserName;
        long len;
        char buf[SIZ];
         time_t now;