Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Embed file
0003  *
0004  * @file emb.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 #include <ndrstandard.h>
0037 #include <stdlib.h>
0038 #include <string.h>
0039 #include <stdio.h>
0040 #include <limits.h>
0041 
0042 #include "ndebug.h"
0043 #include "userlog.h"
0044 #include <errno.h>
0045 /*---------------------------Externs------------------------------------*/
0046 /*---------------------------Macros-------------------------------------*/
0047 /*---------------------------Enums--------------------------------------*/
0048 /*---------------------------Typedefs-----------------------------------*/
0049 /*---------------------------Globals------------------------------------*/
0050 /*---------------------------Statics------------------------------------*/
0051 /*---------------------------Prototypes---------------------------------*/
0052 
0053 /**
0054  * Generate embed sources, in C code, header
0055  * @param in_fname input file name
0056  * @param out_fname output file name
0057  * @param out_suffix output suffix
0058  * @return EXSUCCEED/EXFAIL
0059  */
0060 expublic int ndrx_file_gen_embed(char *in_fname, char *out_fname, char *out_suffix)
0061 {
0062     int ret = EXSUCCEED;
0063     FILE *in=NULL, *out=NULL;
0064     int c;
0065     char outf[PATH_MAX+1] = {EXEOS};
0066     int counter = 0;
0067     int err;
0068     
0069     snprintf(outf, sizeof(outf), "%s.%s", out_fname, out_suffix);
0070     
0071     if (NULL==(in=fopen(in_fname, "rb")))
0072     {
0073         err = errno;
0074         fprintf(stderr, "Failed to open [%s]: %s\n", in_fname, strerror(err));
0075         NDRX_LOG(log_error, "Failed to open [%s]: %s", in_fname, strerror(err));
0076         EXFAIL_OUT(ret);
0077     }
0078     
0079     if (NULL==(out=fopen(outf, "wb")))
0080     {
0081         err = errno;
0082         fprintf(stderr, "Failed to open [%s]: %s\n", outf, strerror(err));
0083         NDRX_LOG(log_error, "Failed to open [%s]: %s", outf, strerror(err));
0084         EXFAIL_OUT(ret);
0085     }
0086     
0087     /* write header */
0088     
0089     fprintf(out, "#include <stdlib.h>\n");
0090     fprintf(out, "const char ndrx_G_resource_%s[] = {\n", out_fname);
0091     
0092     /* read  & write */
0093     while (EOF!=(c=(fgetc(in))))
0094     {
0095         counter++;
0096         
0097         fprintf(out, "0x%02x,", c);
0098         
0099         if (0 == counter % 10)
0100         {
0101             fprintf(out, "\n");
0102         }
0103         else
0104         {
0105             fprintf(out, " ");
0106         }
0107     }
0108     
0109     /* close resource */
0110     fprintf(out, "0x00\n};\n");
0111     
0112     /* write off length (with out EOS..) */
0113     fprintf(out, "const size_t ndrx_G_resource_%s_len = %d;\n", out_fname, counter);
0114     fprintf(out, "#define ndrx_G_resource_%s_len_def %d\n", out_fname, counter);
0115     
0116     /* put the EOL... (additionally so that we can easy operate resource as string) */
0117     
0118 out:
0119     
0120     if (NULL!=in)
0121     {
0122         fclose(in);
0123     }
0124     
0125     if (NULL!=out)
0126     {
0127         fclose(out);
0128     }
0129     
0130     if (EXSUCCEED!=ret) 
0131     {
0132         unlink(outf);
0133     }
0134 
0135     return ret;
0136 }
0137 
0138 /* vim: set ts=4 sw=4 et smartindent: */