Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Table output driver (column with calculator)
0003  *
0004  * @file taboutput.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038 #include <sys/param.h>
0039 
0040 #include <ndrstandard.h>
0041 #include <ndebug.h>
0042 #include <nstdutil.h>
0043 
0044 #include <ndrxdcmn.h>
0045 #include <atmi_int.h>
0046 #include <gencall.h>
0047 #include <utlist.h>
0048 #include <Exfields.h>
0049 
0050 #include "xa_cmn.h"
0051 #include "tpadmsv.h"
0052 #include <ndrx.h>
0053 #include <qcommon.h>
0054 #include <nclopt.h>
0055 #include <tpadm.h>
0056 #include <ubfutil.h>
0057 
0058 /*---------------------------Externs------------------------------------*/
0059 /*---------------------------Macros-------------------------------------*/
0060 /*---------------------------Enums--------------------------------------*/
0061 /*---------------------------Typedefs-----------------------------------*/
0062 /*---------------------------Globals------------------------------------*/
0063 /*---------------------------Statics------------------------------------*/
0064 /*---------------------------Prototypes---------------------------------*/
0065 
0066 /**
0067  * Init the grow list
0068  * @param table table of output
0069  */
0070 expublic void ndrx_tab_init(ndrx_growlist_t *table)
0071 {
0072     ndrx_growlist_init(table, 100, sizeof(ndrx_growlist_t*));
0073 }
0074 
0075 /**
0076  * Add data to column row
0077  * @param table table holder
0078  * @param col_nr column number
0079  * @param str string to add
0080  * @return EXSUCCEED/EXFAIL
0081  */
0082 expublic int ndrx_tab_add_col(ndrx_growlist_t *table, int col_nr, char *str)
0083 {
0084     ndrx_growlist_t *col_new;
0085     ndrx_growlist_t **col_cur;
0086     int ret = EXSUCCEED;
0087     char *dups;
0088     long *width;
0089     int len;
0090     
0091     if (col_nr > table->maxindexused)
0092     {
0093         col_new = NDRX_MALLOC(sizeof(ndrx_growlist_t));
0094         
0095         if (NULL==col_new)
0096         {
0097             NDRX_MALLOC_FAIL_NDRX(sizeof(ndrx_growlist_t));
0098             EXFAIL_OUT(ret);
0099         }
0100         
0101         ndrx_growlist_init(col_new, 100, sizeof(char *));
0102         
0103         if (EXSUCCEED!=ndrx_growlist_add(table, (char *)&col_new, col_nr))
0104         {
0105             NDRX_LOG(log_error, "Failed to add to growlist");
0106             EXFAIL_OUT(ret);
0107         }
0108     }
0109     
0110     col_cur = (ndrx_growlist_t **)(table->mem + sizeof(ndrx_growlist_t *)*col_nr);
0111     
0112     /* calculate width on the fly */
0113     
0114     if (EXFAIL==(*col_cur)->maxindexused)
0115     {
0116         /* this is start we store in ptr the col with -> do not interpret first val */
0117         width = (long *)0;
0118         if (EXSUCCEED!=ndrx_growlist_add(*col_cur, (char *)&width, (*col_cur)->maxindexused+1))
0119         {
0120             NDRX_LOG(log_error, "Failed to add column width entry at 0");
0121             EXFAIL_OUT(ret);
0122         }
0123     }
0124     
0125     width = (long *)((*col_cur)->mem);
0126     
0127     dups = NDRX_STRDUP(str);
0128     
0129     if (NULL==dups)
0130     {
0131         NDRX_MALLOC_FAIL_NDRX(sizeof(char *));
0132         EXFAIL_OUT(ret);
0133     }
0134     
0135     /* assume ptr is long ... */
0136     len = strlen(dups);
0137     if ( (long)*width < len)
0138     {
0139         *width = len;
0140     }
0141 
0142     if (EXSUCCEED!=ndrx_growlist_add(*col_cur, (char *)&dups, (*col_cur)->maxindexused+1))
0143     {
0144         NDRX_LOG(log_error, "Failed to add column at row %d to table", col_nr);
0145         EXFAIL_OUT(ret);
0146     }
0147         
0148 out:
0149     return ret;
0150 }
0151 
0152 /**
0153  * Print the table...
0154  * First row is column with
0155  * Second row is title
0156  * and following are data
0157  * @param table ptr to table
0158  * @param coltypes array of column types - int (numeric = 0, string = 1)
0159  */
0160 expublic void ndrx_tab_print(ndrx_growlist_t *table, ndrx_growlist_t *coltypes)
0161 {
0162     int col, row = 0;
0163     ndrx_growlist_t **col_cur;
0164     char **val_cur;
0165     long *width;
0166     long *typ; /* have some align... */
0167     int first = 0;
0168             
0169     while (1)
0170     {
0171         for (col=0; col<=table->maxindexused; col++)
0172         {
0173             
0174             col_cur = (ndrx_growlist_t **)(table->mem + sizeof(ndrx_growlist_t *)*col);
0175             typ=(long *)(coltypes->mem + sizeof(long)*col);
0176             if (row > (*col_cur)->maxindexused)
0177             {
0178                 goto out;
0179             }
0180                 
0181             val_cur = (*col_cur)->mem + row*sizeof(char *);
0182             width = (*col_cur)->mem;
0183             
0184             if (1==row)
0185             {
0186                 if (0==first)
0187                 {
0188                     if (BFLD_STRING==*typ || BFLD_CARRAY==*typ)
0189                     {
0190                         fprintf(stderr, "%-*s ", (int)*width, *val_cur);
0191                     }
0192                     else
0193                     {
0194                         fprintf(stderr, "%*s ", (int)*width, *val_cur);
0195                     }
0196                 }
0197                 else
0198                 {
0199                     int i;
0200                     for (i=0; i<(int)*width; i++)
0201                     {
0202                         fprintf(stderr, "-");
0203                     }
0204                     fprintf(stderr, " ");
0205 
0206                 }
0207                 /*print footers ... */
0208             }
0209             else if (row > 1)
0210             {
0211                 if (BFLD_STRING==*typ || BFLD_CARRAY==*typ)
0212                 {
0213                     fprintf(stdout, "%-*s ", (int)*width, *val_cur);
0214                 }
0215                 else
0216                 {
0217                     fprintf(stdout, "%*s ", (int)*width, *val_cur);
0218                 }
0219             }
0220         }
0221         
0222         if (1==row)
0223         {
0224             fprintf(stderr, "\n");
0225         }
0226         else if (row > 1)
0227         {
0228             fprintf(stdout, "\n");
0229         }
0230 
0231         if (row==1)
0232         {
0233             first++;
0234             if (first>1)
0235             {
0236                 row++;
0237             }
0238         }
0239         else
0240         {
0241             row++;
0242         }
0243     }
0244     
0245     
0246 out:
0247     
0248     return;
0249 }
0250 
0251 /**
0252  * Free up the table
0253  * @param table
0254  */
0255 expublic void ndrx_tab_free(ndrx_growlist_t *table)
0256 {
0257     int col, row = 0;
0258     ndrx_growlist_t **col_cur;
0259     char **val_cur;
0260             
0261     for (col=0; col<=table->maxindexused; col++)
0262     {
0263         col_cur = (ndrx_growlist_t **)(table->mem + sizeof(ndrx_growlist_t *)*col);
0264         
0265         for (row=1; row<=(*col_cur)->maxindexused; row++)
0266         {
0267             val_cur = (*col_cur)->mem + row*sizeof(char *);
0268             NDRX_FREE(*val_cur);
0269         }
0270         
0271         ndrx_growlist_free(*col_cur);
0272         NDRX_FREE(*col_cur);
0273     }
0274 
0275     ndrx_growlist_free(table);
0276 }
0277 
0278 /* vim: set ts=4 sw=4 et smartindent: */