gcc -c config.c; gcc config.o -o config
./config /home/jjeong/workspace/config/config.property
# UserAuthUrl
UserAuthUrl^http://192.168.1.2:8080/rcpt_auth?email=%s
#
UserAuthCompareStr
UserAuthCompareStr^verified
#
DlAuthUrl
DlAuthUrl^http://192.168.1.2:8080/dlCheck?q={filterSearch:'%s'}
#
DlAuthCompareStr
DlAuthCompareStr^SUCCESS
#
AhfServer
AhfServer^192.168.1.2
#
AhfPort
AhfPort^33000
#AhfUser
AhfUser^jjeong
#AhfPass
AhfPass^1234
#AhfDb
AhfDb^demodb
#AhfQuery
AhfQuery^SELECT * FROM event WHERE
id='%s'
#SkipDomain
SkipDomain^jjeong.com
#include <stdio.h>
#include <stdlib.h>
#include
<string.h>
#include <ctype.h>
typedef struct config
{
char userAuthUrl[256];
char
userAuthCompareStr[128];
char dlAuthUrl[256];
char
dlAuthCompareStr[128];
char ahfServer[128];
int
ahfPort;
char ahfUser[128];
char ahfPass[128];
char ahfDb[128];
char ahfQuery[128];
char
skipDomain[128];
} CONFIG;
int isBlank(char ch) {
return (
(ch==0x20) || (ch=='\t') || (ch=='\r') || (ch=='\n') );
}
char *trim( char *s ) {
char *f=s, *e=0,
*c=s;
/* 뒤쪽 공백 제거 */
e=s +(strlen(s)) -1;
while(
isBlank(*e) && s<=e) e--;
*(e+1)=0;
/* 앞쪽 공백 제거
*/
while( isBlank(*f) && f<=e) f++;
/* 공백 없는 부분 복사
*/
if (s!=f) {
while(f<=e) *(c++) = *(f++);
*c=0;
}
return s;
}
int setConfig(CONFIG *conf, char
*confFile) {
char readBuff[512];
char
varBuff[256];
char *temp;
FILE *fp;
memset(varBuff, 0, 256);
fp = fopen(confFile,
"r");
if ( fp ){
while ( !feof(fp) )
{
memset(readBuff, 0,
512);
if ( fgets(readBuff, 512, fp) == NULL )
{
continue;
}
temp =
trim(readBuff);
if ( *temp == '#' )
{
continue;
}
if ( strncasecmp(temp, "UserAuthUrl",
strlen("UserAuthUrl")) == 0 ) {
strcpy(conf->userAuthUrl, strrchr(temp,
'^')+1);
snprintf(varBuff, sizeof(varBuff),
conf->userAuthUrl,
"sadfasdfsafasfsdafsafsadf");
printf("UserAuthUrl : %s\n", varBuff);
memset(varBuff, 0, 256);
}
if ( strncasecmp(temp, "UserAuthCompareStr",
strlen("UserAuthCompareStr")) == 0 ) {
strcpy(conf->userAuthCompareStr, strrchr(temp,
'^')+1);
printf("UserAuthCompareStr : %s\n",
conf->userAuthCompareStr);
}
if ( strncasecmp(temp, "DlAuthUrl",
strlen("DlAuthUrl")) == 0 ) {
strcpy(conf->dlAuthUrl, strrchr(temp,
'^')+1);
snprintf(varBuff, sizeof(varBuff),
conf->dlAuthUrl, "mail@jjeong.com");
printf("DlAuthUrl : %s\n", varBuff);
memset(varBuff, 0, 256);
}
if ( strncasecmp(temp, "DlAuthCompareStr",
strlen("DlAuthCompareStr")) == 0 ) {
strcpy(conf->dlAuthCompareStr, strrchr(temp,
'^')+1);
if (
strstr("SUCCESS", conf->dlAuthCompareStr) )
{
printf("DlAuthCompareStr
SUCCESS\n");
}
printf("DlAuthCompareStr : %s\n",
conf->dlAuthCompareStr);
}
if ( strncasecmp(temp, "AhfServer",
strlen("AhfServer")) == 0 ) {
strcpy(conf->ahfServer, strrchr(temp,
'^')+1);
printf("AhfServer : %s\n",
conf->ahfServer);
}
if ( strncasecmp(temp, "AhfPort", strlen("AhfPort")) == 0 )
{
conf->ahfPort = atoi(strrchr(temp,
'^')+1);
printf("AhfPort : %d\n",
conf->ahfPort);
}
if ( strncasecmp(temp, "AhfUser", strlen("AhfUser")) == 0 )
{
strcpy(conf->ahfUser, strrchr(temp,
'^')+1);
printf("AhfUser : %s\n",
conf->ahfUser);
}
if ( strncasecmp(temp, "AhfPass", strlen("AhfPass")) == 0 )
{
strcpy(conf->ahfPass, strrchr(temp,
'^')+1);
printf("AhfPass : %s\n",
conf->ahfPass);
}
if ( strncasecmp(temp, "AhfDb", strlen("AhfDb")) == 0 )
{
strcpy(conf->ahfDb, strrchr(temp,
'^')+1);
printf("AhfDb : %s\n",
conf->ahfDb);
}
if
( strncasecmp(temp, "AhfQuery", strlen("AhfQuery")) == 0 )
{
strcpy(conf->ahfQuery, strrchr(temp,
'^')+1);
snprintf(varBuff, sizeof(varBuff),
conf->ahfQuery, "TEST");
printf("AhfQuery:
%s\n", varBuff);
memset(varBuff, 0,
256);
}
if (
strncasecmp(temp, "SkipDomain", strlen("SkipDomain")) == 0 )
{
strcpy(conf->skipDomain, strrchr(temp,
'^')+1);
printf("SkpDomain : %s\n",
conf->skipDomain);
}
}
} else {
}
fclose(fp);
return 1;
}
int main(int argc, char *argv[]) {
CONFIG
conf;
printf("file full path : %s\n", argv[1]);
setConfig(&conf, argv[1]);
return 1;
}
일부 코드는 레퍼런스 코드 가져다 사용하였습니다.
뭐 이런 종류의 코드는 구글링 하면 많이 나오니까.. ^^;;