?? read_ini.c
字號:
#include<stdio.h>#include<stdlib.h>#include<string.h>/*表示類型的宏*/#define STRSTR 0#define STRINT 1#define STRFLOAT 2#define STRDOUBLE 3/*判斷空白*/#define isblank(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r')/*×INI配置文件的格式:×ini文件由多行字符串組成,這些字符可以劃分為三種類型:×(1)數據域:×為配置數據項的分類,此字符串由“【 ”開始,以“】”結束,在中括號之間的即為數據域×(2)配置項×字符串格式為:“配置項名稱 = 配置項取值”,相當于一個以“= ”分隔的固定分隔字符串×它只有2個數據域,第一個域是配置項的名稱,第二個域是配置項的值×(3)注釋行×以“# ”開始的即為注釋行 */ /* 字符串報文庫 */typedef struct STRRESVARstu{ int nAmount; /* 數據域總數 */ int nFlag; /* 0:定長域;1:分隔域 */ int *filedlen; /* 每個域的長度 */ int nCompartlen; /* 分隔符號的長度 */ char szCompart[10]; /* 分隔字符串 */ char **filedaddr; /* 指向每個域首的指針 */} STRRESVAR;typedef STRRESVAR *PSTRRESVAR;/*刪除空白字符*/char * trim_string(char* sz_dest){ int n, n_len; if (sz_dest != NULL){ /* ―――― 以下刪除字符串右邊的空格 ―――― */ for (n = strlen(sz_dest); n > 0; n--){ if (!isblank(sz_dest[n - 1])) break; } sz_dest[n] = '\0'; /* 將右邊最靠左的一個空格替換為0 */ /* ―――― 以下刪除字符串左邊的空格 ―――― */ n_len = strlen(sz_dest); for (n = 0; n < n_len; n++){ if (!isblank(sz_dest[n])) break; } strcpy(sz_dest, sz_dest + n); /* 從左邊第一個非空格起向前移動到串首既可*/ } return sz_dest;}/**/int strrespre(char *buf, PSTRRESVAR pstr_stu){ int i = 0, j = 0; char *p1, *p2; if (pstr_stu->nFlag == 0) /* 固定長度報文 */ { for (i=0; i<pstr_stu->nAmount; i++) { pstr_stu->filedaddr[i] = buf + j; j = j + pstr_stu->filedlen[i] + pstr_stu->nCompartlen; } } else /* 固定分隔報文 */ { p1 = buf; p2 = p1; while (p1 != NULL && i<= pstr_stu->nAmount) { pstr_stu->filedaddr[i] = p1; /* p1指向數據域首地址 */ p2 = strstr(p1, pstr_stu->szCompart); if (p2 == NULL) /* 最后一個數據域 */ { pstr_stu->filedlen[i] = strlen(p1); p1 = NULL; } else /* 非最后一個數據域 */ { pstr_stu->filedlen[i] = p2 - p1; /* 數據域長度 */ p1 = p2 + pstr_stu->nCompartlen; /* p1指向下一個數據域首地址 */ } i++; /* 當前解析的數據域編號 */ } } return 0;}/**/int strresvalue(char *buf, STRRESVAR Strstu, int nIndex, void *pValue, int nType){ char szStrbuf[1024]; memset(szStrbuf, 0, sizeof(szStrbuf)); /* 拷貝數據域原始信息 */ strncpy(szStrbuf, Strstu.filedaddr[nIndex],Strstu.filedlen[nIndex]); trim_string(szStrbuf); /* 清除原始數據首尾的空格 */ /* 轉化數據類型并回傳數據信息 */ switch (nType) { case STRINT : /* 整型 */ *(int *)pValue = atoi(szStrbuf); break; case STRDOUBLE: /* 浮點型 */ *(double *)pValue = atoi(szStrbuf); break; default: /* 缺省為字符型 */ strcpy((char *)pValue, szStrbuf); break; } return 0;}/*解析配置項字符串,*//**配置項字符串是一個以“=”分隔的,共有2個數據域的固定分隔的字符串報文,*該函數解析字符串報文并按照指定的數據類型返回配置項(第二號域)的取值*/int GetConfigStrValue(char *buf, void *p_value, int n_type){ char *n_ce_addr[2]; int n_ce_len[2]; STRRESVAR stu_str; stu_str.nAmount = 2; /* 數據域總數 */ stu_str.nFlag = 1; /* 固定分隔 */ stu_str.filedlen = n_ce_len; /* 每個域的長度 */ stu_str.nCompartlen = 1; /* 分隔符號的長度 */ strcpy(stu_str.szCompart, "="); /* 分隔字符串 */ stu_str.filedaddr = n_ce_addr; /* 指向每個域首的指針 */ strrespre(buf, &stu_str); /* 解析字符串報文 */ strresvalue(buf, stu_str, 1, p_value, n_type); return 0; }/*從指定的配置文件中讀取指定的配置項的信息,讀取szRoot數據域中的szName配置項的配置信息,保存于pValue*/int GetConfigValue(char *sz_path, char *sz_root, char *sz_name, void *p_value, int n_type){ FILE *fp; int nFlag = 0; /* 進入數據域的標志 */ static char buf[1024], sz_root_ext[100], sz_name_ext[100]; fp = fopen(sz_path, "r"); if(fp==NULL){ perror("open file error"); return -1; } sprintf(sz_root_ext, "[%s]", sz_root); /* 數據域“[szRoot]”格式 */ sprintf(sz_name_ext, "%s=", sz_name); /* 數據項“szName=“格式 */ while (feof(fp) == 0){ memset(buf, 0, sizeof(buf)); fgets(buf, sizeof(buf), fp); /* 讀取文件中的每一行字符串 */ if (buf[0] == '#') continue; /* 注解行,跳過 */ /* 未進入數據域時,只查詢數據域行 */ if (nFlag == 0 && buf[0] != '[') continue; /* 未進入數據域,當前處于數據域行 */ else if (nFlag == 0 && buf[0] == '[') { /* 匹配當前數據域是否為所求域 */ if (strncmp(buf, sz_root_ext, strlen(sz_root_ext)) == 0) nFlag = 1; } else if (nFlag == 1 && buf[0] == '['){ break; /* 數據域遍歷完畢 */ } /* 遍歷數據域中的配置項 */ else { /* 匹配當前配置項是否為所求項 */ if (strncmp(buf, sz_name_ext, strlen(sz_name_ext)) == 0){ /* 從配置項中分解配置取值 */ GetConfigStrValue(buf, p_value, n_type) == 0; fclose(fp); return 0; } } }//end of while fclose(fp); return -1;}int main(){ int nPort; char szIp[20]; char proco[20]; GetConfigValue("config.ini", "SERVER01", "IP", szIp, STRSTR); GetConfigValue("config.ini","SERVER01", "PORT", &nPort, STRINT); GetConfigValue("config.ini","SERVER01", "PROCOTOL", proco, STRSTR); printf(" IP=%s \n PORT=%d\n PROCOTOL=%s\n", szIp, nPort,proco); getchar(); return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -