CARLA
Loading...
Searching...
No Matches
IniFile.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
9#include <util/ue-header-guard-begin.h>
10#include "Misc/ConfigCacheIni.h"
11#include <util/ue-header-guard-end.h>
12
13#include <limits>
14
15/// Wrapper around Unreal's INI file. In get functions, @a Target value is only
16/// set if it was present in the INI file, otherwise it keeps its value.
17class CARLA_API FIniFile : private NonCopyable
18{
19private:
20
21 template <typename TARGET, typename SOURCE>
22 static void SafeCastTo(SOURCE source, TARGET &target)
23 {
24 if ((source >= std::numeric_limits<TARGET>::lowest()) &&
25 (source <= std::numeric_limits<TARGET>::max())) {
26 target = static_cast<TARGET>(source);
27 } else {
28 UE_LOG(LogCarla, Error, TEXT("FIniFile: Type cast failed"));
29 }
30 }
31
32public:
33
34 // ===========================================================================
35 /// @name Constructor
36 // ===========================================================================
37 /// @{
38
39 FIniFile() = default;
40
41 explicit FIniFile(const FString &FileName)
42 {
43 ConfigFile.Read(FileName);
44 }
45
46 /// @}
47 // ===========================================================================
48 /// @name Other functions
49 // ===========================================================================
50 /// @{
51
52 bool Combine(const FString &FileName)
53 {
54 return ConfigFile.Combine(FileName);
55 }
56
57 void ProcessInputFileContents(const FString &INIFileContents, const FString &Hint)
58 {
59 ConfigFile.ProcessInputFileContents(INIFileContents, Hint);
60 }
61
62 bool HasSection(const FString &Section) const
63 {
64 return (ConfigFile.Num() > 0) && (ConfigFile.FindSection(Section) != nullptr);
65 }
66
67 void AddSectionIfMissing(const FString &Section)
68 {
69 if (!HasSection(Section)) {
70 ConfigFile.Add(Section, FConfigSection());
71 }
72 }
73
74 /// Write contents to disk.
75 bool Write(const FString &Filename)
76 {
77 return ConfigFile.Write(Filename);
78 }
79
80 /// Retrieve Unreal's FConfigFile.
81 const FConfigFile &GetFConfigFile() const
82 {
83 return ConfigFile;
84 }
85
86 /// @}
87 // ===========================================================================
88 /// @name Get functions
89 // ===========================================================================
90 /// @{
91
92 template <typename T>
93 void GetInt(const TCHAR* Section, const TCHAR* Key, T &Target) const
94 {
95 int64 Value;
96 if (ConfigFile.GetInt64(Section, Key, Value)) {
97 SafeCastTo<T>(Value, Target);
98 }
99 }
100
101 void GetString(const TCHAR* Section, const TCHAR* Key, FString &Target) const
102 {
103 FString Value;
104 if (ConfigFile.GetString(Section, Key, Value)) {
105 Target = Value;
106 }
107 }
108
109 void GetBool(const TCHAR* Section, const TCHAR* Key, bool &Target) const
110 {
111 bool Value;
112 if (ConfigFile.GetBool(Section, Key, Value)) {
113 Target = Value;
114 }
115 }
116
117 void GetFloat(const TCHAR* Section, const TCHAR* Key, float &Target, const float Factor = 1.0f) const
118 {
119 FString Value;
120 if (ConfigFile.GetString(Section, Key, Value)) {
121 Target = Factor * FCString::Atof(*Value);
122 }
123 }
124
125 void GetLinearColor(const TCHAR* Section, const TCHAR* Key, FLinearColor &Target) const
126 {
127 FString Value;
128 if (ConfigFile.GetString(Section, Key, Value)) {
129 Target.InitFromString(Value);
130 }
131 }
132
133 /// @}
134 // ===========================================================================
135 /// @name Set functions
136 // ===========================================================================
137 /// @{
138
139 void SetInt(const TCHAR* Section, const TCHAR* Key, const int64 Value)
140 {
141 ConfigFile.SetInt64(Section, Key, Value);
142 }
143
144 void SetString(const TCHAR* Section, const TCHAR* Key, const TCHAR* Value)
145 {
146 ConfigFile.SetString(Section, Key, Value);
147 }
148
149 void SetString(const TCHAR* Section, const TCHAR* Key, const FString &Value)
150 {
151 SetString(Section, Key, *Value);
152 }
153
154 void SetBool(const TCHAR* Section, const TCHAR* Key, const bool Value)
155 {
156 SetString(Section, Key, Value ? TEXT("True") : TEXT("False"));
157 }
158
159 void SetFloat(const TCHAR* Section, const TCHAR* Key, const float Value)
160 {
161 SetString(Section, Key, FText::AsNumber(Value).ToString());
162 }
163
164 void SetLinearColor(const TCHAR* Section, const TCHAR* Key, const FLinearColor &Value)
165 {
166 SetString(Section, Key, Value.ToString());
167 }
168
169 /// @}
170
171private:
172
173 FConfigFile ConfigFile;
174};
Wrapper around Unreal's INI file.
Definition IniFile.h:18
void SetLinearColor(const TCHAR *Section, const TCHAR *Key, const FLinearColor &Value)
Definition IniFile.h:164
FConfigFile ConfigFile
Definition IniFile.h:173
const FConfigFile & GetFConfigFile() const
Retrieve Unreal's FConfigFile.
Definition IniFile.h:81
bool Combine(const FString &FileName)
Definition IniFile.h:52
bool Write(const FString &Filename)
Write contents to disk.
Definition IniFile.h:75
static void SafeCastTo(SOURCE source, TARGET &target)
Definition IniFile.h:22
bool HasSection(const FString &Section) const
Definition IniFile.h:62
void SetFloat(const TCHAR *Section, const TCHAR *Key, const float Value)
Definition IniFile.h:159
FIniFile()=default
void GetBool(const TCHAR *Section, const TCHAR *Key, bool &Target) const
Definition IniFile.h:109
FIniFile(const FString &FileName)
Definition IniFile.h:41
void GetFloat(const TCHAR *Section, const TCHAR *Key, float &Target, const float Factor=1.0f) const
Definition IniFile.h:117
void GetString(const TCHAR *Section, const TCHAR *Key, FString &Target) const
Definition IniFile.h:101
void SetString(const TCHAR *Section, const TCHAR *Key, const TCHAR *Value)
Definition IniFile.h:144
void SetString(const TCHAR *Section, const TCHAR *Key, const FString &Value)
Definition IniFile.h:149
void ProcessInputFileContents(const FString &INIFileContents, const FString &Hint)
Definition IniFile.h:57
void AddSectionIfMissing(const FString &Section)
Definition IniFile.h:67
void GetInt(const TCHAR *Section, const TCHAR *Key, T &Target) const
Definition IniFile.h:93
void SetInt(const TCHAR *Section, const TCHAR *Key, const int64 Value)
Definition IniFile.h:139
void SetBool(const TCHAR *Section, const TCHAR *Key, const bool Value)
Definition IniFile.h:154
void GetLinearColor(const TCHAR *Section, const TCHAR *Key, FLinearColor &Target) const
Definition IniFile.h:125