File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / inc / aitcfg.h
Revision 1.15: download - view: text, annotated - select for diffs - revision graph
Wed Mar 19 18:38:43 2014 UTC (10 years, 2 months ago) by misho
Branches: MAIN
CVS tags: cfg7_6, HEAD, CFG7_5
version 7.5

    1: /*************************************************************************
    2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: aitcfg.h,v 1.15 2014/03/19 18:38:43 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004 - 2014
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #ifndef __AITCFG_H
   47: #define __AITCFG_H
   48: 
   49: 
   50: #include <pthread.h>
   51: #include <elwix.h>
   52: 
   53: 
   54: struct tagCfg {
   55: 	ait_val_t		cfg_sec;
   56: 	ait_val_t		cfg_attr;
   57: 	ait_val_t		cfg_val;
   58: 
   59: 	TAILQ_ENTRY(tagCfg)	cfg_next;
   60: 	RB_ENTRY(tagCfg)	cfg_node;
   61: };
   62: typedef struct tagRC {
   63: 	pthread_mutex_t		rc_mtx;
   64: 
   65: 	struct tagCfg		*tqh_first;
   66: 	struct tagCfg		**tqh_last;
   67: 	struct tagCfg		*rbh_root;
   68: } cfg_root_t;
   69: #define CFG_RC_LOCK(x)		pthread_mutex_lock(&(x)->rc_mtx)
   70: #define CFG_RC_UNLOCK(x)	pthread_mutex_unlock(&(x)->rc_mtx)
   71: 
   72: #define CFG_SECTION(x)		(assert((x)), AIT_GET_LIKE(&((struct tagCfg*) (x))->cfg_sec, const char*))
   73: #define CFG_ATTRIBUTE(x)	(assert((x)), AIT_GET_LIKE(&((struct tagCfg*) (x))->cfg_attr, const char*))
   74: #define CFG_VALUE(x)		(assert((x)), AIT_GET_LIKE(&((struct tagCfg*) (x))->cfg_val, const char*))
   75: 
   76: 
   77: // cfg_GetErrno() Get error code of last operation
   78: int cfg_GetErrno();
   79: // cfg_GetError() Get error text of last operation
   80: const char *cfg_GetError();
   81: 
   82: 
   83: /*
   84:  * Macros for config library.
   85:  */
   86: #define CFG_ISEMPTY(x)		RB_EMPTY((x))
   87: 
   88: 
   89: /*
   90:  * cfgInitConfig() - Init config root
   91:  *
   92:  * return: NULL error or !=NULL allocated config root
   93:  */
   94: cfg_root_t *cfgInitConfig();
   95: /*
   96:  * cfgEndConfig() - Free resources & config root
   97:  *
   98:  * @pcfg = Config root
   99:  * return: none
  100:  */
  101: void cfgEndConfig(cfg_root_t **pcfg);
  102: /*
  103:  * cfgLoadConfig() - Load config from file
  104:  *
  105:  * @cfgName = Config filename
  106:  * @cfg = Config root
  107:  * return: -1 error or 0 ok
  108:  */
  109: int cfgLoadConfig(const char *cfgName, cfg_root_t * __restrict cfg);
  110: /*
  111:  * cfgUnloadConfig() - Unload config from memory and destroy resources
  112:  *
  113:  * @cfg = Config root
  114:  * return: none
  115:  */
  116: void cfgUnloadConfig(cfg_root_t * __restrict cfg);
  117: /*
  118:  * cfgClearConfig() - Clear config and free resources
  119:  *
  120:  * @cfg = Config root
  121:  * return: none
  122:  */
  123: void cfgClearConfig(cfg_root_t * __restrict cfg);
  124: /*
  125:  * cfgCreateConfig() - Create config file from memory
  126:  *
  127:  * @csConfigName = New config filename
  128:  * @cfg = Config root
  129:  * @whitespace = Additional whitespace characters to file
  130:  * return: -1 error or 0 ok
  131:  */
  132: int cfgCreateConfig(const char *csConfigName, cfg_root_t * __restrict cfg, 
  133: 		int whitespace);
  134: 
  135: /*
  136:  * cfgReadLines() - Read custom lines and add new item at config root
  137:  *
  138:  * @f = File resource
  139:  * @delim = Custom delimiter, if =NULL default is '='
  140:  * @end = Custom user end of file, if =NULL default is EOF
  141:  * @cfg = Config root
  142:  * return: -1 error or 0 ok
  143:  */
  144: int cfgReadLines(FILE *f, const char *delim, const char *end, 
  145: 		cfg_root_t * __restrict cfg);
  146: /*
  147:  * cfgWriteLines() - Write custom lines and export data to variable
  148:  *
  149:  * @f = File resource
  150:  * @delim = Custom delimiter, if =NULL default is '='
  151:  * @eol = End of line string, if =NULL default is "\n"
  152:  * @section = Export only section, if =NULL default is all
  153:  * @cfg = Config root
  154:  * return: =NULL error or !=NULL exported data, must be free after use with ait_freeVar()
  155:  */
  156: ait_val_t *cfgWriteLines(FILE *f, const char *delim, const char *eol, 
  157: 		const char *section, cfg_root_t * __restrict cfg);
  158: /*
  159:  * cfgReadConfig() - Read file and add new item at config root
  160:  *
  161:  * @f = File resource
  162:  * @cfg = Config root
  163:  * return: -1 error or 0 ok
  164:  */
  165: int cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg);
  166: /*
  167:  * cfgWriteConfig() - Write config from memory
  168:  *
  169:  * @f = File handle
  170:  * @cfg = Config root
  171:  * @whitespace = Additional whitespace characters to file
  172:  * return: -1 error or 0 ok
  173:  */
  174: int cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, int whitespace);
  175: /*
  176:  * cfgConcatConfig() - Concat two configs into one
  177:  *
  178:  * @cfg = Config root
  179:  * @add_cfg = Concated config will be destroy after merge
  180:  * return: -1 error or 0 ok
  181:  */
  182: int cfgConcatConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg);
  183: /*
  184:  * cfgMergeConfig() - Marge two list in one cfg and destroy add_cfg
  185:  *
  186:  * @cfg = Config root of main list
  187:  * @add_cfg = Merged config will be destroy after merge
  188:  * return: -1 error or 0 ok
  189:  */
  190: int cfgMergeConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg);
  191: 
  192: /*
  193:  * cfg_getSection() - Get entire section attributes into array
  194:  *
  195:  * @cfg = Config root
  196:  * @csSec = Config section //[{csSec}]
  197:  * return: NULL not found or !=NULL allocated array, must free with array_Destroy() after use!
  198:  */
  199: array_t *cfg_getSection(cfg_root_t * __restrict cfg, const char *csSec);
  200: /*
  201:  * cfg_findAttribute() - Find attribute position in config file
  202:  *
  203:  * @cfg = Config root
  204:  * @csSec = Config section //[{csSec}]
  205:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
  206:  * return: 0 not found item, -1 error or >0 position in list
  207:  */
  208: int cfg_findAttribute(cfg_root_t * __restrict cfg, 
  209: 		const char *csSec, const char *csAttr);
  210: /*
  211:  * cfg_getAttribute() - Get item from config and return value from it
  212:  *
  213:  * @cfg = Config root
  214:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  215:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
  216:  * return: NULL item not found or null parameters, !=NULL value const string
  217:  */
  218: const char *cfg_getAttribute(cfg_root_t * __restrict cfg, 
  219: 		const char *csSec, const char *csAttr);
  220: /*
  221:  * cfg_setAttribute() - Set item in config or adding new item if not exists
  222:  *
  223:  * @cfg = Config root
  224:  * @csSec = Config section //[{csSec}], if NULL set in *default* section
  225:  * @csAttr = Config attribute //{csAttr} = ...
  226:  * @csVal = Config value //... = {csVal} to setup
  227:  * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
  228:  */
  229: int cfg_setAttribute(cfg_root_t * __restrict cfg, const char *csSec, 
  230: 		const char *csAttr, const char *csVal);
  231: /*
  232:  * cfg_unsetAttribute() - Unset item from config and free resources
  233:  *
  234:  * @cfg = Config root
  235:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  236:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
  237:  * return: 0 item not found, -1 error or 1 removed item
  238:  */
  239: int cfg_unsetAttribute(cfg_root_t * __restrict cfg, const char *csSec, 
  240: 		const char *csAttr);
  241: 
  242: /*
  243:  * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value*
  244:  *
  245:  * @cfg = Config root
  246:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  247:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
  248:  * @val = Return buffer for item Value //... = {val}
  249:  * @csDefValue = *Default Value* for return in //{val}, if not found item in config
  250:  * return: 0 item not found, -1 error or >0 number of copied bytes in //{val}
  251:  */
  252: int cfg_loadAttribute(cfg_root_t * __restrict cfg, const char *csSec, 
  253: 		const char *csAttr, ait_val_t * __restrict val, const char *csDefValue);
  254: 
  255: 
  256: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>