Logo Search packages:      
Sourcecode: c-icap version File versions  Download package

int ci_format_text ( ci_request_t req_data,
const char *  fmt,
char *  buffer,
int  len,
struct ci_fmt_entry user_table 
)

Produces formated text based on template text.

Parameters:
req_dataThe current request
fmtThe format string
bufferThe output buffer
lenThe length of the output buffer
user_tableAn array of user defined directives
Returns:
Non zero on success, zero on error

This function uses the internal formating directives table. Also the user can define his own table.

Definition at line 206 of file txt_format.c.

References ci_fmt_entry::format.

{
   const char *s;
   char *b, *lb;
   struct ci_fmt_entry *fmte;
   int directive_len, val_len, remains, left_align, i;
   unsigned int width, space=0;
   char parameter[MAX_VARIABLE_SIZE];

   s = fmt;
   b = buffer;
   remains = len;
   while (*s && remains > 0) {
     if (*s == '%') {
       fmte = check_tables(s, user_table, &directive_len, 
                           &width, &left_align, parameter);
       ci_debug_printf(7,"Width: %d, Parameter:%s\n", width, parameter);
       if (width != 0) 
            space = width = (remains<width?remains:width);
       else
            space = remains;
       if(fmte != NULL) { 
            if (width) {
                if (left_align) {
                    val_len=fmte->format(req_data, b, space, parameter); 
                    b += val_len;
                    for (i=0; i < width-val_len; i++) b[i]=' ';
                    b += width-val_len;
                }
                else {
                    lb = malloc((space+1)*sizeof(char));      
                    val_len=fmte->format(req_data, lb, space, parameter);
                    for (i=0; i < width-val_len; i++) b[i]=' ';
                    b += width-val_len; 
                    for (i=0; i < val_len; i++) b[i]=lb[i];
                    b += val_len;
                }

                remains -= width;
            } 
             else {
             val_len=fmte->format(req_data, b, space, parameter);
                if (val_len > space) {
                    ci_debug_printf(1,"format_line BUG! Please contact authors!!!\n");
                    return 0;
                }
                b += val_len; 
                remains -= val_len;
             }
            s += directive_len;
       }
       else
         *b++ = *s++, remains--; 
     }
     else 
        *b++ = *s++, remains--;
   }
   *b = '\0';
   return len-remains;
}

Generated by  Doxygen 1.6.0   Back to index