~ubuntu-branches/debian/sid/gsmartcontrol/sid

« back to all changes in this revision

Viewing changes to src/hz/win32_tools.h

  • Committer: Bazaar Package Importer
  • Author(s): Giuseppe Iuculano
  • Date: 2011-07-15 14:59:29 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110715145929-2o1o4phm5w1rwttz
Tags: 0.8.6-1
* [dbb993d] Updated my email address and removed DM-Upload-Allowed
  control field
* [b681b5b] Imported Upstream version 0.8.6
* [ab9bb7a] Refreshed patches
* [a909506] Bump to Standards-Version 3.9.2, no changes needed
* [48dd13d] Switch to dpkg-source 3.0 (quilt) format

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**************************************************************************
2
2
 Copyright:
3
 
      (C) 2003 - 2009  Alexander Shaduri <ashaduri 'at' gmail.com>
 
3
      (C) 2003 - 2011  Alexander Shaduri <ashaduri 'at' gmail.com>
4
4
 License: See LICENSE_zlib.txt file
5
5
***************************************************************************/
 
6
/// \file
 
7
/// \author Alexander Shaduri
 
8
/// \ingroup hz
 
9
/// \weakgroup hz
 
10
/// @{
6
11
 
7
12
#ifndef HZ_WIN32_TOOLS_H
8
13
#define HZ_WIN32_TOOLS_H
26
31
 
27
32
 
28
33
 
 
34
/**
 
35
\file
 
36
\brief Win32-specific API functions.
 
37
\note The public API works with UTF-8 strings, unless noted otherwise.
 
38
*/
 
39
 
 
40
 
 
41
 
