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#include <vector>
13#include <utf8/utf8.h>
14
15#undef MessageBox
16#undef CopyFile
17#undef LoadString
18#undef ShellExecute
19#undef GetTempPath
20#undef GetTempFileName
21#undef GetFullPathName
22#undef GetModuleFileName
23#undef RegCreateKey
24#undef RegDeleteKey
25#undef RegOpenKey
26#undef RegSetValue
27#undef RegQueryValue
28#undef RegGetValue
29#undef RegDeleteTree
30#undef RegEnumKey
31#undef RegDeleteValue
32#undef RegEnumValue
33
34namespace utf8 {
35
36std::vector<std::string> get_argv ();
37char** get_argv (int* argc);
38void free_argv (int argc, char** argv);
39
40bool chmod (const char* filename, int mode);
41bool chmod (const std::string& filename, int mode);
42
43bool access (const char* filename, int mode);
44bool access (const std::string& filename, int mode);
45
46bool splitpath (const std::string& path, char* drive, char* dir, char* fname, char* ext);
47bool splitpath (const std::string& path, std::string& drive, std::string& dir,
48 std::string& fname, std::string& ext);
49bool makepath (std::string& path, const std::string& drive, const std::string& dir,
50 const std::string& fname, const std::string& ext);
51std::string fullpath (const std::string& relpath);
52
53std::string getenv (const std::string& var);
54bool putenv (const std::string& str);
55bool putenv (const std::string& var, const std::string& val);
56
57bool symlink (const char* path, const char* link, bool directory);
58bool symlink (const std::string& path, const std::string& target, bool directory);
59
60int system (const std::string& cmd);
61
62int MessageBox (HWND hWnd, const std::string& text, const std::string& caption,
63 unsigned int type);
64bool CopyFile (const std::string& from, const std::string& to, bool fail_exist);
65std::string LoadString (UINT id, HINSTANCE hInst = NULL);
66
67//registry functions
68LSTATUS RegCreateKey (HKEY key, const std::string& subkey, HKEY& result,
69 DWORD options = REG_OPTION_NON_VOLATILE, REGSAM sam = KEY_ALL_ACCESS,
70 const SECURITY_ATTRIBUTES* psa = nullptr, DWORD* disp = nullptr);
71LSTATUS RegOpenKey (HKEY key, const std::string& subkey, HKEY& result,
72 REGSAM sam = KEY_ALL_ACCESS, bool link = false);
73LSTATUS RegDeleteKey (HKEY key, const std::string& subkey, REGSAM sam = 0);
74LSTATUS RegDeleteValue (HKEY key, const std::string& value);
75LSTATUS RegDeleteTree (HKEY key, const std::string& subkey = std::string ());
76LSTATUS RegRenameKey (HKEY key, const std::string& subkey, const std::string& new_name);
77LSTATUS RegSetValue (HKEY key, const std::string& value, DWORD type, const void* data, DWORD size);
78LSTATUS RegSetValue (HKEY key, const std::string& value, const std::string& data);
79LSTATUS RegSetValue (HKEY key, const std::string& value, const std::vector<std::string>& data);
80LSTATUS RegQueryValue (HKEY key, const std::string& value, DWORD* type, void* data, DWORD* size);
81LSTATUS RegGetValue (HKEY key, const std::string& subkey, const std::string& value,
82 DWORD flags, void* data, DWORD* size, DWORD* type = NULL);
83LSTATUS RegGetValue (HKEY key, const std::string& subkey, const std::string& value,
84 std::string& data, bool expand = false);
85LSTATUS RegGetValue (HKEY key, const std::string& subkey, const std::string& value,
86 std::vector<std::string>& data);
87LSTATUS RegEnumKey (HKEY key, DWORD index, std::string& name, DWORD maxlen = 0, FILETIME* last_write_time = NULL);
88LSTATUS RegEnumKey (HKEY key, std::vector<std::string>& names);
89LSTATUS RegEnumValue (HKEY key, DWORD index, std::string& value, DWORD maxlen = 0, DWORD* type = 0, void* data = 0, DWORD* data_len = 0);
90LSTATUS RegEnumValue (HKEY key, std::vector<std::string>& values);
91
92HINSTANCE ShellExecute (const std::string& file, const std::string& verb = std::string (),
93 const std::string& parameters = std::string (),
94 const std::string& directory = std::string ("."),
95 HWND hWnd = NULL, int show = SW_SHOW);
96std::string GetTempPath ();
97std::string GetTempFileName (const std::string& path, const std::string& prefix, UINT unique=0);
98std::string GetFullPathName (const std::string& rel_path);
99bool GetModuleFileName (HMODULE hModule, std::string& filename);
100std::string GetModuleFileName (HMODULE hModule = NULL);
101
103struct find_data {
104 find_data ()
105 : handle{ INVALID_HANDLE_VALUE }
106 , attributes{ 0 }
107 , creation_time{ 0, 0 }
108 , access_time{ 0,0 }
109 , write_time{ 0,0 }
110 , size{ 0 }
111 {}
112 HANDLE handle;
114 FILETIME creation_time;
115 FILETIME access_time;
116 FILETIME write_time;
117 int64_t size;
118 std::string filename;
119 std::string short_name;
120};
121
122bool find_first (const std::string& name, find_data& fdat);
123bool find_next (find_data& fdat);
124void find_close (find_data& fdat);
125
142class file_enumerator : protected find_data
143{
144public:
145 explicit file_enumerator (const std::string& name);
147 bool ok () const;
148 bool next ();
149
150 operator bool () const;
151
156 using find_data::size;
159};
160
162class buffer {
163public:
164 explicit buffer (size_t size_);
165 ~buffer ();
166 buffer (const buffer& other);
167 buffer& operator = (const buffer& rhs);
168 buffer& operator = (const std::string& rhs);
169 operator wchar_t* ();
170 operator std::string () const;
171 DWORD size () const;
172
173private:
174 wchar_t *ptr;
175 size_t sz;
176};
177
178inline std::ostream&
179operator << (std::ostream& s, const buffer& b)
180{
181 s << (std::string)b;
182 return s;
183}
184
185
186//--------------------- INLINE FUNCTIONS --------------------------------------
187
198inline
199bool chmod (const char* filename, int mode)
200{
201 return (_wchmod (widen (filename).c_str (), mode) == 0);
202}
203
205inline
206bool chmod (const std::string& filename, int mode)
207{
208 return (_wchmod (widen (filename).c_str (), mode) == 0);
209}
210
211
225inline
226bool access (const char* filename, int mode)
227{
228 return (_waccess (widen (filename).c_str (), mode) == 0);
229}
230
232inline
233bool access (const std::string& filename, int mode)
234{
235 return (_waccess (widen (filename).c_str (), mode) == 0);
236}
237
238
247inline
248bool putenv (const std::string& str)
249{
250 return (_wputenv (utf8::widen (str).c_str ()) == 0);
251}
252
263inline
264bool putenv (const std::string& var, const std::string& val)
265{
266 return (_wputenv_s (widen (var).c_str (),
267 widen (val).c_str ()) == 0);
268}
269
278inline
279int system (const std::string& cmd)
280{
281 std::wstring wcmd = utf8::widen (cmd);
282 return _wsystem (wcmd.c_str ());
283}
284
285// -------------------- file_enumerator ------------------------------------
286
288inline
289file_enumerator::file_enumerator (const std::string& name)
290{
291 find_first (name, *this);
292}
293
295inline
297{
298 if (handle != INVALID_HANDLE_VALUE)
299 find_close (*this);
300}
301
303inline
305{
306 return (handle != INVALID_HANDLE_VALUE);
307}
308
311inline
312file_enumerator::operator bool () const
313{
314 return ok ();
315}
316
318inline
320{
321 return find_next (*this);
322}
323
324//---------------------------- buffer ----------------------------------------
325
327inline
328 buffer::operator wchar_t* ()
329{
330 return ptr;
331}
332
334inline
335 buffer::operator std::string () const
336{
337 return utf8::narrow (ptr);
338}
339
341inline DWORD
343{
344 return (DWORD)sz;
345}
346
347} //end namespace
A simple buffer for caching values returned by Windows API.
Definition winutf8.h:162
buffer & operator=(const buffer &rhs)
Principal assignment operator.
Definition buffer.cpp:71
DWORD size() const
Return buffer size.
Definition winutf8.h:342
~buffer()
Destructor.
Definition buffer.cpp:55
An object - oriented wrapper for find_... functions.
Definition winutf8.h:143
file_enumerator(const std::string &name)
Constructs a file_enumerator object and tries to locate the first file.
Definition winutf8.h:289
bool next()
Advance the enumerator to next file.
Definition winutf8.h:319
bool ok() const
Return true if a file has been enumerated.
Definition winutf8.h:304
~file_enumerator()
Closes the search handle associated with this object.
Definition winutf8.h:296
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:103
std::string filename
file name
Definition winutf8.h:118
DWORD attributes
file attributes
Definition winutf8.h:113
HANDLE handle
search handle
Definition winutf8.h:112
FILETIME access_time
file last access time
Definition winutf8.h:115
int64_t size
file size
Definition winutf8.h:117
std::string short_name
8.3 file name
Definition winutf8.h:119
FILETIME creation_time
file creation time
Definition winutf8.h:114
FILETIME write_time
file last write time
Definition winutf8.h:116
UTF-8 Conversion functions.
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:248
bool chmod(const char *filename, int mode)
Changes the file access permissions.
Definition winutf8.h:199
int system(const std::string &cmd)
Passes command to command interpreter.
Definition winutf8.h:279
bool access(const char *filename, int mode)
Determines if a file has the requested access permissions.
Definition winutf8.h:226