-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathreadConfig.c
335 lines (291 loc) · 7.37 KB
/
readConfig.c
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/***************************************************
* filename:readConfig.c
* author:mkirin
* e-mail:[email protected]
* description: reading the thread config file
* 读取配置文件
*
* 1、 通过 char *GetParamValue(char *cpConfFile,
* char *cpParam, char *cpValue)
* 函数获取指定配置文件中的指定参数的值,保存
* 在cpValue中
* 2、 配置文件风格说明:
* (1) 自动忽略掉每行开头的空格
* (2) 以 # 开头的行是注释行
* (3) 不能以 = 开头
* (4) 支持行末注释
***************************************************/
#include "readConfig.h"
/* function name: GetMaxLineLen
* function parameter :
* cpFileName: the thread pool config file's full path (with filename)
* function description: to get the thread pool config file's size of the max line(获取指定文件中最长的行的长度)
* returned value: the size of max line
*/
static unsigned long GetMaxLineLen(char *cpFileName)
{
FILE *fpFile = NULL;
unsigned long ulMaxLineLen = 0;
unsigned long ulCntLineLen = 0;
char cCh = EOF;
/*打开文件*/
fpFile = fopen(cpFileName, "r");
if(NULL == fpFile)
{
return 0;
}
/*逐行读取文件,遇到'/n'结束一行*/
while(1)
{
cCh = fgetc(fpFile);
if(EOF == cCh)
{
if(ulCntLineLen > ulMaxLineLen)
{
ulMaxLineLen = ulCntLineLen;
ulCntLineLen = 0;
}
break;
}
ulCntLineLen++;
if('/n' == cCh)
{
if(ulCntLineLen > ulMaxLineLen)
{
ulMaxLineLen = ulCntLineLen;
ulCntLineLen = 0;
}
ulCntLineLen = 0;
}
}
fclose(fpFile);
return ulMaxLineLen;
}
/* function name: TrimLeft
* function parameter :
* cpString: any strings
* function description: remove the string at the beginning of the blank character(去掉字符串开头的空白字符)
* returned value: the pointer to the string
*/
static char *TrimLeft(char *cpString)
{
char *cpRet = NULL;
/*过滤空字符串*/
if('\0' == *cpString)
{
return cpString;
}
/*忽略头部的 空格 '/r' '/n' '/t'*/
cpRet = cpString;
while(' ' == *cpRet || '\t' == *cpRet || '\r' == *cpRet || '\n' == *cpRet)
{
cpRet++;
}
return cpRet;
}
/* function name: TrimRight
* function parameter :
* cpString: any strings
* function description: remove the string at the end of the blank character(去掉字符串末尾的空白字符)
* returned value: the pointer to the string
*/
static void TrimRight(char *cpString)
{
unsigned long ulStrLen = 0;
/*过滤空字符串*/
if('\0' == *cpString)
{
return;
}
/*去掉尾部的 空格 '/r' '/n' '/t'*/
ulStrLen = strlen(cpString);
while(' ' == cpString[ulStrLen - 1] || '\t' == cpString[ulStrLen - 1]
|| '\r' == cpString[ulStrLen - 1] || '\n' == cpString[ulStrLen - 1])
{
ulStrLen--;
}
cpString[ulStrLen] = '\0';
}
/* function name: TrimStr
* function parameter :
* cpString: any strings
* function description: remove the string at the beginning of the blank character
* and remove the string at the end of the blank character(去掉字符串开头和末尾的空白字符)
* returned value: the pointer to the string
*/
static char *TrimStr(char *cpString)
{
char *cpRet = NULL;
TrimRight(cpString);
cpRet = TrimLeft(cpString);
return cpRet;
}
/* function name: ParseLine
* function parameter :
* cpLine: any strings of any lines
* cpParam: The specified parameter name
* cpValue:The specified parameter value
* function description: From the value of parameter specifies the parameter name string in a given string line
(从给定行字符串中解析出指定参数名的参数值)
* returned value: the pointer to the specified parameter value
*/
static char *ParseLine(char *cpLine, char *cpParam, char *cpValue)
{
char *cpTmp = NULL;
char *cpPtr = NULL;
unsigned long ulSepLoc = 0;
/*去掉行首的空格*/
cpTmp = TrimLeft(cpLine);
/*过滤注释行,即以 # 开头的行, 第一个字符不可以是 = */
if('#' == *cpTmp || '=' == *cpTmp)
{
return NULL;
}
/*查找 # 号,移除行末注释*/
ulSepLoc = 0;
while(ulSepLoc < strlen(cpTmp))
{
if('#' == cpTmp[ulSepLoc])
{
break;
}
ulSepLoc++;
}
cpTmp[ulSepLoc] = '\0';
/*查找 = */
ulSepLoc = 0;
while(ulSepLoc < strlen(cpTmp))
{
if('=' == cpTmp[ulSepLoc])
{
break;
}
ulSepLoc++;
}
if(ulSepLoc == strlen(cpTmp))
{
return NULL;
}
/*获取参数*/
cpTmp[ulSepLoc] = '\0';
cpPtr = TrimStr(cpTmp);
if(strcmp(cpPtr, cpParam))
{
return NULL;
}
/*获取参数的值*/
cpPtr = TrimStr(cpTmp + ulSepLoc + 1);
strcpy(cpValue, cpPtr);
return cpValue;
}
/*从指定配置文件中获取指定参数名的参数值*/
/* function name: GetParamValue
* function parameter :
* cpConfFile: the config file's full path (with filename)
* cpParam: The specified parameter name
* cpValue:The specified parameter value
* function description: From the value of parameter specifies the parameter name string in a given config file
(从指定配置文件中获取指定参数名的参数值)
* returned value: the pointer to the specified parameter value
*/
char *GetParamValue(char *cpConfFile, char *cpParam, char *cpValue)
{
FILE *fpConf = NULL;
char *cpLine = NULL;
char *cpRet = NULL;
char cCh = EOF;
unsigned long ulMaxLineLen = 0;
unsigned long ulLineLen = 0;
/*获取最大行长度*/
ulMaxLineLen = GetMaxLineLen(cpConfFile);
if(0 == ulMaxLineLen)
{
return NULL;
}
/*打开文件*/
fpConf = fopen(cpConfFile, "r");
if(NULL == fpConf)
{
return NULL;
}
/*分配内存*/
cpLine = (char *)malloc(ulMaxLineLen + 1);
if(NULL == cpLine)
{
fclose(fpConf);
return NULL;
}
/*逐行读取并分析*/
ulLineLen = 0;
while(1)
{
cCh = fgetc(fpConf);
switch(cCh)
{
case '\n':
cpLine[ulLineLen++] = cCh;
cpLine[ulLineLen++] = '\0';
if(NULL != (cpRet = ParseLine(cpLine, cpParam, cpValue)))
{
break;
}
ulLineLen = 0;
break;
case EOF:
cpLine[ulLineLen++] = '\0';
if(NULL != (cpRet = ParseLine(cpLine, cpParam, cpValue)))
{
break;
}
ulLineLen = 0;
break;
default:
cpLine[ulLineLen++] = cCh;
break;
}
if(NULL != cpRet || EOF == cCh)
{
break;
}
}
/*释放内存、关闭文件*/
free(cpLine);
fclose(fpConf);
return cpRet;
}
/*
following just a test……
*/
/*
int g_def_thread_num = 0;
int g_manage_adjust_interval = 0;
int g_max_thread_num = 0;
int g_min_thread_num = 0;
int g_thread_worker_high_ratio = 0;
int g_thread_worker_low_ratio = 0;
int get_config_value(char *item)
{
char value[50] = {0};
printf("+++++%s\n",item);
if(GetParamValue("thread_pool_config.conf",item,value) == NULL)
{
return -1;
}
printf("%s = %s\n",item,value);
return atoi(value);
}
void conf_init()
{
g_max_thread_num = get_config_value("MAX_THREAD_NUM");
g_min_thread_num = get_config_value("MIN_THREAD_NUM");
g_def_thread_num = get_config_value("DEF_THREAD_NUM");
g_manage_adjust_interval = get_config_value("MANAGE_ADJUST_INTERVAL");
g_thread_worker_high_ratio = get_config_value("THREAD_WORKER_HIGH_RATIO");
g_thread_worker_low_ratio = get_config_value("THREAD_WORKER_LOW_RATIO");
}
int main()
{
conf_init();
return 0;
}
*/