Photon Common Library 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
IniFile.h
Go to the documentation of this file.
1#pragma once
2
3#include "Common/assertion.h"
4
5#include <string>
6#include <string_view>
7#include <vector>
8#include <cstddef>
9#include <optional>
10#include <utility>
11
12namespace ph
13{
14
20class IniFile final
21{
22public:
23 static IniFile read(const std::string& iniFilePath);
24
25public:
28 IniFile();
29
30 explicit IniFile(const std::string& iniFilePath);
31
32 void save(const std::string& iniFilePath);
33 void clear();
34
35 std::size_t numSections() const;
36 std::string_view getSectionName(std::size_t sectionIdx) const;
37 std::string_view getCurrentSectionName() const;
38 std::optional<std::size_t> findSectionIndex(std::string_view sectionName) const;
39 void setCurrentSection(std::size_t sectionIdx);
40 void setCurrentSection(std::string_view sectionName, bool createIfNotExist = true);
41
42 std::size_t numProperties() const;
43 std::string_view getPropertyName(std::size_t propertyIdx) const;
44 std::string_view getPropertyValue(std::size_t propertyIdx) const;
45 std::optional<std::size_t> findPropertyIndex(std::string_view propertyName) const;
46
49 void setProperty(std::size_t propertyIdx, std::string_view propertyValue);
50
53 void setProperty(
54 std::string_view propertyName,
55 std::string_view propertyValue,
56 bool createIfNotExist = true);
57
63 void append(const IniFile& other);
64
65private:
66 struct IniSection final
67 {
68 std::string name;
69 std::vector<std::pair<std::string, std::string>> keyValPairs;
70 };
71
72 IniSection& getIniSection(std::size_t sectionIdx);
73 const IniSection& getIniSection(std::size_t sectionIdx) const;
74
75 std::vector<IniSection> m_sections;
76 std::size_t m_currentSectionIdx;
77};
78
79// In-header Implementations:
80
81inline std::size_t IniFile::numSections() const
82{
83 return m_sections.size();
84}
85
86inline std::size_t IniFile::numProperties() const
87{
88 return getIniSection(m_currentSectionIdx).keyValPairs.size();
89}
90
91inline std::string_view IniFile::getSectionName(const std::size_t sectionIdx) const
92{
93 return getIniSection(sectionIdx).name;
94}
95
96inline std::string_view IniFile::getCurrentSectionName() const
97{
98 return getIniSection(m_currentSectionIdx).name;
99}
100
101inline void IniFile::setCurrentSection(const std::size_t sectionIdx)
102{
103 PH_ASSERT_LT(sectionIdx, m_sections.size());
104
105 m_currentSectionIdx = sectionIdx;
106}
107
108inline std::string_view IniFile::getPropertyName(const std::size_t propertyIdx) const
109{
110 const IniSection& section = getIniSection(m_currentSectionIdx);
111
112 PH_ASSERT_LT(propertyIdx, section.keyValPairs.size());
113 return section.keyValPairs[propertyIdx].first;
114}
115
116inline std::string_view IniFile::getPropertyValue(const std::size_t propertyIdx) const
117{
118 const IniSection& section = getIniSection(m_currentSectionIdx);
119
120 PH_ASSERT_LT(propertyIdx, section.keyValPairs.size());
121 return section.keyValPairs[propertyIdx].second;
122}
123
124inline void IniFile::setProperty(const std::size_t propertyIdx, const std::string_view propertyValue)
125{
126 IniSection& section = getIniSection(m_currentSectionIdx);
127
128 PH_ASSERT_LT(propertyIdx, section.keyValPairs.size());
129 section.keyValPairs[propertyIdx].second = propertyValue;
130}
131
132inline IniFile::IniSection& IniFile::getIniSection(const std::size_t sectionIdx)
133{
134 PH_ASSERT_LT(sectionIdx, m_sections.size());
135
136 return m_sections[sectionIdx];
137}
138
139inline const IniFile::IniSection& IniFile::getIniSection(const std::size_t sectionIdx) const
140{
141 PH_ASSERT_LT(sectionIdx, m_sections.size());
142
143 return m_sections[sectionIdx];
144}
145
146inline void IniFile::clear()
147{
148 m_sections.clear();
149}
150
151}// end namespace ph
#define PH_ASSERT_LT(a, b)
Definition assertion.h:64
INI file I/O. This class is useful for recording various settings across the entire engine project....
Definition IniFile.h:21
std::optional< std::size_t > findSectionIndex(std::string_view sectionName) const
Definition IniFile.cpp:77
std::string_view getPropertyValue(std::size_t propertyIdx) const
Definition IniFile.h:116
void setCurrentSection(std::size_t sectionIdx)
Definition IniFile.h:101
std::string_view getCurrentSectionName() const
Definition IniFile.h:96
std::size_t numSections() const
Definition IniFile.h:81
std::optional< std::size_t > findPropertyIndex(std::string_view propertyName) const
Definition IniFile.cpp:90
std::string_view getPropertyName(std::size_t propertyIdx) const
Definition IniFile.h:108
std::string_view getSectionName(std::size_t sectionIdx) const
Definition IniFile.h:91
void clear()
Definition IniFile.h:146
IniFile()
Creates a file with no content. An empty section is made current.
Definition IniFile.cpp:10
void append(const IniFile &other)
Add another INI file to this one. All properties from the other file will be added to this one....
Definition IniFile.cpp:129
void save(const std::string &iniFilePath)
Definition IniFile.cpp:25
std::size_t numProperties() const
Definition IniFile.h:86
void setProperty(std::size_t propertyIdx, std::string_view propertyValue)
Set a property under current section by index.
Definition IniFile.h:124
static IniFile read(const std::string &iniFilePath)
Definition IniFile.cpp:142
The root for all renderer implementations.
Definition assertion.h:9