Grok 20.3.2
IniParser.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2026 Grok Image Compression Inc.
3 *
4 * This source code is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License, version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This source code is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
12 *
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18#pragma once
19
20#include <string>
21#include <map>
22#include <fstream>
23
24namespace grk
25{
26
27// Simple INI parser for AWS credentials and config files
29{
30public:
31 std::map<std::string, std::map<std::string, std::string>> sections;
32
33 bool parse(const std::string& filename)
34 {
35 std::ifstream file(filename);
36 if(!file.is_open())
37 {
38 grklog.debug("Failed to open file: %s", filename.c_str());
39 return false;
40 }
41
42 std::string current_section;
43 std::string line;
44 while(std::getline(file, line))
45 {
46 // Trim whitespace
47 line.erase(0, line.find_first_not_of(" \t"));
48 line.erase(line.find_last_not_of(" \t") + 1);
49
50 // Skip empty lines and comments
51 if(line.empty() || line[0] == ';' || line[0] == '#')
52 continue;
53
54 // Check for section header
55 if(line[0] == '[' && line.back() == ']')
56 {
57 current_section = line.substr(1, line.size() - 2);
58 sections[current_section] = {};
59 continue;
60 }
61
62 // Parse key-value pairs
63 size_t eq_pos = line.find('=');
64 if(eq_pos != std::string::npos)
65 {
66 std::string key = line.substr(0, eq_pos);
67 std::string value = line.substr(eq_pos + 1);
68 // Trim whitespace
69 key.erase(0, key.find_first_not_of(" \t"));
70 key.erase(key.find_last_not_of(" \t") + 1);
71 value.erase(0, value.find_first_not_of(" \t"));
72 value.erase(value.find_last_not_of(" \t") + 1);
73 if(!current_section.empty() && !key.empty())
74 {
75 sections[current_section][key] = value;
76 }
77 }
78 }
79 file.close();
80 return true;
81 }
82};
83
84} // namespace grk
Definition IniParser.h:29
bool parse(const std::string &filename)
Definition IniParser.h:33
std::map< std::string, std::map< std::string, std::string > > sections
Definition IniParser.h:31
ResWindow.
Definition CompressedChunkCache.h:36
ILogger & grklog
Definition Logger.cpp:24