29
42
namespace hz {
30
43
 
31
44
 
32
 
// Note: The public API works with UTF-8 strings, unless noted otherwise.
33
 
 
34
 
 
35
 
// Get a list of drives available for the user. The drives are in
36
 
// "C:\" format (uppercase, utf-8).
 
45
/// Get a list of drives available for the user. The drives are in
 
46
/// "C:\" format (uppercase, utf-8).
37
47
template<class Container> inline
38
48
bool win32_get_drive_list(Container& put_here);
39
49
 
40
50
 
41
 
// Get windows "special" folder by CSIDL (CSIDL_APPDATA, for example).
42
 
// See http://msdn.microsoft.com/en-us/library/bb762494(VS.85).aspx
43
 
// for details.
 
51
/// Get windows "special" folder by CSIDL (CSIDL_APPDATA, for example).
 
52
/// See http://msdn.microsoft.com/en-us/library/bb762494(VS.85).aspx
 
53
/// for details.
44
54
inline std::string win32_get_special_folder(int csidl, bool auto_create = true);
45
55
 
46
56
 
47
 
// Usually C:\Windows or something like that.
 
57
/// Usually C:\\Windows or something like that.
48
58
inline std::string win32_get_windows_directory();
49
59
 
50
60
 
51
 
// base may be e.g. HKEY_CURRENT_USER.
52
 
// Note that this works only with REG_SZ (string) types,
53
 
// returning their value as utf-8. False is returned for all other types.
 
61
/// Get registry value as a string.
 
62
/// Base may be e.g. HKEY_CURRENT_USER.
 
63
/// Note that this works only with REG_SZ (string) types,
 
64
/// returning their value as utf-8. False is returned for all other types.
54
65
inline bool win32_get_registry_value_string(HKEY base,
55
66
                const std::string& keydir, const std::string& key, std::string& put_here);
56
67
 
57
68
 
58
 
// Note that this sets the type as REG_SZ (string). The accepted utf-8
59
 
// value is converted to utf-16 for storage.
 
69
/// Set registry value as a string.
 
70
/// Note that this sets the type as REG_SZ (string). The accepted utf-8
 
71
/// value is converted to utf-16 for storage.
60
72
inline bool win32_set_registry_value_string(HKEY base,
61
73
                const std::string& keydir, const std::string& key, const std::string& value);
62
74
 
63
75
 
64
 
// Redirect stdout and stderr to console window (if open).
65
 
inline bool win32_redirect_stdio_to_console();
66
 
 
67
 
 
68
 
// Redirect stdout to stdout.txt and stderr to stderr.txt.
69
 
// Call once.
 
76
/// Redirect stdout and stderr to console window (if open). Requires winxp (at compile-time).
 
77
/// \param create_if_none if true, create a new console if none was found and attach to it.
 
78
/// \return false if failed or unsupported.
 
79
inline bool win32_redirect_stdio_to_console(bool create_if_none = false);
 
80
 
 
81
 
 
82
/// Redirect stdout and stderr to console window (if open). Requires winxp (at compile-time).
 
83
/// \param create_if_none if true, create a new console if none was found and attach to it.
 
84
/// \param console_created Set to true if a new console was created due to \c create_if_none.
 
85
/// \return false if failed or unsupported.
 
86
inline bool win32_redirect_stdio_to_console(bool create_if_none, bool& console_created);
 
87
 
 
88
 
 
89
/// Redirect stdout to stdout.txt and stderr to stderr.txt.
 
90
/// Call once.
 
91
/// \return true on success, false on failure.
70
92
inline bool win32_redirect_stdio_to_files(std::string stdout_file = "", std::string stderr_file = "");
71
93
 
72
94
 
73
 
// Convert a string in user-specified encoding to utf-16 string (00-terminated array of wchar_t).
74
 
// wchar_t is 2 bytes on windows, thus holding utf-16 code unit.
75
 
// Caller must delete[] the returned value.
76
 
// returned_buf_size (if not 0) will be set to the size of returned buffer
77
 
// (in wchar_t; including terminating 00). The byte size will be (returned_buf_size*2).
 
95
/// Convert a string in user-specified encoding to utf-16 string (00-terminated array of wchar_t).
 
96
/// wchar_t is 2 bytes on windows, thus holding utf-16 code unit.
 
97
/// Caller must delete[] the returned value.
 
98
/// returned_buf_size (if not 0) will be set to the size of returned buffer
 
99
/// (in wchar_t; including terminating 00). The byte size will be (returned_buf_size*2).
78
100
inline wchar_t* win32_user_to_utf16(UINT from_cp, const char* from_str, unsigned int* returned_buf_size = 0);
79
101
 
80
102
 
81
 
// Convert utf-16 string (represented as 0-terminated array of wchar_t, where
82
 
// wchar_t is 2 bytes (on windows)) to a string in user-specified encoding.
83
 
// Caller must delete[] the returned value.
84
 
// The size of the returned buffer is std::strlen(buf) + 1.
85
 
// returned_buf_size (if not 0) will be set to the size of returned buffer
86
 
// (in char; including terminating 0).
 
103
/// Convert utf-16 string (represented as 0-terminated array of wchar_t, where
 
104
/// wchar_t is 2 bytes (on windows)) to a string in user-specified encoding.
 
105
/// Caller must delete[] the returned value.
 
106
/// The size of the returned buffer is std::strlen(buf) + 1.
 
107
/// returned_buf_size (if not 0) will be set to the size of returned buffer
 
108
/// (in char; including terminating 0).
87
109
inline char* win32_utf16_to_user(UINT to_cp, const wchar_t* utf16_str, unsigned int* returned_buf_size = 0);
88
110
 
89
111
 
90
 
// Convert utf-8 string to utf-16 string. See win32_user_to_utf16() for details.
 
112
/// Convert utf-8 string to utf-16 string. See win32_user_to_utf16() for details.
91
113
inline wchar_t* win32_utf8_to_utf16(const char* utf8_str, unsigned int* returned_buf_size = 0);
92
114
 
93
115
 
94
 
// Convert utf-16 string to utf-8 string. See win32_utf16_to_user() for details.
 
116
/// Convert utf-16 string to utf-8 string. See win32_utf16_to_user() for details.
95
117
inline char* win32_utf16_to_utf8(const wchar_t* utf16_str, unsigned int* returned_buf_size = 0);
96
118
 
97
119
 
98
 
// Same as win32_utf16_to_utf8(), but safer since it never returns 0 (it returns "" instead).
99
 
// Note that we don't provide non-utf8 versions of this function, because the other charsets
100
 
// may actually be multi-byte (std::string can't hold their terminating 0 properly).
101
 
// Also, we don't provide std::wstring versions for utf16, because they are not always
102
 
// supported by non-MS compilers (mingw, for example).
 
120
/// Same as win32_utf16_to_utf8(), but safer since it never returns 0 (it returns "" instead).
 
121
/// Note that we don't provide non-utf8 versions of this function, because the other charsets
 
122
/// may actually be multi-byte (std::string can't hold their terminating 0 properly).
 
123
/// Also, we don't provide std::wstring versions for utf16, because they are not always
 
124
/// supported by non-MS compilers (mingw, for example).
103
125
inline std::string win32_utf16_to_utf8_string(const wchar_t* utf16_str);
104
126
 
105
127
 
106
 
// Convert current locale-encoded string to utf-16 string. See win32_user_to_utf16() for details.
107
 
// Use CP_ACP is system default windows ANSI codepage.
108
 
// Use CP_THREAD_ACP is for current thread. Think before you choose. :)
 
128
/// Convert current locale-encoded string to utf-16 string. See win32_user_to_utf16() for details.
 
129
/// Use CP_ACP is system default windows ANSI codepage.
 
130
/// Use CP_THREAD_ACP is for current thread. Think before you choose. :)
109
131
inline wchar_t* win32_locale_to_utf16(const char* loc_str, unsigned int* returned_buf_size = 0,
110
132
                bool use_thread_locale = false);
111
133
 
112
134
 
113
 
// Convert utf-16 string to locale-encoded string. See win32_utf16_to_user() for details.
 
135
/// Convert utf-16 string to locale-encoded string. See win32_utf16_to_user() for details.
114
136
inline char* win32_utf16_to_locale(const wchar_t* utf16_str, unsigned int* returned_buf_size = 0,
115
137
                bool use_thread_locale = false);
116
138
 
117
139
 
118
 
// Convert current locale-encoded string to utf-8 string. The returned value must be freed by caller.
 
140
/// Convert current locale-encoded string to utf-8 string. The returned value must be freed by caller.
119
141
inline char* win32_locale_to_utf8(const char* loc_str, unsigned int* returned_buf_size = 0,
120
142
                bool use_thread_locale = false);
121
143
 
122
144
 
123
 
// Convert utf-8 string to locale-encoded string. See win32_utf8_to_user() for details.
124
 
inline char* win32_utf8_to_locale(const wchar_t* utf8_str, unsigned int* returned_buf_size = 0,
 
145
/// Convert utf-8 string to locale-encoded string. See win32_utf8_to_user() for details.
 
146
inline char* win32_utf8_to_locale(const char* utf8_str, unsigned int* returned_buf_size = 0,
125
147
                bool use_thread_locale = false);
126
148
 
127
149
 
329
351
 
330
352
 
331
353
 
332
 
 
333
 
// Redirect stdout and stderr to console window (if open).
334
 
inline bool win32_redirect_stdio_to_console()
335
 
{
 
354
// Redirect stdout and stderr to console window (if open).
 
355
inline bool win32_redirect_stdio_to_console(bool create_if_none)
 
356
{
 
357
        bool console_created = false;
 
358
        return win32_redirect_stdio_to_console(create_if_none, console_created);
 
359
}
 
360
 
 
361
 
 
362
 
 
363
// Redirect stdout and stderr to console window (if open).
 
364
inline bool win32_redirect_stdio_to_console(bool create_if_none, bool& console_created)
 
365
{
 
366
        console_created = false;
 
367
 
336
368
        // AttachConsole is since winxp (note that mingw and MS headers say win2k,
337
 
        // which is incorrect).
 
369
        // which is incorrect; msdn is correct).
338
370
#if defined WINVER && WINVER >= 0x0501
339
371
        if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
340
 
                return false;
 
372
                if (create_if_none) {
 
373
                        // Even though msdn says that stdio is redirected to the new console,
 
374
                        // it isn't so and we must do it manually.
 
375
                        if (!AllocConsole()) {
 
376
                                return false;
 
377
                        } else {
 
378
                                console_created = true;
 
379
                        }
 
380
                } else {
 
381
                        return false;
 
382
                }
341
383
        }
342
384
 
343
385
        // redirect unbuffered STDOUT to the console
384
426
 
385
427
 
386
428
 
387
 
        // returns full path (in utf-16) to the output filename (in utf-8)
 
429
        /// Returns full path (in utf-16) to the output filename (in utf-8)
388
430
        inline wchar_t* win32_get_std_output_file(const char* base)
389
431
        {
390
432
                if (!base)
417
459
 
418
460
 
419
461
 
420
 
        // Remove the output files if there was no output written.
421
 
        // This is an atexit() callback.
 
462
        /// Remove the output files if there was no output written.
 
463
        /// This is an atexit() callback.
422
464
        extern "C" inline void win32_redirect_stdio_to_files_cleanup()
423
465
        {
424
466
                // Flush the output in case anything is queued
688
730
#endif  // win32
689
731
 
690
732
#endif  // hg
 
733
 
 
734
/// @}