[Section] Key=Value1,Value2,Value3 Key=Value1 [Section] Key=Value1,Value2
[Plugin] cpu=libmycpu.so mem=libmymem.so [logfile] common=/usr/local/sms/common.log error=/usr/local/sms/error.log
// Standard C++ Library #include <iostream> #include <string> // Standard C Library #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/types.h> using namespace std; const char *token = "="; class Config { private: FILE *fp; off_t last_seek; string last_section; int section_find; char key[80]; char value[256]; public: Config() { last_seek = 0; fp = NULL; last_section = ""; }; ~Config() { fclose(fp); } // 주어진 문자열을 Key와 Value로 나눈다. int Split(char *data) { char *sp, *ep; memset(key, 0x00, 80); memset(value, 0x00, 256); sp = strstr(data, "="); ep = strstr(data, "\n"); memcpy(key, data, sp-data); key[sp-data+1] = '\0'; memcpy(value, sp+1, (ep-sp)); key[sp-data+1] = '\0'; memcpy(value, sp+1, (ep-sp)); value[ep-sp-1] = '\0'; } // 설정파일을 open 한다. int openCfg(const char *fname) { fp = fopen(fname, "r"); if (fp == NULL) return -1; return 1; } // section이 존재하는지 검사한다. int findSection(string Section) { string SectionName; SectionName = "["+Section+"]"; char buf[256]; int rtv = 0; rewind(fp); while(fgets(buf, 255, fp)) { if (strncmp(SectionName.c_str(), buf, SectionName.length()) == 0) { last_seek = ftell(fp); rtv = 1; break; } } return rtv; } // fineSection으로 검색된 색션의 아이템 목록을 얻어온다. // 얻어온 아이템에서 Key,Value를 얻어온다. char *nextItem() { char buf[256]; while(1) { if(fgets(buf, 255, fp) == NULL) return NULL; if(buf[0] != '#') { if (strstr(buf, "=")) break; } } if(buf[0] == '[') return NULL; Split(buf); return key; } // section과, key를 이용해서 value를 얻어온다. char *getValue(string Section,char *Name) { rewind(fp); char buf[256]; int NameLen = strlen(Name); string SectionName; SectionName = "["+Section+"]"; section_find = 0; value[0] = '\0'; while(fgets(buf, 255, fp)) { if (buf[0] == '#') continue; if (section_find == 1) { if(strncmp(buf, Name, NameLen) == 0) { printf("%s", buf); Split(buf); return value; } } else if (strncmp(SectionName.c_str(), buf, Section.length()+2) == 0) { printf("Find\n"); last_seek = ftell(fp); printf("Last_seek %d\n", last_seek); section_find = 1; } } return NULL; } // Value를 얻어온다. char *getValue() { return value; } // Key를 얻어온다. char *getKey() { return key; } };
[CPU] POLL TIME=300 TRAP TIME=20 TH=95 COUNT=3 [IF] POLL TIME=300 TRAP TIME=20 INBPS TH=10,200 OUTBPS TH=10,500 INPPS TH=10,100 OUTPPS TH=10,100 [PLUGIN] CPU=libmycpu.so MEM=libmymem.so
int main(int argc, char *argv[]) { Config *agentCfg; int rtv; agentCfg = new Config(); rtv = agentCfg->openCfg("config.cfg"); if (rtv == -1) { perror("Error"); } rtv = agentCfg->findSection("PLUGIN"); if (!rtv) { return 1; } while(agentCfg->nextItem()) { printf("%s : %s\n",agentCfg->getKey(), agentCfg->getValue()); } return 0; }
Copyrights © - Joinc, All Rights Reserved. Inherited From - Yundream Rebranded By - Joonphil
소개
기능
코드
예제
변경사항
2007/7/19
Recent Posts
Archive Posts
Tags