Collaboration diagram for Config File Parser:
Data Structures | |
| struct | switch_config |
| A simple file handle representing an open configuration file. More... | |
Typedefs | |
| typedef typedefSWITCH_BEGIN_EXTERN_C struct switch_config | switch_config_t |
Functions | |
| int | switch_config_open_file (switch_config_t *cfg, char *file_path) |
| Open a configuration file. | |
| void | switch_config_close_file (switch_config_t *cfg) |
| Close a previously opened configuration file. | |
| int | switch_config_next_pair (switch_config_t *cfg, char **var, char **val) |
| Retrieve next name/value pair from configuration file. | |
EXAMPLE
[category1] var1 => val1 var2 => val2 # lines that begin with # are comments #var3 => val3
| typedef typedefSWITCH_BEGIN_EXTERN_C struct switch_config switch_config_t |
Definition at line 61 of file switch_config.h.
| void switch_config_close_file | ( | switch_config_t * | cfg | ) |
Close a previously opened configuration file.
| cfg | (switch_config_t *) config handle to use |
Definition at line 91 of file switch_config.c.
Referenced by switch_config_open_file().
00092 { 00093 00094 if (cfg->file) { 00095 fclose(cfg->file); 00096 } 00097 00098 memset(cfg, 0, sizeof(*cfg)); 00099 }
| int switch_config_next_pair | ( | switch_config_t * | cfg, | |
| char ** | var, | |||
| char ** | val | |||
| ) |
Retrieve next name/value pair from configuration file.
| cfg | (switch_config_t *) config handle to use | |
| var | pointer to aim at the new variable name | |
| val | pointer to aim at the new value |
Definition at line 101 of file switch_config.c.
References switch_copy_string().
Referenced by switch_config_open_file().
00102 { 00103 int ret = 0; 00104 char *p, *end; 00105 00106 *var = *val = NULL; 00107 00108 if (!cfg->path) { 00109 return 0; 00110 } 00111 00112 for (;;) { 00113 cfg->lineno++; 00114 00115 if (!fgets(cfg->buf, sizeof(cfg->buf), cfg->file)) { 00116 ret = 0; 00117 break; 00118 } 00119 *var = cfg->buf; 00120 00121 if (**var == '[' && (end = strchr(*var, ']')) != 0) { 00122 *end = '\0'; 00123 (*var)++; 00124 if (**var == '+') { 00125 (*var)++; 00126 switch_copy_string(cfg->section, *var, sizeof(cfg->section)); 00127 cfg->sectno++; 00128 00129 if (cfg->lockto > -1 && cfg->sectno != cfg->lockto) { 00130 break; 00131 } 00132 cfg->catno = 0; 00133 cfg->lineno = 0; 00134 *var = ""; 00135 *val = ""; 00136 return 1; 00137 } else { 00138 switch_copy_string(cfg->category, *var, sizeof(cfg->category)); 00139 cfg->catno++; 00140 } 00141 continue; 00142 } 00143 00144 if (**var == '#' || **var == ';' || **var == '\n' || **var == '\r') { 00145 continue; 00146 } 00147 00148 if (!strncmp(*var, "__END__", 7)) { 00149 break; 00150 } 00151 00152 if ((end = strchr(*var, '#')) != 0 || (end = strchr(*var, ';')) != 0) { 00153 *end = '\0'; 00154 end--; 00155 } else if ((end = strchr(*var, '\n')) != 0) { 00156 if (*(end - 1) == '\r') { 00157 end--; 00158 } 00159 *end = '\0'; 00160 } 00161 00162 p = *var; 00163 while ((*p == ' ' || *p == '\t') && p != end) { 00164 *p = '\0'; 00165 p++; 00166 } 00167 *var = p; 00168 00169 if ((*val = strchr(*var, '=')) == 0) { 00170 ret = -1; 00171 /* log_printf(0, server.log, "Invalid syntax on %s: line %d\n", cfg->path, cfg->lineno); */ 00172 continue; 00173 } else { 00174 p = *val - 1; 00175 *(*val) = '\0'; 00176 (*val)++; 00177 if (*(*val) == '>') { 00178 *(*val) = '\0'; 00179 (*val)++; 00180 } 00181 00182 while ((*p == ' ' || *p == '\t') && p != *var) { 00183 *p = '\0'; 00184 p--; 00185 } 00186 00187 p = *val; 00188 while ((*p == ' ' || *p == '\t') && p != end) { 00189 *p = '\0'; 00190 p++; 00191 } 00192 *val = p; 00193 ret = 1; 00194 break; 00195 } 00196 } 00197 00198 return ret; 00199 }
| int switch_config_open_file | ( | switch_config_t * | cfg, | |
| char * | file_path | |||
| ) |
Open a configuration file.
| cfg | (switch_config_t *) config handle to use | |
| file_path | path to the file |
Definition at line 36 of file switch_config.c.
References switch_directories::conf_dir, switch_config_close_file(), switch_config_next_pair(), SWITCH_GLOBAL_dirs, switch_is_file_path(), SWITCH_PATH_SEPARATOR, switch_set_string, and switch_snprintf().
00037 { 00038 FILE *f; 00039 char *path = NULL; 00040 char path_buf[1024]; 00041 00042 if (switch_is_file_path(file_path)) { 00043 path = file_path; 00044 } else { 00045 switch_snprintf(path_buf, sizeof(path_buf), "%s%s%s", SWITCH_GLOBAL_dirs.conf_dir, SWITCH_PATH_SEPARATOR, file_path); 00046 path = path_buf; 00047 } 00048 00049 if (!path) { 00050 return 0; 00051 } 00052 00053 memset(cfg, 0, sizeof(*cfg)); 00054 cfg->lockto = -1; 00055 00056 if (!(f = fopen(path, "r"))) { 00057 if (!switch_is_file_path(file_path)) { 00058 int last = -1; 00059 char *var, *val; 00060 00061 switch_snprintf(path_buf, sizeof(path_buf), "%s%sfreeswitch.conf", SWITCH_GLOBAL_dirs.conf_dir, SWITCH_PATH_SEPARATOR); 00062 path = path_buf; 00063 00064 if ((f = fopen(path, "r")) == 0) { 00065 return 0; 00066 } 00067 00068 cfg->file = f; 00069 switch_set_string(cfg->path, path); 00070 00071 while (switch_config_next_pair(cfg, &var, &val)) { 00072 if (file_path && (cfg->sectno != last) && !strcmp(cfg->section, file_path)) { 00073 cfg->lockto = cfg->sectno; 00074 return 1; 00075 } 00076 } 00077 00078 switch_config_close_file(cfg); 00079 memset(cfg, 0, sizeof(*cfg)); 00080 return 0; 00081 } 00082 00083 return 0; 00084 } else { 00085 cfg->file = f; 00086 switch_set_string(cfg->path, path); 00087 return 1; 00088 } 00089 }
1.4.7