Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Enduro/X Standard Configuration string handler
0003  *
0004  * @file stdcfgstr.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 
0041 #include "ndebug.h"
0042 #include "userlog.h"
0043 #include <errno.h>
0044 #include <nstdutil.h>
0045 #include <utlist.h>
0046 /*---------------------------Externs------------------------------------*/
0047 /*---------------------------Macros-------------------------------------*/
0048 /*---------------------------Enums--------------------------------------*/
0049 /*---------------------------Typedefs-----------------------------------*/
0050 /*---------------------------Globals------------------------------------*/
0051 /*---------------------------Statics------------------------------------*/
0052 /*---------------------------Prototypes---------------------------------*/
0053 
0054 /**
0055  * Tokenize settings string. Syntax is following:
0056  * setting1, setting2 setting3\tsetting=4\t,,,setting="HELLO", settingA="=HELLO",settingB"=CCC"
0057  * The parser does not recognize is the equal sign in the quotes or outside.
0058  * We assume that our settings does not contain equal sign.
0059  * We assume that after equal sign of the block, value follows.
0060  * @param input input string to parse
0061  * @param parsed 
0062  */
0063 expublic int ndrx_stdcfgstr_parse(const char *input, ndrx_stdcfgstr_t** parsed)
0064 {
0065     int ret = EXSUCCEED;
0066     int i;
0067     char *tok;
0068     char *str = NDRX_STRDUP(input);
0069     char *p;
0070     ndrx_stdcfgstr_t *list =NULL, *el=NULL;
0071     
0072 #define SEPS     " \t\n,"
0073 #define BLOCKS     "'\""
0074     
0075     if (NULL==str)
0076     {
0077         userlog("Failed to strdup!");
0078         EXFAIL_OUT(ret);
0079     }
0080     
0081     *parsed = NULL;
0082     
0083     for (tok = ndrx_strtokblk ( str, SEPS, BLOCKS), i=0; 
0084                 NULL!=tok; 
0085                 tok = ndrx_strtokblk (NULL, SEPS, BLOCKS), i++)
0086     {
0087         p = strchr(tok, '=');
0088         
0089         el = NDRX_FPMALLOC(sizeof(ndrx_stdcfgstr_t), 0);
0090         
0091         if (NULL==el)
0092         {
0093             userlog("Failed to fpmalloc %d bytes!", sizeof(ndrx_stdcfgstr_t));
0094             EXFAIL_OUT(ret);
0095         }
0096         
0097         if (NULL!=p)
0098         {
0099             *p=EXEOS;
0100             p++;
0101         }
0102         
0103         NDRX_STRCPY_SAFE(el->key, tok);
0104         el->value=NULL;
0105         
0106         if (NULL!=p)
0107         {
0108             size_t bufsz = strlen(p)+1;
0109             el->value = NDRX_FPMALLOC(bufsz, 0);
0110             
0111             if (NULL==el->value)
0112             {
0113                 userlog("Failed to fpmalloc %d bytes!", bufsz);
0114                 EXFAIL_OUT(ret);
0115             }
0116             
0117             strcpy(el->value, p);
0118         }
0119         
0120         DL_APPEND(list, el);
0121         el=NULL;
0122     }
0123     
0124 out:
0125     
0126     if (EXSUCCEED!=ret)
0127     {
0128         if (el!=NULL)
0129         {
0130             if (el->value!=NULL)
0131             {
0132                 NDRX_FPFREE(el->value);
0133             }
0134             NDRX_FPFREE(el);
0135         }
0136     }
0137 
0138     if (NULL!=str)
0139     {
0140         NDRX_FREE(str);
0141     }
0142 
0143     if (EXSUCCEED==ret)
0144     {
0145         *parsed=list;
0146     }
0147     else if (NULL!=list)
0148     {
0149         ndrx_stdcfgstr_free(list);
0150     }
0151     
0152     return ret;
0153 }
0154 
0155 /**
0156  * Delete memory used by settings parser
0157  * @param stdcfg parsed settings list
0158  */
0159 expublic void ndrx_stdcfgstr_free(ndrx_stdcfgstr_t* stdcfg)
0160 {
0161     ndrx_stdcfgstr_t *el, *elt;
0162     
0163     DL_FOREACH_SAFE(stdcfg, el, elt)
0164     {
0165         DL_DELETE(stdcfg, el);
0166         
0167         if (el->value!=NULL)
0168         {
0169             NDRX_FPFREE(el->value);
0170         }
0171         
0172         NDRX_FPFREE(el);
0173     }
0174     
0175 }
0176         
0177 /* vim: set ts=4 sw=4 et smartindent: */