lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /************************************************************************
|
| 2 | * Id: cfg.c *
|
| 3 | * *
|
| 4 | * TR069 Project: A TR069 library in C *
|
| 5 | * Copyright (C) 2013-2014 netcwmp.netcwmp group *
|
| 6 | * *
|
| 7 | * *
|
| 8 | * Email: netcwmp ( & ) gmail dot com *
|
| 9 | * *
|
| 10 | ***********************************************************************/
|
| 11 |
|
| 12 | #include <cwmp/cwmp.h>
|
| 13 | #include <cwmp/pool.h>
|
| 14 | #include <cwmp/log.h>
|
| 15 | #include <cwmp/cfg.h>
|
| 16 | #include <ini.h>
|
| 17 |
|
| 18 |
|
| 19 | typedef struct conf_t conf_t;
|
| 20 |
|
| 21 | struct conf_t {
|
| 22 | char * filename;
|
| 23 | FILE * fd;
|
| 24 | };
|
| 25 |
|
| 26 |
|
| 27 | static conf_t * cwmp_conf_handle = NULL;
|
| 28 |
|
| 29 | int cwmp_conf_open(const char * filename)
|
| 30 | {
|
| 31 | FUNCTION_TRACE();
|
| 32 | cwmp_conf_handle = malloc(sizeof(conf_t)+1);
|
| 33 | if (!cwmp_conf_handle)
|
| 34 | {
|
| 35 | cwmp_log_error("conf malloc faild.\n");
|
| 36 | return CWMP_ERROR;
|
| 37 | }
|
| 38 | cwmp_conf_handle->filename = TRstrdup(filename);
|
| 39 | if(NULL == cwmp_conf_handle->filename)
|
| 40 | {
|
| 41 | cwmp_log_error("cwmp_conf_handle->filename is null, filename:%s", filename);
|
| 42 | return CWMP_ERROR;
|
| 43 | }
|
| 44 |
|
| 45 | return CWMP_OK;
|
| 46 | }
|
| 47 |
|
| 48 | void cwmp_conf_split(char * name, char **s , char **k)
|
| 49 | {
|
| 50 | *s = strchr(name, ':');
|
| 51 | if(*s == NULL)
|
| 52 | {
|
| 53 | k = &name;
|
| 54 | *s = "cwmp";
|
| 55 | }
|
| 56 | else
|
| 57 | {
|
| 58 | *s[0] = 0;
|
| 59 | *k = *s+1;
|
| 60 | *s = name;
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | int cwmp_conf_get(const char * key, char *value)
|
| 65 | {
|
| 66 | char * s, *k;
|
| 67 | char name[INI_BUFFERSIZE+1] = {0};
|
| 68 | //char value[INI_BUFFERSIZE] = {0};
|
| 69 | FUNCTION_TRACE();
|
| 70 | if(key == NULL)
|
| 71 | {
|
| 72 | return -1;
|
| 73 | }
|
| 74 | TRstrncpy(name, key, INI_BUFFERSIZE);
|
| 75 | cwmp_conf_split(name, &s, &k);
|
| 76 |
|
| 77 | ini_gets(s,k,NULL,value,INI_BUFFERSIZE, cwmp_conf_handle->filename);
|
| 78 | return 0;
|
| 79 | }
|
| 80 |
|
| 81 | int cwmp_conf_set(const char * key, const char * value)
|
| 82 | {
|
| 83 | char * s, *k;
|
| 84 | char name[INI_BUFFERSIZE+1] = {0};
|
| 85 | FUNCTION_TRACE();
|
| 86 | if(key == NULL)
|
| 87 | {
|
| 88 | return CWMP_ERROR;
|
| 89 | }
|
| 90 | TRstrncpy(name, key, INI_BUFFERSIZE);
|
| 91 | cwmp_conf_split(name, &s, &k);
|
| 92 |
|
| 93 | return ini_puts(s, k, value, cwmp_conf_handle->filename);
|
| 94 | }
|
| 95 |
|
| 96 | char * cwmp_conf_pool_get(pool_t * pool, const char * key)
|
| 97 | {
|
| 98 | char * s, *k;
|
| 99 | char name[INI_BUFFERSIZE+1] = {0};
|
| 100 | char value[INI_BUFFERSIZE+1] = {0};
|
| 101 | //FUNCTION_TRACE();
|
| 102 | if(key == NULL)
|
| 103 | {
|
| 104 | return NULL;
|
| 105 | }
|
| 106 | TRstrncpy(name, key, INI_BUFFERSIZE);
|
| 107 |
|
| 108 | cwmp_conf_split(name, &s, &k);
|
| 109 |
|
| 110 | ini_gets(s,k,NULL,value,INI_BUFFERSIZE, cwmp_conf_handle->filename);
|
| 111 |
|
| 112 | return pool_pstrdup(pool, value);
|
| 113 | }
|
| 114 |
|
| 115 | int cwmp_conf_get_int(const char * key)
|
| 116 | {
|
| 117 | char * s, *k;
|
| 118 | char name[INI_BUFFERSIZE+1] = {0};
|
| 119 |
|
| 120 | FUNCTION_TRACE();
|
| 121 | if(key == NULL)
|
| 122 | {
|
| 123 | cwmp_log_error("key is NULL");
|
| 124 | return 0;
|
| 125 | }
|
| 126 | TRstrncpy(name, key, INI_BUFFERSIZE);
|
| 127 | cwmp_conf_split(name, &s, &k);
|
| 128 |
|
| 129 | return (int)ini_getl(s,k,0,cwmp_conf_handle->filename);
|
| 130 | }
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|