File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / inc / aitcfg.h
Revision 1.11: download - view: text, annotated - select for diffs - revision graph
Thu May 30 09:12:27 2013 UTC (10 years, 11 months ago) by misho
Branches: MAIN
CVS tags: cfg7_2, cfg7_1, HEAD, CFG7_1, CFG7_0
version 7.0

    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.11 2013/05/30 09:12:27 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, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
   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: 	SLIST_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		*slh_first;
   66: 	struct tagCfg		*rbh_root;
   67: } cfg_root_t;
   68: #define CFG_RC_LOCK(x)		pthread_mutex_lock(&(x)->rc_mtx)
   69: #define CFG_RC_UNLOCK(x)	pthread_mutex_unlock(&(x)->rc_mtx)
   70: 
   71: 
   72: // cfg_GetErrno() Get error code of last operation
   73: int cfg_GetErrno();
   74: // cfg_GetError() Get error text of last operation
   75: const char *cfg_GetError();
   76: 
   77: 
   78: /*
   79:  * Macros for config library.
   80:  */
   81: #define CFG_ISEMPTY(x)		RB_EMPTY((x))
   82: 
   83: 
   84: /*
   85:  * cfgInitConfig() - Init config root
   86:  *
   87:  * @cfg = Config root
   88:  * return: -1 error or 0 ok
   89:  */
   90: int cfgInitConfig(cfg_root_t * __restrict cfg);
   91: /*
   92:  * cfgLoadConfig() - Load config from file
   93:  *
   94:  * @cfgName = Config filename
   95:  * @cfg = Config root
   96:  * return: -1 error or 0 ok
   97:  */
   98: int cfgLoadConfig(const char *cfgName, cfg_root_t * __restrict cfg);
   99: /*
  100:  * cfgUnloadConfig() - Unload config from memory and destroy resources
  101:  *
  102:  * @cfg = Config root
  103:  * return: none
  104:  */
  105: void cfgUnloadConfig(cfg_root_t * __restrict cfg);
  106: /*
  107:  * cfgClearConfig() - Clear config and free resources
  108:  *
  109:  * @cfg = Config root
  110:  * return: none
  111:  */
  112: void cfgClearConfig(cfg_root_t * __restrict cfg);
  113: /*
  114:  * cfgCreateConfig() - Create config file from memory
  115:  *
  116:  * @csConfigName = New config filename
  117:  * @cfg = Config root
  118:  * @whitespace = Additional whitespace characters to file
  119:  * return: -1 error or 0 ok
  120:  */
  121: int cfgCreateConfig(const char *csConfigName, cfg_root_t * __restrict cfg, 
  122: 		int whitespace);
  123: 
  124: /*
  125:  * cfgReadLines() - Read custom lines and add new item at config root
  126:  *
  127:  * @f = File resource
  128:  * @delim = Custom delimiter, if =NULL default is '='
  129:  * @end = Custom user end of file, if =NULL default is EOF
  130:  * @cfg = Config root
  131:  * return: -1 error or 0 ok
  132:  */
  133: int cfgReadLines(FILE *f, const char *delim, const char *end, 
  134: 		cfg_root_t * __restrict cfg);
  135: /*
  136:  * cfgWriteLines() - Write custom lines and export data to variable
  137:  *
  138:  * @f = File resource
  139:  * @delim = Custom delimiter, if =NULL default is '='
  140:  * @eol = End of line string, if =NULL default is "\n"
  141:  * @section = Export only section, if =NULL default is all
  142:  * @cfg = Config root
  143:  * return: =NULL error or !=NULL exported data, must be free after use with ait_freeVar()
  144:  */
  145: ait_val_t *cfgWriteLines(FILE *f, const char *delim, const char *eol, 
  146: 		const char *section, cfg_root_t * __restrict cfg);
  147: /*
  148:  * cfgReadConfig() - Read file and add new item at config root
  149:  *
  150:  * @f = File resource
  151:  * @cfg = Config root
  152:  * return: -1 error or 0 ok
  153:  */
  154: int cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg);
  155: /*
  156:  * cfgWriteConfig() - Write config from memory
  157:  *
  158:  * @f = File handle
  159:  * @cfg = Config root
  160:  * @whitespace = Additional whitespace characters to file
  161:  * return: -1 error or 0 ok
  162:  */
  163: int cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, int whitespace);
  164: /*
  165:  * cfgConcatConfig() - Concat two configs into one
  166:  *
  167:  * @cfg = Config root
  168:  * @add_cfg = Concated config will be destroy after merge
  169:  * return: -1 error or 0 ok
  170:  */
  171: int cfgConcatConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg);
  172: /*
  173:  * cfgMergeConfig() - Marge two list in one cfg and destroy add_cfg
  174:  *
  175:  * @cfg = Config root of main list
  176:  * @add_cfg = Merged config will be destroy after merge
  177:  * return: -1 error or 0 ok
  178:  */
  179: int cfgMergeConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg);
  180: 
  181: /*
  182:  * cfg_findAttribute() - Find attribute position in config file
  183:  *
  184:  * @cfg = Config root
  185:  * @csSec = Config section //[{csSec}]
  186:  * @csAttr = Config attribute //{csAttr} = ...
  187:  * return: 0 not found item, -1 error or >0 position in list
  188:  */
  189: int cfg_findAttribute(cfg_root_t * __restrict cfg, 
  190: 		const char *csSec, const char *csAttr);
  191: /*
  192:  * cfg_getAttribute() - Get item from config and return value from it
  193:  *
  194:  * @cfg = Config root
  195:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  196:  * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute
  197:  * return: NULL item not found or null parameters, !=NULL value const string
  198:  */
  199: const char *cfg_getAttribute(cfg_root_t * __restrict cfg, 
  200: 		const char *csSec, const char *csAttr);
  201: /*
  202:  * cfg_setAttribute() - Set item in config or adding new item if not exists
  203:  *
  204:  * @cfg = Config root
  205:  * @csSec = Config section //[{csSec}], if NULL set in *default* section
  206:  * @csAttr = Config attribute //{csAttr} = ...
  207:  * @csVal = Config value //... = {csVal} to setup
  208:  * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
  209:  */
  210: int cfg_setAttribute(cfg_root_t * __restrict cfg, const char *csSec, 
  211: 		const char *csAttr, const char *csVal);
  212: /*
  213:  * cfg_unsetAttribute() - Unset item from config and free resources
  214:  *
  215:  * @cfg = Config root
  216:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  217:  * @csAttr = Config attribute //{csAttr} = ...
  218:  * return: 0 item not found, -1 error or 1 removed item
  219:  */
  220: int cfg_unsetAttribute(cfg_root_t * __restrict cfg, const char *csSec, 
  221: 		const char *csAttr);
  222: 
  223: /*
  224:  * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value*
  225:  *
  226:  * @cfg = Config root
  227:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  228:  * @csAttr = Config attribute //{csAttr} = ...
  229:  * @val = Return buffer for item Value //... = {val}
  230:  * @csDefValue = *Default Value* for return in //{val}, if not found item in config
  231:  * return: 0 item not found, -1 error or >0 number of copied bytes in //{val}
  232:  */
  233: int cfg_loadAttribute(cfg_root_t * __restrict cfg, const char *csSec, 
  234: 		const char *csAttr, ait_val_t * __restrict val, const char *csDefValue);
  235: 
  236: 
  237: #endif

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