Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief 'C' lang support
0003  *
0004  * @file clang.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 
0035 /*---------------------------Includes-----------------------------------*/
0036 
0037 #include <unistd.h>
0038 #include <stdio.h>
0039 #include <errno.h>
0040 #include <stdlib.h>
0041 #include <atmi.h>
0042 
0043 #include <ubf.h>
0044 #include <ferror.h>
0045 #include <fieldtable.h>
0046 #include <fdatatype.h>
0047 
0048 #include <ndrstandard.h>
0049 #include <ndebug.h>
0050 #include <errno.h>
0051 #include "viewc.h"
0052 #include <typed_view.h>
0053 /*---------------------------Externs------------------------------------*/
0054 /*---------------------------Macros-------------------------------------*/
0055 /*---------------------------Enums--------------------------------------*/
0056 /*---------------------------Typedefs-----------------------------------*/
0057 /*---------------------------Globals------------------------------------*/
0058 /*---------------------------Statics------------------------------------*/
0059 /*---------------------------Prototypes---------------------------------*/
0060 
0061 /**
0062  * Plot the C header file.
0063  * This will generate all the stuff loaded into memory, if multiple view files
0064  * loaded, then output will go to this one. This must be followed by developers.
0065  * @param basename - file name with out extension
0066  * @return EXSUCCEED/EXFAIL
0067  */
0068 expublic int ndrx_view_plot_c_header(char *outdir, char *basename)
0069 {
0070     int ret = EXSUCCEED;
0071     char  fname[NDRX_BPATH_MAX+1];
0072     FILE *f = NULL;
0073     ndrx_typedview_t * views = ndrx_view_get_handle();
0074     ndrx_typedview_t * vel, *velt;
0075     ndrx_typedview_field_t * fld;
0076     char default_null[NDRX_VIEW_NULL_LEN+32];
0077             
0078     /* we generate headers in current directoy */
0079     snprintf(fname, sizeof(fname), "%s.h", basename);
0080     
0081     NDRX_LOG(log_info, "Opening output file: [%s]", fname);
0082     
0083     if (NULL==(f=NDRX_FOPEN(fname, "w")))
0084     {
0085         NDRX_LOG(log_error, "Failed to open output file [%s]: %s", 
0086                 fname, strerror(errno));
0087         EXFAIL_OUT(ret);
0088     }
0089     
0090     /* Iterate the hash... */
0091     
0092     EXHASH_ITER(hh, views, vel, velt)
0093     {
0094         fprintf(f, "\nstruct %s {\n", vel->vname);
0095         
0096         DL_FOREACH(vel->fields, fld)
0097         {
0098             if (!fld->nullval_default)
0099             {
0100                 if (fld->nullval_quotes)
0101                 {
0102                     snprintf(default_null, sizeof(default_null), "\t/* null=\"%s\" */",
0103                         fld->nullval);
0104                 }
0105                 else
0106                 {
0107                     snprintf(default_null, sizeof(default_null), "\t/* null=%s */",
0108                         fld->nullval);
0109                 }
0110             }
0111             else
0112             {
0113                 default_null[0]=EXEOS;
0114             }
0115                     
0116             if (fld->flags & NDRX_VIEW_FLAG_ELEMCNT_IND_C)
0117             {
0118                 fprintf(f, "\tshort\tC_%s;\n", fld->cname);
0119             }
0120             
0121             if (fld->flags & NDRX_VIEW_FLAG_LEN_INDICATOR_L)
0122             {
0123                 if (fld->count > 1)
0124                 {
0125                     fprintf(f, "\tunsigned short\tL_%s[%d];\n", fld->cname, fld->count);
0126                 }
0127                 else
0128                 {
0129                     fprintf(f, "\tunsigned short\tL_%s;\n", fld->cname);
0130                 }
0131 
0132             }
0133             
0134             switch (fld->typecode_full)
0135             {
0136                 case BFLD_CHAR:
0137                     
0138                     if (fld->count > 1)
0139                     {
0140                         fprintf(f, "\tchar\t%s[%d];%s\n", fld->cname, fld->count, 
0141                                 default_null);
0142                     }
0143                     else
0144                     {
0145                         fprintf(f, "\tchar\t%s;%s\n", fld->cname,
0146                                 default_null);
0147                     }
0148                     
0149                     break;
0150                 case BFLD_INT:
0151                     if (fld->count > 1)
0152                     {
0153                         fprintf(f, "\tint\t%s[%d];%s\n", fld->cname, fld->count,
0154                                     default_null);
0155                     }
0156                     else
0157                     {
0158                         fprintf(f, "\tint\t%s;%s\n", fld->cname, default_null);
0159                     }
0160                     break;
0161                     
0162                 case BFLD_SHORT:
0163                     if (fld->count > 1)
0164                     {
0165                         fprintf(f, "\tshort\t%s[%d];%s\n", fld->cname, fld->count, 
0166                                 default_null);
0167                     }
0168                     else
0169                     {
0170                         fprintf(f, "\tshort\t%s;%s\n", fld->cname, default_null);
0171                     }
0172                     break;
0173                 case BFLD_LONG:
0174                     if (fld->count > 1)
0175                     {
0176                         fprintf(f, "\tlong\t%s[%d];%s\n", fld->cname, fld->count,
0177                                 default_null);
0178                     }
0179                     else
0180                     {
0181                         fprintf(f, "\tlong\t%s;%s\n", fld->cname, default_null);
0182                     }
0183                     
0184                     break;
0185                 case BFLD_FLOAT:
0186                     if (fld->count > 1)
0187                     {
0188                         fprintf(f, "\tfloat\t%s[%d];%s\n", fld->cname, fld->count,
0189                                 default_null);
0190                     }
0191                     else
0192                     {
0193                         fprintf(f, "\tfloat\t%s;%s\n", fld->cname, default_null);
0194                     }
0195                     break;
0196                 case BFLD_DOUBLE:
0197                     if (fld->count > 1)
0198                     {
0199                         fprintf(f, "\tdouble\t%s[%d];%s\n", fld->cname, fld->count,
0200                                 default_null);
0201                     }
0202                     else
0203                     {
0204                         fprintf(f, "\tdouble\t%s;%s\n", fld->cname,
0205                                 default_null);
0206                     }
0207                     
0208                     break;
0209                     
0210                 case BFLD_STRING:
0211                     
0212                     if (fld->count > 1)
0213                     {
0214                         fprintf(f, "\tchar\t%s[%d][%d];%s\n", fld->cname, 
0215                                 fld->count, fld->size, default_null);
0216                     }
0217                     else
0218                     {
0219                         fprintf(f, "\tchar\t%s[%d];%s\n", fld->cname, fld->size,
0220                                 default_null);
0221                     }
0222                     
0223                     break;
0224                     
0225                 case BFLD_CARRAY:
0226                     
0227                     if (fld->count > 1)
0228                     {
0229                         fprintf(f, "\tchar\t%s[%d][%d];%s\n", fld->cname, 
0230                                 fld->count, fld->size, default_null);
0231                     }
0232                     else
0233                     {
0234                         fprintf(f, "\tchar\t%s[%d];%s\n", fld->cname, fld->size,
0235                                 default_null);
0236                     }
0237                     
0238                     break;
0239             }
0240         }
0241         
0242         fprintf(f, "};\n\n");
0243     }
0244     
0245 out:
0246 
0247     if (NULL!=f)
0248     {
0249         NDRX_FCLOSE(f);
0250         f = NULL;
0251     }
0252     return ret;
0253 }
0254 /* vim: set ts=4 sw=4 et smartindent: */