UTF8 - Simple Library for Internationalization
Loading...
Searching...
No Matches
winutf8.h
Go to the documentation of this file.
1/*
2 Copyright (c) Mircea Neacsu (2014-2024) Licensed under MIT License.
3 This is part of UTF8 project. See LICENSE file for full license terms.
4*/
5
7#pragma once
8
9#include <Windows.h>
10#include <string>
11#include <ostream>
12
13#undef MessageBox
14#undef CopyFile
15#undef LoadString
16#undef ShellExecute
17#undef GetTempPath
18#undef GetTempFileName
19#undef GetFullPathName
20#undef GetModuleFileName
21#undef RegCreateKey
22#undef RegDeleteKey
23#undef RegOpenKey
24#undef RegSetValue
25#undef RegQueryValue
26#undef RegGetValue
27#undef RegDeleteTree
28#undef RegEnumKey
29#undef RegDeleteValue
30#undef RegEnumValue
31
32namespace utf8 {
33
34std::vector<std::string> get_argv ();
35char** get_argv (int* argc);
36void free_argv (int argc, char** argv);
37
38bool chmod (const char* filename, int mode);
39bool chmod (const std::string& filename, int mode);
40
41bool access (const char* filename, int mode);
42bool access (const std::string& filename, int mode);
43
44bool splitpath (const std::string& path, char* drive, char* dir, char* fname, char* ext);
45bool splitpath (const std::string& path, std::string& drive, std::string& dir,
46 std::string& fname, std::string& ext);
47bool makepath (std::string& path, const std::string& drive, const std::string& dir,
48 const std::string& fname, const std::string& ext);
49std::string fullpath (const std::string& relpath);
50
51std::string getenv (const std::string& var);
52bool putenv (const std::string& str);
53bool putenv (const std::string& var, const std::string& val);
54
55bool symlink (const char* path, const char* link, bool directory);
56bool symlink (const std::string& path, const std::string& target, bool directory);
57
58int system (const std::string& cmd);
59
60int MessageBox (HWND hWnd, const std::string& text, const std::string& caption,
61 unsigned int type);
62bool CopyFile (const std::string& from, const std::string& to, bool fail_exist);
63std::string LoadString (UINT id, HINSTANCE hInst = NULL);
64
65//registry functions
66LSTATUS RegCreateKey (HKEY key, const std::string& subkey, HKEY& result,
67 DWORD options = REG_OPTION_NON_VOLATILE, REGSAM sam = KEY_ALL_ACCESS,
68 const SECURITY_ATTRIBUTES* psa = nullptr, DWORD* disp = nullptr);
69LSTATUS RegOpenKey (HKEY key, const std::string& subkey, HKEY& result,
70 REGSAM sam = KEY_ALL_ACCESS, bool link = false);
71LSTATUS RegDeleteKey (HKEY key, const std::string& subkey, REGSAM sam = 0);
72LSTATUS RegDeleteValue (HKEY key, const std::string& value);
73LSTATUS RegDeleteTree (HKEY key, const std::string& subkey = std::string ());
74LSTATUS RegRenameKey (HKEY key, const std::string& subkey, const std::string& new_name);
75LSTATUS RegSetValue (HKEY key, const std::string& value, DWORD type, const void* data, DWORD size);
76LSTATUS RegSetValue (HKEY key, const std::string& value, const std::string& data);
77LSTATUS RegSetValue (HKEY key, const std::string& value, const std::vector<std::string>& data);
78LSTATUS RegQueryValue (HKEY key, const std::string& value, DWORD* type, void* data, DWORD* size);
79LSTATUS RegGetValue (HKEY key, const std::string& subkey, const std::string& value,
80 DWORD flags, void* data, DWORD* size, DWORD* type = NULL);
81LSTATUS RegGetValue (HKEY key, const std::string& subkey, const std::string& value,
82 std::string& data, bool expand = false);
83LSTATUS RegGetValue (HKEY key, const std::string& subkey, const std::string& value,
84 std::vector<std::string>& data);
85LSTATUS RegEnumKey (HKEY key, DWORD index, std::string& name, DWORD maxlen = 0, FILETIME* last_write_time = NULL);
86LSTATUS RegEnumKey (HKEY key, std::vector<std::string>& names);
87LSTATUS RegEnumValue (HKEY key, DWORD index, std::string& value, DWORD maxlen = 0, DWORD* type = 0, void* data = 0, DWORD* data_len = 0);
88LSTATUS RegEnumValue (HKEY key, std::vector<std::string>& values);
89
90HINSTANCE ShellExecute (const std::string& file, const std::string& verb = std::string (),
91 const std::string& parameters = std::string (),
92 const std::string& directory = std::string ("."),
93 HWND hWnd = NULL, int show = SW_SHOW);
94std::string GetTempPath ();
95std::string GetTempFileName (const std::string& path, const std::string& prefix, UINT unique=0);
96std::string GetFullPathName (const std::string& rel_path);
97bool GetModuleFileName (HMODULE hModule, std::string& filename);
98std::string GetModuleFileName (HMODULE hModule = NULL);
99
101struct find_data {
102 find_data ()
103 : handle{ INVALID_HANDLE_VALUE }
104 , attributes{ 0 }
105 , creation_time{ 0, 0 }
106 , access_time{ 0,0 }
107 , write_time{ 0,0 }
108 , size{ 0 }
109 {}
110 HANDLE handle;
112 FILETIME creation_time;
113 FILETIME access_time;
114 FILETIME write_time;
115 __int64 size;
116 std::string filename;
117 std::string short_name;
118};
119
120bool find_first (const std::string& name, find_data& fdat);
121bool find_next (find_data& fdat);
122void find_close (find_data& fdat);
123
140class file_enumerator : protected find_data
141{
142public:
143 explicit file_enumerator (const std::string& name);
145 bool ok () const;
146 bool next ();
147
148 operator bool () const;
149
157};
158
160class buffer {
161public:
162 explicit buffer (size_t size_);
163 ~buffer ();
164 buffer (const buffer& other);
165 buffer& operator = (const buffer& rhs);
166 buffer& operator = (const std::string& rhs);
167 operator wchar_t* ();
168 operator std::string () const;
169 DWORD size () const;
170
171private:
172 wchar_t *ptr;
173 size_t sz;
174};
175
176inline std::ostream&
177operator << (std::ostream& s, const buffer& b)
178{
179 s << (std::string)b;
180 return s;
181}
182
183
184//--------------------- INLINE FUNCTIONS --------------------------------------
185
196inline
197bool chmod (const char* filename, int mode)
198{
199 return (_wchmod (widen (filename).c_str (), mode) == 0);
200}
201
203inline
204bool chmod (const std::string& filename, int mode)
205{
206 return (_wchmod (widen (filename).c_str (), mode) == 0);
207}
208
209
223inline
224bool access (const char* filename, int mode)
225{
226 return (_waccess (widen (filename).c_str (), mode) == 0);
227}
228
230inline
231bool access (const std::string& filename, int mode)
232{
233 return (_waccess (widen (filename).c_str (), mode) == 0);
234}
235
236
245inline
246bool putenv (const std::string& str)
247{
248 return (_wputenv (utf8::widen (str).c_str ()) == 0);
249}
250
261inline
262bool putenv (const std::string& var, const std::string& val)
263{
264 return (_wputenv_s (widen (var).c_str (),
265 widen (val).c_str ()) == 0);
266}
267
276inline
277int system (const std::string& cmd)
278{
279 std::wstring wcmd = utf8::widen (cmd);
280 return _wsystem (wcmd.c_str ());
281}
282
283// -------------------- file_enumerator ------------------------------------
284
286inline
287file_enumerator::file_enumerator (const std::string& name)
288{
289 find_first (name, *this);
290}
291
293inline
295{
296 if (handle != INVALID_HANDLE_VALUE)
297 find_close (*this);
298}
299
301inline
303{
304 return (handle != INVALID_HANDLE_VALUE);
305}
306
309inline
310file_enumerator::operator bool () const
311{
312 return ok ();
313}
314
316inline
318{
319 return find_next (*this);
320}
321
322//---------------------------- buffer ----------------------------------------
323
325inline
326 buffer::operator wchar_t* ()
327{
328 return ptr;
329}
330
332inline
333 buffer::operator std::string () const
334{
335 return utf8::narrow (ptr);
336}
337
339inline DWORD
341{
342 return (DWORD)sz;
343}
344
345} //end namespace
346
A simple buffer for caching values returned by Windows API.
Definition winutf8.h:160
buffer & operator=(const buffer &rhs)
Principal assignment operator.
Definition buffer.cpp:71
DWORD size() const
Return buffer size.
Definition winutf8.h:340
~buffer()
Destructor.
Definition buffer.cpp:55
An object - oriented wrapper for find_... functions.
Definition winutf8.h:141
file_enumerator(const std::string &name)
Constructs a file_enumerator object and tries to locate the first file.
Definition winutf8.h:287
bool next()
Advance the enumerator to next file.
Definition winutf8.h:317
bool ok() const
Return true if a file has been enumerated.
Definition winutf8.h:302
~file_enumerator()
Closes the search handle associated with this object.
Definition winutf8.h:294
std::string narrow(const wchar_t *s, size_t nch)
Conversion from wide character to UTF-8.
Definition utf8.cpp:52
std::wstring widen(const char *s, size_t nch)
Conversion from UTF-8 to wide character.
Definition utf8.cpp:207
LSTATUS RegCreateKey(HKEY key, const std::string &subkey, HKEY &result, DWORD options, REGSAM sam, const SECURITY_ATTRIBUTES *psa, DWORD *disp)
Convenience wrapper for RegCreateKeyEx
Definition win.cpp:483
LSTATUS RegGetValue(HKEY key, const std::string &subkey, const std::string &value, DWORD flags, void *data, DWORD *size, DWORD *type)
Wrapper for RegGetValue
Definition win.cpp:683
LSTATUS RegDeleteKey(HKEY key, const std::string &subkey, REGSAM sam)
Wrapper for RegDeleteKeyEx or RegDeleteKey
Definition win.cpp:522
LSTATUS RegSetValue(HKEY key, const std::string &value, DWORD type, const void *data, DWORD size)
Wrapper for RegSetValueEx
Definition win.cpp:590
LSTATUS RegDeleteTree(HKEY key, const std::string &subkey)
Wrapper for RegDeleteTree
Definition win.cpp:555
LSTATUS RegRenameKey(HKEY key, const std::string &subkey, const std::string &new_name)
Wrapper for RegRenameKey
Definition win.cpp:572
LSTATUS RegQueryValue(HKEY key, const std::string &value, DWORD *type, void *data, DWORD *size)
Wrapper for RegQueryValueEx
Definition win.cpp:664
LSTATUS RegEnumKey(HKEY key, DWORD index, std::string &name, DWORD maxlen, FILETIME *last_write_time)
Wrapper for RegEnumKeyEx
Definition win.cpp:779
LSTATUS RegDeleteValue(HKEY key, const std::string &value)
Wrapper for RegDeleteValue
Definition win.cpp:540
LSTATUS RegOpenKey(HKEY key, const std::string &subkey, HKEY &result, REGSAM sam, bool link)
Wrapper for RegOpenKeyEx
Definition win.cpp:499
LSTATUS RegEnumValue(HKEY key, DWORD index, std::string &value, DWORD maxlen, DWORD *type, void *data, DWORD *data_len)
Wrapper for RegEnumValue
Definition win.cpp:847
File enumeration structure used by find_first() and find_next() functions.
Definition winutf8.h:101
__int64 size
file size
Definition winutf8.h:115
std::string filename
file name
Definition winutf8.h:116
DWORD attributes
file attributes
Definition winutf8.h:111
HANDLE handle
search handle
Definition winutf8.h:110
FILETIME access_time
file last access time
Definition winutf8.h:113
std::string short_name
8.3 file name
Definition winutf8.h:117
FILETIME creation_time
file creation time
Definition winutf8.h:112
FILETIME write_time
file last write time
Definition winutf8.h:114
std::string GetTempPath()
Convenience wrapper for GetTempPath
Definition win.cpp:171
bool makepath(std::string &path, const std::string &drive, const std::string &dir, const std::string &fname, const std::string &ext)
Creates a path from UTF-8 encoded components.
Definition win.cpp:353
bool find_first(const std::string &name, find_data &fdat)
Searches a directory for a file or subdirectory with a name that matches a name (that can have wildca...
Definition win.cpp:40
std::string fullpath(const std::string &relpath)
Returns the absolute (full) path of a filename.
Definition win.cpp:368
bool GetModuleFileName(HMODULE hModule, std::string &filename)
Convenience wrapper for GetModuleFileName
Definition win.cpp:227
std::string LoadString(UINT id, HINSTANCE hInst)
Convenience wrapper for Windows LoadString function.
Definition win.cpp:134
HINSTANCE ShellExecute(const std::string &file, const std::string &verb, const std::string &parameters, const std::string &directory, HWND hWnd, int show)
Convenience wrapper for Windows ShellExecute function.
Definition win.cpp:155
std::string getenv(const std::string &var)
Retrieves the value of an environment variable.
Definition win.cpp:383
int MessageBox(HWND hWnd, const std::string &text, const std::string &caption, unsigned int type)
Convenience wrapper for Windows MessageBox function.
Definition win.cpp:107
bool splitpath(const std::string &path, char *drive, char *dir, char *fname, char *ext)
Breaks a path name into components.
Definition win.cpp:289
void free_argv(int argc, char **argv)
Frees the memory allocated by get_argv(int *argc)
Definition win.cpp:432
bool find_next(find_data &fdat)
Continues a search started by find_first() function.
Definition win.cpp:64
bool CopyFile(const std::string &from, const std::string &to, bool fail_exist)
Convenience wrapper for Windows CopyFile function.
Definition win.cpp:121
std::string GetFullPathName(const std::string &rel_path)
Convenience wrapper for GetFullPathName
Definition win.cpp:205
char ** get_argv(int *argc)
Converts wide byte command arguments to an array of pointers to UTF-8 strings.
Definition win.cpp:408
std::string GetTempFileName(const std::string &path, const std::string &prefix, UINT unique)
Convenience wrapper for GetTempFileName
Definition win.cpp:188
bool symlink(const char *path, const char *link, bool directory)
Create a symbolic link.
Definition win.cpp:264
void find_close(find_data &fdat)
Closes a "search handle" opened by find_first() function.
Definition win.cpp:88
bool putenv(const std::string &str)
Creates, modifies, or removes environment variables.
Definition winutf8.h:246
bool chmod(const char *filename, int mode)
Changes the file access permissions.
Definition winutf8.h:197
int system(const std::string &cmd)
Passes command to command interpreter.
Definition winutf8.h:277
bool access(const char *filename, int mode)
Determines if a file has the requested access permissions.
Definition winutf8.h:224