OpenClonk
StdRegistry.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Some wrappers for easier access to the Windows registry */
19 
20 #ifndef INC_STDREGISTRY
21 #define INC_STDREGISTRY
22 
23 #ifdef _WIN32
24 #include "lib/StdCompiler.h"
26 
27 StdCopyStrBuf GetRegistryString(const char *szSubKey, const char *szValueName);
28 bool SetRegistryString(const char *szSubKey, const char *szValueName, const char *szValue);
29 
30 bool SetRegShell(const wchar_t *szClassName,
31  const wchar_t *szShellName,
32  const wchar_t *szShellCaption,
33  const wchar_t *szCommand,
34  bool fMakeDefault = false);
35 
36 bool RemoveRegShell(const char *szClassName,
37  const char *szShellName);
38 
39 bool StoreWindowPosition(HWND hwnd,
40  const char *szWindowName,
41  const char *szSubKey,
42  bool fStoreSize = true);
43 
44 bool RestoreWindowPosition(HWND hwnd,
45  const char *szWindowName,
46  const char *szSubKey,
47  bool fHidden = false);
48 
49 // config writer
50 class StdCompilerConfigWrite : public StdCompiler
51 {
52 public:
53 
54  // Construct with root key
55  StdCompilerConfigWrite(HKEY hRoot, const char *szPath);
56  ~StdCompilerConfigWrite() override;
57 
58  // Properties
59  bool hasNaming() override { return true; }
60  bool isRegistry() override { return true; }
61 
62  // Naming
63  bool Name(const char *szName) override;
64  void NameEnd(bool fBreak = false) override;
65  bool FollowName(const char *szName) override;
66  bool Default(const char *szName) override;
67 
68  // Separators
69  bool Separator(Sep eSep) override;
70 
71  // Data writers
72  void DWord(int32_t &rInt) override;
73  void DWord(uint32_t &rInt) override;
74  void Word(int16_t &rShort) override;
75  void Word(uint16_t &rShort) override;
76  void Byte(int8_t &rByte) override;
77  void Byte(uint8_t &rByte) override;
78  void Boolean(bool &rBool) override;
79  void Character(char &rChar) override;
80  void String(char *szString, size_t iMaxLength, RawCompileType eType = RCT_Escaped) override;
81  void String(char **pszString, RawCompileType eType = RCT_Escaped) override;
82  void String(std::string &str, RawCompileType type = RCT_Escaped) override;
83  void Raw(void *pData, size_t iSize, RawCompileType eType = RCT_Escaped) override;
84 
85  // Passes
86  void Begin() override;
87  void End() override;
88 
89 private:
90 
91  // Key stack
92  int iDepth;
93  struct Key
94  {
95  StdCopyStrBuf Name;
96  StdCopyStrBuf LastChildName; // last occuring child name to increase subindex if needed
97  int32_t subindex; // incremented when multiple keys of the same name are encountered
98  HKEY Handle;
99  Key *Parent;
100  } *pKey;
101 
102  // Current string for writing with separators
103  std::string last_written_string;
104 
105  // Writing
106  void CreateKey(HKEY hParent = nullptr);
107  void WriteDWord(uint32_t iVal);
108  void WriteString(const char *szStr);
109 
110 };
111 
112 // config reader
113 class StdCompilerConfigRead : public StdCompiler
114 {
115 public:
116 
117  // Construct with root key
118  StdCompilerConfigRead(HKEY hRoot, const char *szPath);
119  ~StdCompilerConfigRead() override;
120 
121  // Properties
122  bool isDeserializer() override { return true; }
123  bool hasNaming() override { return true; }
124  bool isRegistry() override { return true; }
125 
126  // Naming
127  bool Name(const char *szName) override;
128  void NameEnd(bool fBreak = false) override;
129  bool FollowName(const char *szName) override;
130 
131  // Separators
132  bool Separator(Sep eSep) override;
133  void NoSeparator() override;
134 
135  // Data writers
136  void DWord(int32_t &rInt) override;
137  void DWord(uint32_t &rInt) override;
138  void Word(int16_t &rShort) override;
139  void Word(uint16_t &rShort) override;
140  void Byte(int8_t &rByte) override;
141  void Byte(uint8_t &rByte) override;
142  void Boolean(bool &rBool) override;
143  void Character(char &rChar) override;
144  void String(char *szString, size_t iMaxLength, RawCompileType eType = RCT_Escaped) override;
145  void String(char **pszString, RawCompileType eType = RCT_Escaped) override;
146  void String(std::string &str, RawCompileType type = RCT_Escaped) override;
147  void Raw(void *pData, size_t iSize, RawCompileType eType = RCT_Escaped) override;
148 
149  // Passes
150  void Begin() override;
151  void End() override;
152 
153 private:
154 
155  // Key stack
156  int iDepth;
157  struct Key
158  {
159  StdCopyStrBuf Name;
160  StdCopyStrBuf LastChildName; // last occuring child name to increase subindex if needed
161  int32_t subindex; // incremented when multiple keys of the same name are encountered
162  HKEY Handle; // for keys only
163  Key *Parent;
164  bool Virtual;
165  DWORD Type; // for values only
166  } *pKey;
167 
168  // Current string for reading with separators
169  std::string last_read_string;
170  bool has_read_string = false;
171  bool has_separator_mismatch = false;
172 
173  // Reading
174  uint32_t ReadDWord();
175  void ReadString();
176  void ResetLastString();
177 
178 };
179 
180 #endif
181 
182 #endif // INC_STDREGISTRY
uint32_t DWORD
int iSize
Definition: TstC4NetIO.cpp:32
virtual void Raw(void *pData, size_t iSize, RawCompileType eType=RCT_Escaped)=0
virtual bool Separator(Sep eSep=SEP_SEP)
Definition: StdCompiler.h:119
virtual void Character(char &rChar)=0
virtual bool Default(const char *szName)
Definition: StdCompiler.h:88
virtual void Word(int16_t &rShort)=0
virtual void NoSeparator()
Definition: StdCompiler.h:120
virtual void End()
Definition: StdCompiler.h:156
virtual void NameEnd(bool fBreak=false)
Definition: StdCompiler.h:78
virtual void Byte(int8_t &rByte)=0
virtual void Begin()
Definition: StdCompiler.h:154
virtual bool FollowName(const char *szName)
Definition: StdCompiler.h:84
virtual void String(char *szString, size_t iMaxLength, RawCompileType eType=RCT_Escaped)=0
virtual void DWord(int32_t &rInt)=0
virtual bool isDeserializer()
Definition: StdCompiler.h:53
virtual bool isRegistry()
Definition: StdCompiler.h:65
virtual void Boolean(bool &rBool)=0
virtual bool Name(const char *szName)
Definition: StdCompiler.h:77
virtual bool hasNaming()
Definition: StdCompiler.h:58