Changeset 580

Show
Ignore:
Timestamp:
01/03/07 15:49:54 (2 years ago)
Author:
alo
Message:

--

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • cherokee/trunk/ChangeLog

    r578 r580  
     12007-01-03  A.D.F  <adefacc@tin.it> 
     2 
     3        * cherokee/buffer.c: 
     4        - added a few comments to cherokee_buffer_substitute_string(). 
     5 
     6        * cherokee/handler_dirlist.c: 
     7        - removed unused define DEFAULT_NAME_LEN 40; 
     8        - renamed substitute_token_guts() -> substitute_vbuf_token() 
     9          and changed it: 
     10          - added an array of two temporary buffers; 
     11          - added a parameter pidx_buf to update the index 
     12            of last content buffer to be used in next calls 
     13            as source buffer; 
     14        - added a few macros _VTMP_* to hide details about 
     15          declaration of substitution variables and repeated code. 
     16 
     17        * A few changes by Alvaro over these ones. 
     18         
    1192007-01-02  A.D.F  <adefacc@tin.it> 
    220 
  • cherokee/trunk/cherokee/buffer.c

    r578 r580  
    15591559 
    15601560 
    1561 /* Returns: 
     1561/* Substitute (substring)s found in (bufsrc) with (replacement) 
     1562 * and writes the resulting content to (bufdst). 
     1563 * NOTE: (bufdst) is written only if at least on (substring) instance 
     1564 *       is found in (bufsrc), in this case return value is ret_ok; 
     1565 *       if (substring) is NOT found in (bufsrc) then nothing is done 
     1566 *       in order to avoid an unnecessary copy of data. 
     1567 * Returns: 
    15621568 *  ret_ok          bufdst has been written with the substitution string(s) 
    15631569 *  ret_not_found   substring not found in bufsrc 
  • cherokee/trunk/cherokee/handler_dirlist.c

    r578 r580  
    5050#include "common.h" 
    5151 
    52 #define DEFAULT_NAME_LEN 40 
    53  
    5452 
    5553struct file_entry { 
     
    8482} 
    8583 
     84 
    8685static ret_t 
    8786parse_if (cherokee_buffer_t *buf, char *if_entry, cherokee_boolean_t show) 
     
    115114} 
    116115 
     116 
    117117static ret_t 
    118118parse_macros_in_buffer (cherokee_buffer_t *buf, cherokee_handler_dirlist_props_t *props) 
     
    125125        return ret_ok; 
    126126} 
     127 
    127128 
    128129static ret_t 
     
    685686 
    686687 
    687 static ret_t 
    688 substitute_token_guts (cherokee_buffer_t *buf1, cherokee_buffer_t *buf2, char *token, char token_len, char *replacement) 
    689 
    690         if (replacement == NULL) { 
    691                 return cherokee_buffer_substitute_string (buf1, buf2, token, token_len, "", 0); 
    692         } 
    693  
    694         return cherokee_buffer_substitute_string (buf1, buf2, token, token_len, replacement, strlen(replacement)); 
    695 
     688/* Substitute all instances of (token) found in vbuf[*pidx_buf]; 
     689 * if at least one (token) instance is found, then contents 
     690 * resulting from substitution(s) are copied to the buffer 
     691 * in next position of (vbuf) and (*pidx_buf) is updated to point 
     692 * to current content buffer. 
     693 * NOTE: only the first 2 positions in vbuf are used 
     694 *       (flip/flop algorithm). 
     695 */ 
     696static ret_t 
     697substitute_vbuf_token ( 
     698                cherokee_buffer_t **vbuf, size_t *pidx_buf, 
     699                char *token, int token_len, 
     700                char *replacement) 
     701
     702        ret_t ret; 
     703 
     704        if (replacement == NULL) 
     705                replacement = ""; 
     706 
     707        /* Substitute all instances of token in vbuf[*pidx_buf], 
     708         * if everything goes well then result is copied in next index position 
     709         * of vbuf[] and in this case we can increment the index position. 
     710         * NOTE: *pidx_buf ^= 1 is faster than *pidx_buf = (*pidx_buf + 1) % 2 
     711         */ 
     712        ret = cherokee_buffer_substitute_string ( 
     713                        vbuf[*pidx_buf], vbuf[*pidx_buf ^ 1], 
     714                        token, token_len, replacement, strlen(replacement)); 
     715        if (ret == ret_ok) 
     716                *pidx_buf ^= 1; 
     717 
     718        return ret; 
     719
     720 
     721 
     722/* Useful macros to declare, to initialize and to handle an array of 
     723 * two flip/flop temporary buffers (used for substitution purposes). 
     724 */ 
     725 
     726#define VTMP_INIT_SUBST(thread, vtmp, buffer_pattern) \ 
     727        vtmp[0] = THREAD_TMP_BUF1(thread);            \ 
     728        vtmp[1] = THREAD_TMP_BUF2(thread);            \ 
     729        cherokee_buffer_clean (vtmp[0]);              \ 
     730        cherokee_buffer_clean (vtmp[1]);              \ 
     731        cherokee_buffer_add_buffer (vtmp[0], (buffer_pattern)) 
     732 
     733#define VTMP_SUBSTITUTE_TOKEN(token, val)             \ 
     734        substitute_vbuf_token (vtmp, &idx_tmp, (token), sizeof(token)-1, (val)) 
     735 
    696736 
    697737static ret_t 
     
    699739{ 
    700740        cherokee_boolean_t                is_dir; 
     741        cherokee_buffer_t                *vtmp[2]; 
    701742        char                             *alt      = NULL; 
    702743        char                             *icon     = NULL; 
     
    706747        cherokee_handler_dirlist_props_t *props    = HDL_DIRLIST_PROP(dhdl); 
    707748        cherokee_thread_t                *thread   = HANDLER_THREAD(dhdl); 
    708         size_t                            idx_tmp  = 0; 
    709         cherokee_buffer_t                *vtmp[2]; 
    710  
    711 #define substitute_token(idx, token, val) \ 
    712         (idx = (substitute_token_guts(vtmp[idx], vtmp[(idx) ^ 1], token, sizeof(token)-1, (val)) == ret_ok ? (idx ^ 1) : (idx & 1))) 
    713  
    714         /* Initialize array of tmp buffers 
    715          */ 
    716         vtmp[0] = THREAD_TMP_BUF1(thread); 
    717         vtmp[1] = THREAD_TMP_BUF2(thread); 
    718  
    719         /* Clear tmp buffers. 
    720          */ 
    721         cherokee_buffer_clean(vtmp[0]); 
    722         cherokee_buffer_clean(vtmp[1]); 
    723  
    724         /* Add entry text 
    725          */ 
    726         cherokee_buffer_add_buffer (vtmp[0], &props->entry); 
    727          
     749        size_t                            idx_tmp  = 0;  
     750 
     751        /* Initialize temporary substitution buffers 
     752         */ 
     753        VTMP_INIT_SUBST (thread, vtmp, &props->entry); 
     754 
    728755        /* Add the icon 
    729756         */ 
     
    743770#endif 
    744771 
    745         substitute_token (idx_tmp, "%icon_alt%", alt); 
    746         substitute_token (idx_tmp, "%icon%", icon); 
     772        VTMP_SUBSTITUTE_TOKEN ("%icon_alt%", alt); 
     773        VTMP_SUBSTITUTE_TOKEN ("%icon%", icon); 
    747774 
    748775        /* File 
    749776         */ 
    750         substitute_token (idx_tmp, "%file_name%", name); 
    751         substitute_token (idx_tmp, "%file_link%", name); 
     777        VTMP_SUBSTITUTE_TOKEN ("%file_name%", name); 
     778        VTMP_SUBSTITUTE_TOKEN ("%file_link%", name); 
    752779 
    753780        /* Date 
     
    758785 
    759786                strftime (tmp->buf, 32, "%d-%b-%Y %H:%M", localtime(&file->stat.st_mtime)); 
    760                 substitute_token (idx_tmp, "%date%", tmp->buf); 
     787                VTMP_SUBSTITUTE_TOKEN ("%date%", tmp->buf); 
    761788        }  
    762789 
     
    765792        if (props->show_size) { 
    766793                if (is_dir) { 
    767                         substitute_token (idx_tmp, "%size_unit%", NULL); 
    768                         substitute_token (idx_tmp, "%size%", "-"); 
     794                        VTMP_SUBSTITUTE_TOKEN ("%size_unit%", NULL); 
     795                        VTMP_SUBSTITUTE_TOKEN ("%size%", "-"); 
    769796                } else { 
    770797                        char *unit; 
     
    778805                        while ((*unit >= '0')  && (*unit <= '9')) unit++; 
    779806 
    780                         substitute_token (idx_tmp, "%size_unit%", unit); 
     807                        VTMP_SUBSTITUTE_TOKEN ("%size_unit%", unit); 
    781808                        *unit = '\0'; 
    782                         substitute_token (idx_tmp, "%size%", tmp->buf); 
     809                        VTMP_SUBSTITUTE_TOKEN ("%size%", tmp->buf); 
    783810                } 
    784811        }  
     
    793820                name = (char *) (user->pw_name) ? user->pw_name : "unknown"; 
    794821 
    795                 substitute_token (idx_tmp, "%user%", name); 
     822                VTMP_SUBSTITUTE_TOKEN ("%user%", name); 
    796823        }  
    797824 
     
    805832                group = (char *) (user->gr_name) ? user->gr_name : "unknown"; 
    806833                 
    807                 substitute_token (idx_tmp, "%group%", group); 
     834                VTMP_SUBSTITUTE_TOKEN ("%group%", group); 
    808835        } 
    809836 
     
    819846render_parent_directory (cherokee_handler_dirlist_t *dhdl, cherokee_buffer_t *buffer) 
    820847{ 
     848        cherokee_buffer_t                *vtmp[2]; 
    821849        char                             *icon     = NULL; 
    822850        cherokee_icons_t                 *icons    = HANDLER_SRV(dhdl)->icons; 
    823851        cherokee_handler_dirlist_props_t *props    = HDL_DIRLIST_PROP(dhdl); 
    824         cherokee_thread_t                *thread   = HANDLER_THREAD(dhdl); 
    825         cherokee_buffer_t                *vtmp[2]; 
    826         size_t                            idx_tmp = 0; 
    827  
    828         /* Initialize array of tmp buffers 
    829          */ 
    830         vtmp[0] = THREAD_TMP_BUF1(thread); 
    831         vtmp[1] = THREAD_TMP_BUF2(thread); 
    832  
    833         /* Clear tmp buffers. 
    834          */ 
    835         cherokee_buffer_clean(vtmp[0]); 
    836         cherokee_buffer_clean(vtmp[1]); 
    837  
    838         /* Add entry text 
    839          */ 
    840         cherokee_buffer_add_buffer (vtmp[0], &props->entry); 
     852        cherokee_thread_t                *thread   = HANDLER_THREAD(dhdl); 
     853        size_t                            idx_tmp  = 0;  
     854 
     855        /* Initialize temporary substitution buffers 
     856         */ 
     857        VTMP_INIT_SUBST (thread, vtmp, &props->entry); 
    841858 
    842859#ifndef CHEROKEE_EMBEDDED 
     
    846863#endif 
    847864 
    848         substitute_token (idx_tmp, "%icon%", icon); 
    849         substitute_token (idx_tmp, "%icon_alt%", "[DIR]"); 
    850  
    851         substitute_token (idx_tmp, "%file_link%", "../"); 
    852         substitute_token (idx_tmp, "%file_name%", "Parent Directory"); 
    853  
    854         substitute_token (idx_tmp, "%date%", NULL); 
    855         substitute_token (idx_tmp, "%size_unit%", NULL); 
    856         substitute_token (idx_tmp, "%size%", "-"); 
    857         substitute_token (idx_tmp, "%user%", NULL); 
    858         substitute_token (idx_tmp, "%group%", NULL); 
     865        VTMP_SUBSTITUTE_TOKEN ("%icon%", icon); 
     866        VTMP_SUBSTITUTE_TOKEN ("%icon_alt%", "[DIR]"); 
     867 
     868        VTMP_SUBSTITUTE_TOKEN ("%file_link%", "../"); 
     869        VTMP_SUBSTITUTE_TOKEN ("%file_name%", "Parent Directory"); 
     870 
     871        VTMP_SUBSTITUTE_TOKEN ("%date%", NULL); 
     872        VTMP_SUBSTITUTE_TOKEN ("%size_unit%", NULL); 
     873        VTMP_SUBSTITUTE_TOKEN ("%size%", "-"); 
     874        VTMP_SUBSTITUTE_TOKEN ("%user%", NULL); 
     875        VTMP_SUBSTITUTE_TOKEN ("%group%", NULL); 
    859876 
    860877        /* Add final result to buffer 
     
    867884 
    868885static ret_t 
    869 render_header_footer_vbles (cherokee_handler_dirlist_t *dhdl, cherokee_buffer_t *buffer, cherokee_buffer_t *bufpattern) 
    870 
    871         cherokee_thread_t                *thread   = HANDLER_THREAD(dhdl); 
    872         cherokee_buffer_t                *vtmp[2]; 
    873         size_t                            idx_tmp = 0; 
    874  
    875         /* Initialize array of tmp buffers 
    876          */ 
    877         vtmp[0] = THREAD_TMP_BUF1(thread); 
    878         vtmp[1] = THREAD_TMP_BUF2(thread); 
    879  
    880         /* Clear tmp buffers. 
    881          */ 
    882         cherokee_buffer_clean(vtmp[0]); 
    883         cherokee_buffer_clean(vtmp[1]); 
    884  
    885         /* Add entry text 
    886          */ 
    887         cherokee_buffer_add_buffer (vtmp[0], bufpattern); 
     886render_header_footer_vbles (cherokee_handler_dirlist_t *dhdl, cherokee_buffer_t *buffer, cherokee_buffer_t *buf_pattern) 
     887
     888        cherokee_buffer_t  *vtmp[2]; 
     889        cherokee_thread_t  *thread   = HANDLER_THREAD(dhdl); 
     890        size_t              idx_tmp  = 0;  
     891 
     892        /* Initialize temporary substitution buffers 
     893         */ 
     894        VTMP_INIT_SUBST (thread, vtmp, buf_pattern); 
    888895 
    889896        /* Public dir 
    890897         */ 
    891         substitute_token (idx_tmp, "%public_dir%", dhdl->public_dir.buf); 
     898        VTMP_SUBSTITUTE_TOKEN ("%public_dir%", dhdl->public_dir.buf); 
    892899 
    893900        /* Server software 
    894901         */ 
    895         substitute_token (idx_tmp, "%server_software%", dhdl->software_str_ref->buf); 
     902        VTMP_SUBSTITUTE_TOKEN ("%server_software%", dhdl->software_str_ref->buf); 
    896903 
    897904        /* Notice 
    898905         */ 
    899         substitute_token (idx_tmp, "%notice%", dhdl->header.buf); 
     906        VTMP_SUBSTITUTE_TOKEN ("%notice%", dhdl->header.buf); 
    900907 
    901908        /* Orders 
    902909         */ 
    903         substitute_token (idx_tmp, "%order_name%",  
    904                        (dhdl->sort == Name_Down) ? "N" : "n"); 
    905         substitute_token (idx_tmp, "%order_size%",  
    906                        (dhdl->sort == Size_Down) ? "S" : "s"); 
    907         substitute_token (idx_tmp, "%order_date%",  
    908                        (dhdl->sort == Date_Down) ? "D" : "d"); 
     910        VTMP_SUBSTITUTE_TOKEN ("%order_name%", (dhdl->sort == Name_Down) ? "N" : "n"); 
     911        VTMP_SUBSTITUTE_TOKEN ("%order_size%", (dhdl->sort == Size_Down) ? "S" : "s"); 
     912        VTMP_SUBSTITUTE_TOKEN ("%order_date%", (dhdl->sort == Date_Down) ? "D" : "d"); 
    909913 
    910914        /* Add final result to buffer