°£´ÜÇÑ ¼³Á¤ÆÄÀÏ Àбâ ÇÔ¼ö
ÃÑ ÆäÀÌÁö ¼ö : 3224

Àüü ÇÔ¼ö/¿ë¾î»çÀü
Facebook Joinc ±×·ì   Joinc QA »çÀÌÆ®
ÇöÀçÀ§Ä¡ : Code>C++>Simple_Config_Reader



joinc´Â Firefox¿Í chrome¿¡¼­ Å×½ºÆ® Çß½À´Ï´Ù. IE¿¡¼­´Â Å×À̺íÀÌ ±úÁö°Å³ª À̹ÌÁö°¡ º¸ÀÌÁö ¾ÊÀ» ¼ö ÀÖ½À´Ï´Ù. ƯÈ÷ ±¸±Û DocsÀ̹ÌÁöÀÇ °æ¿ì ¿¢¹Úó¸®µÉ ¼ö ÀÖ½À´Ï´Ù.

¼Ò°³

¾Æ·¡´Â Áö¿øÇÏ´Â ¼³Á¤ÆÄÀÏ Æ÷¸ËÀÔ´Ï´Ù. ´ÜÁö Àб⸸ °¡´ÉÇÕ´Ï´Ù.
[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 
 

±â´É

  1. Section, Key¸¦ ÀÌ¿ëÇØ¼­ Value¸¦ ¾ò¾î¿Ã ¼ö ÀÖ´Ù.
  2. SectionÀ» ÀÌ¿ëÇØ¼­ Section¿¡ Æ÷ÇÔµÈ ¾ÆÀÌÅÛµéÀ» scanÇÒ ¼ö ÀÖ´Ù.

ÄÚµå

C++·Î ÀÛ¼ºµÇ¾î ÀÖ½À´Ï´Ù.
  1. ÇÁ·ÎÅäŸÀÔÀÇ ÄÚµå·Î º¸¾Æ¾ß ÇÑ´Ù.
  2. Á¤ÇüÀûÀÌÁö ¾Ê´Â ¼³Á¤ÀÇ °æ¿ì ¾î¶»°Ô ÀÛµ¿ÇÒÁö º¸ÀåÇÒ ¼ö ¾ø´Ù. : ¿¡·¯Ã³¸® ºÎÁ·
// 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 
 

PLUGIN SectionÀÇ ¸ðµç ¾ÆÀÌÅÛÀÇ Key¿Í Value¸¦ ¾ò¾î¿Â´Ù.
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; 
} 
 

º¯°æ»çÇ×

2007/7/19

  1. ÁÖ¼®Ã³¸® ±â´É Ãß°¡
  2. '='¸¦ Æ÷ÇÔÇÏÁö ¾ÊÀº ¶óÀε¥ ´ëÇÑ ¿¹¿Ü ó¸®
EmailÀ» ±âÀÔÇϸé, ´ñ±ÛÀÌ ¸ÞÀÏ·Î Àü´ÞµË´Ï´Ù.