ReactOS 0.4.16-dev-91-g764881a
configparser.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Config parser
5 * COPYRIGHT: Copyright 2009 Dmitry Chapyshev (dmitry@reactos.org)
6 * Copyright 2015 Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
7 * Copyright 2017 Alexander Shaposhnikov (sanchaez@reactos.org)
8 * Copyright 2021 Mark Jansen <mark.jansen@reactos.org>
9 */
10
11#include "rapps.h"
12#include <debug.h>
13
15{
19};
20
22{
25};
27
30{
31 for (DWORD len = 256, ret;; len *= 2)
32 {
33 ret = GetPrivateProfileString(Section, Name, L"\n", Output.GetBuffer(len), len, File);
34 if (ret + 1 != len)
35 {
36 Output.ReleaseBuffer(ret);
37 return ret && Output[0] != L'\n' ? ret : HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
38 }
39 }
40}
41
43{
44 CacheINI();
45}
46
47void
49{
50 DWORD len = 512;
52
53 do
54 {
55 len *= 2;
56
58 Buffer.ReleaseBuffer(result);
59 } while (result == len - 2);
60
61 len = 0;
62 while (len < result)
63 {
64 // Explicitly use the null terminator!
65 CString tmp = Buffer.GetBuffer() + len;
66 if (tmp.GetLength() > 0)
67 {
68 len += tmp.GetLength() + 1;
69
70 int idx = tmp.Find('=');
71 if (idx >= 0)
72 {
73 CString key = tmp.Left(idx);
74
75#ifndef _M_IX86
76 // On non-x86 architecture we need the architecture specific URL
77 if (!isArch && key == "URLDownload")
78 {
79 continue;
80 }
81#endif
82
83 // Is this key already present from a more specific translation?
84 if (m_Keys.FindKey(key) >= 0)
85 {
86 continue;
87 }
88
89 CString value = tmp.Mid(idx + 1);
91 }
92 else
93 {
94 DPRINT1("ERROR: invalid key/value pair: '%S'\n", tmp.GetString());
95 }
96 }
97 else
98 {
99 break;
100 }
101 }
102}
103
104VOID
106{
107 // Cache section names
109 {
110 CString szLocaleID;
111 const INT cchLocaleSize = 5;
112
113 GetLocaleInfoW(GetUserDefaultLCID(), LOCALE_ILANGUAGE, szLocaleID.GetBuffer(cchLocaleSize), cchLocaleSize);
114 szLocaleID.ReleaseBuffer();
115 CString INISectionLocale = L"Section." + szLocaleID;
116
117 g_Names.ArchSpecific.Locale = INISectionLocale + L"." CurrentArchitecture;
118 g_Names.ArchNeutral.Locale = INISectionLocale;
119
120 // turn "Section.0c0a" into "Section.0a", keeping just the neutral lang part
121 if (szLocaleID.GetLength() >= 2)
122 {
123 g_Names.ArchSpecific.LocaleNeutral = L"Section." + szLocaleID.Right(2) + L"." CurrentArchitecture;
124 g_Names.ArchNeutral.LocaleNeutral = L"Section." + szLocaleID.Right(2);
125 }
126
127 g_Names.ArchSpecific.Section = L"Section." CurrentArchitecture;
128 g_Names.ArchNeutral.Section = L"Section";
129 }
130
131 // Use a shared buffer so that we don't have to re-allocate it every time
133
136 {
138 }
140
143 {
145 }
147}
148
149BOOL
151{
152 int nIndex = m_Keys.FindKey(KeyName);
153 if (nIndex >= 0)
154 {
155 ResultString = m_Keys.GetValueAt(nIndex);
156 return TRUE;
157 }
158
159 ResultString.Empty();
160 return FALSE;
161}
162
163BOOL
165{
167
168 iResult = 0;
169
170 // grab the text version of our entry
171 if (!GetString(KeyName, Buffer))
172 return FALSE;
173
174 if (Buffer.IsEmpty())
175 return FALSE;
176
177 // convert it to an actual integer
178 iResult = StrToIntW(Buffer.GetString());
179
180 // we only care about values > 0
181 return (iResult > 0);
182}
183
184UINT
186{
187 HRESULT hr; // Return value; length of ini string or 0 on failure.
188 CStringW SecBuf;
189 WCHAR FullLoc[5], *NeutralLoc = FullLoc + 2;
191
192 SecBuf.Format(L"%s.%s.%s", Section, FullLoc, CurrentArchitecture);
193 if ((hr = ReadIniValue(szConfigPath, SecBuf, Name, Result)) > 0)
194 return hr;
195
196 if (*NeutralLoc)
197 {
198 SecBuf.Format(L"%s.%s.%s", Section, NeutralLoc, CurrentArchitecture);
199 if ((hr = ReadIniValue(szConfigPath, SecBuf, Name, Result)) > 0)
200 return hr;
201 }
202
203 SecBuf.Format(L"%s.%s", Section, CurrentArchitecture);
204 if ((hr = ReadIniValue(szConfigPath, SecBuf, Name, Result)) > 0)
205 return hr;
206
207 SecBuf.Format(L"%s.%s", Section, FullLoc);
208 if ((hr = ReadIniValue(szConfigPath, SecBuf, Name, Result)) > 0)
209 return hr;
210
211 if (*NeutralLoc)
212 {
213 SecBuf.Format(L"%s.%s", Section, NeutralLoc);
214 if ((hr = ReadIniValue(szConfigPath, SecBuf, Name, Result)) > 0)
215 return hr;
216 }
217
218 if ((hr = ReadIniValue(szConfigPath, Section, Name, Result)) > 0)
219 return hr;
220 return 0;
221}
PCWSTR FilePath
#define DPRINT1
Definition: precomp.h:8
TVal & GetValueAt(int nIndex)
Definition: atlsimpcoll.h:358
int FindKey(const TKey &key) const
Definition: atlsimpcoll.h:315
BOOL Add(const TKey &key, const TVal &val)
Definition: atlsimpcoll.h:309
bool IsEmpty() const noexcept
Definition: atlsimpstr.h:394
void ReleaseBuffer(_In_ int nNewLength=-1)
Definition: atlsimpstr.h:387
PXSTR GetString() noexcept
Definition: atlsimpstr.h:367
int GetLength() const noexcept
Definition: atlsimpstr.h:362
void Empty() noexcept
Definition: atlsimpstr.h:253
CStringT Right(int nCount) const
Definition: cstringt.h:788
CStringT Left(int nCount) const
Definition: cstringt.h:776
void __cdecl Format(UINT nFormatID,...)
Definition: cstringt.h:818
int Find(_In_ PCXSTR pszSub, _In_opt_ int iStart=0) const noexcept
Definition: cstringt.h:696
CStringT Mid(int iFirst, int nCount) const
Definition: cstringt.h:748
Definition: bufpool.h:45
CSimpleMap< CStringW, CStringW > m_Keys
Definition: configparser.h:8
CConfigParser(const CStringW &FilePath)
const CStringW szConfigPath
Definition: configparser.h:7
BOOL GetString(const CStringW &KeyName, CStringW &ResultString)
BOOL GetInt(const CStringW &KeyName, INT &iResult)
UINT GetSectionString(LPCWSTR Section, LPCWSTR Name, CStringW &Result)
void ReadSection(CStringW &Buffer, const CStringW &Section, BOOL isArch)
Definition: File.h:16
static CSectionNames g_Names
HRESULT ReadIniValue(LPCWSTR File, LPCWSTR Section, LPCWSTR Name, CStringW &Output)
HRESULT ReadIniValue(LPCWSTR File, LPCWSTR Section, LPCWSTR Name, CStringW &Output)
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
unsigned int idx
Definition: utils.c:41
INT WINAPI StrToIntW(LPCWSTR lpString)
Definition: string.c:407
INT WINAPI GetPrivateProfileSectionW(LPCWSTR section, LPWSTR buffer, DWORD len, LPCWSTR filename)
Definition: profile.c:1351
LCID WINAPI GetUserDefaultLCID(void)
Definition: locale.c:1210
INT WINAPI GetLocaleInfoW(LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len)
Definition: locale.c:1665
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLenum GLsizei len
Definition: glext.h:6722
GLuint64EXT * result
Definition: glext.h:11304
unsigned int UINT
Definition: ndis.h:50
#define L(x)
Definition: ntvdm.h:50
@ Output
Definition: arc.h:85
HRESULT hr
Definition: shlfolder.c:183
#define _countof(array)
Definition: sndvol32.h:70
CStringW LocaleNeutral
CLocaleSections ArchSpecific
CLocaleSections ArchNeutral
Definition: copy.c:22
int32_t INT
Definition: typedefs.h:58
Definition: pdh_main.c:94
int ret
_Must_inspect_result_ _In_ WDFDEVICE _In_ PCUNICODE_STRING KeyName
Definition: wdfdevice.h:2699
#define GetPrivateProfileString
Definition: winbase.h:3835
#define HRESULT_FROM_WIN32(x)
Definition: winerror.h:92
#define ERROR_NOT_FOUND
Definition: winerror.h:690
#define LOCALE_ILANGUAGE
Definition: winnls.h:25
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:409
__wchar_t WCHAR
Definition: xmlstorage.h:180
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185