티스토리 뷰
728x90
    
    
  반응형
    
    
    
  INI 파일을 읽고 데이터에 접근할 수 있는 간단한 클래스를 구현하려면, 표준 라이브러리만 사용하여 파일을 파싱하는 로직을 작성할 수 있습니다. 다음은 기본적인 INI 파일 형식을 읽고 데이터에 접근할 수 있는 C++ 클래스의 예제입니다.
주요 특징
- 파일 읽기:
- 파일을 줄 단위로 읽으며 섹션, 키, 값을 파싱합니다.
 
 - 공백 및 주석 처리:
- 공백을 제거하고 ; 또는 #로 시작하는 주석을 무시합니다.
 
 - 데이터 저장:
- 섹션(std::string)과 키(std::string)를 기반으로 값을 저장합니다.
 
 - 오류 처리:
- 없는 섹션이나 키에 접근하려고 할 때 예외를 던집니다.
 
 - 간단한 API:
- 섹션 또는 키 값을 손쉽게 가져올 수 있습니다.
 
 
#include <string>
#include <unordered_map>
#include <fstream>
#include <sstream>
#include <stdexcept>
class IniParser {
public:
    IniParser() = default;
    // INI 파일 읽기
    void loadFromFile(const std::string& filename) {
        std::ifstream file(filename);
        if (!file.is_open()) {
            throw std::runtime_error("Failed to open file: " + filename);
        }
        std::string line;
        std::string currentSection;
        while (std::getline(file, line)) {
            line = trim(line);
            // 빈 줄이나 주석 무시
            if (line.empty() || line[0] == ';' || line[0] == '#') {
                continue;
            }
            // 섹션 처리
            if (line.front() == '[' && line.back() == ']') {
                currentSection = line.substr(1, line.size() - 2);
                currentSection = trim(currentSection);
                continue;
            }
            // 키=값 처리
            auto delimiterPos = line.find('=');
            if (delimiterPos != std::string::npos) {
                std::string key = trim(line.substr(0, delimiterPos));
                std::string value = trim(line.substr(delimiterPos + 1));
                data[currentSection][key] = value;
            }
        }
    }
    // 특정 키 값 가져오기
    std::string getValue(const std::string& section, const std::string& key) const {
        auto sectionIt = data.find(section);
        if (sectionIt != data.end()) {
            auto keyIt = sectionIt->second.find(key);
            if (keyIt != sectionIt->second.end()) {
                return keyIt->second;
            }
        }
        throw std::runtime_error("Key not found: " + section + "." + key);
    }
    // 섹션의 모든 키-값 가져오기
    std::unordered_map<std::string, std::string> getSection(const std::string& section) const {
        auto sectionIt = data.find(section);
        if (sectionIt != data.end()) {
            return sectionIt->second;
        }
        throw std::runtime_error("Section not found: " + section);
    }
private:
    // 공백 제거 함수
    static std::string trim(const std::string& str) {
        const auto strBegin = str.find_first_not_of(" \t");
        if (strBegin == std::string::npos) {
            return ""; // 공백만 있는 문자열
        }
        const auto strEnd = str.find_last_not_of(" \t");
        const auto strRange = strEnd - strBegin + 1;
        return str.substr(strBegin, strRange);
    }
    // 데이터 저장: 섹션 -> (키 -> 값)
    std::unordered_map<std::string, std::unordered_map<std::string, std::string>> data;
};
[Database]
Host=127.0.0.1
Port=3306
User=admin
Password=secret
[General]
AppName=MyApp
Version=1.0.0
#include <iostream>
int main() {
    try {
        IniParser parser;
        parser.loadFromFile("config.ini");
        // 섹션과 키를 통해 값 가져오기
        std::string dbHost = parser.getValue("Database", "Host");
        std::string appName = parser.getValue("General", "AppName");
        std::cout << "Database Host: " << dbHost << std::endl;
        std::cout << "App Name: " << appName << std::endl;
        // 섹션 전체 가져오기
        auto dbSection = parser.getSection("Database");
        for (const auto& [key, value] : dbSection) {
            std::cout << key << " = " << value << std::endl;
        }
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }
    return 0;
}728x90
    
    
  
					댓글
						
					
					
					
				
			반응형
    
    
    
  
										공지사항
										
								
							
							
							
								최근에 올라온 글
								
							
							
								
									최근에 달린 댓글
									
							
							
								- Total
 
- Today
 
- Yesterday
 
									링크
									
							
							
								
									TAG
									
							
							
							- RSA
 - 성산블루버블
 - 윈도우
 - 서귀포
 - 제주도
 - ReFS
 - 블루버블다이빙팀
 - OpenSource
 - Build
 - Thread
 - 양파다이브
 - Linux
 - 울릉도
 - 다이빙
 - 스쿠버다이빙
 - Windows
 - 리눅스
 - 서귀포블루버블
 - 블루버블다이브팀
 - PowerShell
 - C++
 - 블루버블
 - 패턴
 - 현포다이브
 - 디자인패턴
 - ip
 - C
 - DLL
 - 암호화
 - 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 | 
									글 보관함
									
							
					