config.h 3.5 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _TD_CONFIG_H_
#define _TD_CONFIG_H_

#include "os.h"

#ifdef __cplusplus
extern "C" {
#endif

S
config  
Shengliang Guan 已提交
26 27
#define CFG_NAME_MAX_LEN 128

S
Shengliang Guan 已提交
28
typedef enum {
S
config  
Shengliang Guan 已提交
29 30 31 32 33 34 35
  CFG_STYPE_DEFAULT,
  CFG_STYPE_CFG_FILE,
  CFG_STYPE_ENV_FILE,
  CFG_STYPE_ENV_VAR,
  CFG_STYPE_APOLLO_URL,
  CFG_STYPE_ARG_LIST,
  CFG_STYPE_API_OPTION
S
Shengliang Guan 已提交
36
} ECfgSrcType;
S
Shengliang Guan 已提交
37 38

typedef enum {
S
Shengliang Guan 已提交
39 40
  CFG_DTYPE_NONE,
  CFG_DTYPE_BOOL,
S
Shengliang Guan 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  CFG_DTYPE_INT8,
  CFG_DTYPE_UINT16,
  CFG_DTYPE_INT32,
  CFG_DTYPE_INT64,
  CFG_DTYPE_FLOAT,
  CFG_DTYPE_STRING,
  CFG_DTYPE_IPSTR,
  CFG_DTYPE_DIR,
} ECfgDataType;

typedef enum {
  CFG_UTYPE_NONE,
  CFG_UTYPE_GB,
  CFG_UTYPE_MB,
  CFG_UTYPE_BYTE,
  CFG_UTYPE_SECOND,
S
config  
Shengliang Guan 已提交
57 58
  CFG_UTYPE_MS,
  CFG_UTYPE_PERCENT
S
Shengliang Guan 已提交
59 60
} ECfgUnitType;

S
Shengliang Guan 已提交
61
typedef struct SConfigItem {
S
config  
Shengliang Guan 已提交
62
  ECfgSrcType  stype;
S
Shengliang Guan 已提交
63 64
  ECfgUnitType utype;
  ECfgDataType dtype;
S
cfgtest  
Shengliang Guan 已提交
65
  char        *name;
S
Shengliang Guan 已提交
66 67 68 69 70 71 72 73 74 75 76
  union {
    bool     boolVal;
    int8_t   int8Val;
    uint16_t uint16Val;
    int32_t  int32Val;
    int64_t  int64Val;
    float    floatVal;
    char    *strVal;
    char    *ipstrVal;
    char    *dirVal;
  };
S
config  
Shengliang Guan 已提交
77 78 79 80 81 82 83 84 85
  union {
    int64_t minIntVal;
    double  minFloatVal;
  };
  union {
    int64_t maxIntVal;
    double  maxFloatVal;
  };

S
Shengliang Guan 已提交
86 87
} SConfigItem;

S
Shengliang Guan 已提交
88 89 90
typedef struct SConfig SConfig;

SConfig *cfgInit();
S
Shengliang Guan 已提交
91
int32_t  cfgLoad(SConfig *pConfig, ECfgSrcType cfgType, const char *sourceStr);
S
Shengliang Guan 已提交
92 93
void     cfgCleanup(SConfig *pConfig);

S
Shengliang Guan 已提交
94
int32_t      cfgGetSize(SConfig *pConfig);
S
Shengliang Guan 已提交
95 96 97
SConfigItem *cfgIterate(SConfig *pConfig, SConfigItem *pIter);
void         cfgCancelIterate(SConfig *pConfig, SConfigItem *pIter);
SConfigItem *cfgGetItem(SConfig *pConfig, const char *name);
S
Shengliang Guan 已提交
98

S
Shengliang Guan 已提交
99
int32_t cfgAddBool(SConfig *pConfig, const char *name, bool defaultVal, ECfgUnitType utype);
S
config  
Shengliang Guan 已提交
100 101 102 103 104 105 106 107 108 109
int32_t cfgAddInt8(SConfig *pConfig, const char *name, int8_t defaultVal, int64_t minval, int64_t maxval,
                   ECfgUnitType utype);
int32_t cfgAddUInt16(SConfig *pConfig, const char *name, uint16_t defaultVal, int64_t minval, int64_t maxval,
                     ECfgUnitType utype);
int32_t cfgAddInt32(SConfig *pConfig, const char *name, int32_t defaultVal, int64_t minval, int64_t maxval,
                    ECfgUnitType utype);
int32_t cfgAddInt64(SConfig *pConfig, const char *name, int64_t defaultVal, int64_t minval, int64_t maxval,
                    ECfgUnitType utype);
int32_t cfgAddFloat(SConfig *pConfig, const char *name, float defaultVal, double minval, double maxval,
                    ECfgUnitType utype);
S
Shengliang Guan 已提交
110 111 112
int32_t cfgAddString(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype);
int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype);
int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype);
S
Shengliang Guan 已提交
113

S
Shengliang Guan 已提交
114 115 116 117
const char *cfgStypeStr(ECfgSrcType type);
const char *cfgDtypeStr(ECfgDataType type);
const char *cfgUtypeStr(ECfgUnitType type);

S
Shengliang Guan 已提交
118 119 120 121 122
#ifdef __cplusplus
}
#endif

#endif /*_TD_CONFIG_H_*/