Pyrogenesis  trunk
sysdep.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 Wildfire Games.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * various system-specific function implementations
25  */
26 
27 #ifndef INCLUDED_SYSDEP
28 #define INCLUDED_SYSDEP
29 
30 #ifdef _MSC_VER
31 # pragma warning(disable:4091) // hides previous local declaration
32 #endif
33 
34 #include "lib/debug.h" // ErrorReactionInternal
35 #include "lib/os_path.h"
36 
37 
38 //
39 // output
40 //
41 
42 /**
43  * display a message.
44  *
45  * @param caption title message
46  * @param msg message contents
47  *
48  * implemented as a MessageBox on Win32 and printf on Unix.
49  * called from debug_DisplayMessage.
50  **/
51 extern void sys_display_msg(const wchar_t* caption, const wchar_t* msg);
52 
53 /**
54  * show the error dialog.
55  *
56  * @param text to display (practically unlimited length)
57  * @param flags: see DebugDisplayErrorFlags.
58  * @return ErrorReactionInternal (except ERI_EXIT, which is acted on immediately)
59  *
60  * called from debug_DisplayError unless overridden by means of
61  * ah_display_error.
62  **/
63 extern ErrorReactionInternal sys_display_error(const wchar_t* text, size_t flags);
64 
65 
66 //
67 // misc
68 //
69 
70 /**
71  * @return whether a debugger is attached to the process
72  * (if so, it is safe to use debug_break; otherwise, that would
73  * raise an exception)
74  **/
76 
77 /**
78  * @return a wide string conversion of the platform's encoding of main's argv.
79  *
80  * (NB: wseh.cpp defines a wmain that converts argv to UTF-8 and calls main())
81  **/
82 std::wstring sys_WideFromArgv(const char* argv_i);
83 
84 /**
85  * describe the current OS error state.
86  *
87  * @param err: if not 0, use that as the error code to translate; otherwise,
88  * uses GetLastError or similar.
89  * @param buf output buffer
90  * @param max_chars
91  *
92  * rationale: it is expected to be rare that OS return/error codes are
93  * actually seen by user code, but we leave the possibility open.
94  **/
95 extern Status sys_StatusDescription(int err, wchar_t* buf, size_t max_chars);
96 
97 /**
98  * determine filename of the module to whom an address belongs.
99  *
100  * @param addr
101  * @param pathname Full path to module (unchanged unless INFO::OK is returned).
102  * @return Status
103  *
104  * note: this is useful for handling exceptions in other modules.
105  **/
106 Status sys_get_module_filename(void* addr, OsPath& pathname);
107 
108 /**
109  * @return full pathname of the current executable.
110  *
111  * this is useful for determining installation directory, e.g. for VFS.
112  **/
114 
115 /**
116  * Get the current user's login name.
117  *
118  * @return login name, or empty string on error
119  */
120 extern std::wstring sys_get_user_name();
121 
122 /**
123  * Have the user choose a directory via OS dialog.
124  *
125  * @param path Path's input value determines the starting directory for
126  * faster browsing. if INFO::OK is returned, it receives
127  * chosen directory path.
128  **/
129 extern Status sys_pick_directory(OsPath& path);
130 
131 /**
132  * Open the user's default web browser to the given URL.
133  **/
134 extern Status sys_open_url(const std::string& url);
135 
136 /**
137  * return the largest sector size [bytes] of any storage medium
138  * (HD, optical, etc.) in the system.
139  *
140  * this may be a bit slow to determine (iterates over all drives),
141  * but caches the result so subsequent calls are free.
142  * (caveat: device changes won't be noticed during this program run)
143  *
144  * sector size is relevant because Windows aio requires all IO
145  * buffers, offsets and lengths to be a multiple of it. this requirement
146  * is also carried over into the vfs / file.cpp interfaces for efficiency
147  * (avoids the need for copying to/from align buffers).
148  *
149  * waio uses the sector size to (in some cases) align IOs if
150  * they aren't already, but it's also needed by user code when
151  * aligning their buffers to meet the requirements.
152  *
153  * the largest size is used so that we can read from any drive. while this
154  * is a bit wasteful (more padding) and requires iterating over all drives,
155  * it is the only safe way: this may be called before we know which
156  * drives will be needed, and hardlinks may confuse things.
157  **/
158 extern size_t sys_max_sector_size();
159 
160 /**
161  * generate high-quality random bytes.
162  *
163  * this should only be used with small numbers of bytes, to avoid
164  * hogging the system's entropy.
165  **/
166 Status sys_generate_random_bytes(u8* buf, size_t count);
167 
168 /**
169  * get the proxy address for accessing the given HTTP URL.
170  *
171  * this may be very slow (tens of seconds).
172  *
173  * @return INFO::OK on success; INFO::SKIPPED if no proxy found.
174  **/
175 Status sys_get_proxy_config(const std::wstring& url, std::wstring& proxy);
176 
177 /**
178  * open a file like with fopen (but taking an OsPath argument).
179  */
180 FILE* sys_OpenFile(const OsPath& pathname, const char* mode);
181 
182 /**
183  * directory separation character
184  **/
185 #if OS_WIN
186 # define SYS_DIR_SEP '\\'
187 #else
188 # define SYS_DIR_SEP '/'
189 #endif
190 
191 #endif // #ifndef INCLUDED_SYSDEP
Status sys_get_proxy_config(const std::wstring &url, std::wstring &proxy)
get the proxy address for accessing the given HTTP URL.
Definition: unix.cpp:346
std::wstring sys_get_user_name()
Get the current user's login name.
Definition: unix.cpp:300
Status sys_generate_random_bytes(u8 *buf, size_t count)
generate high-quality random bytes.
Definition: unix.cpp:323
size_t sys_max_sector_size()
return the largest sector size [bytes] of any storage medium (HD, optical, etc.) in the system...
Definition: unix.cpp:291
Status sys_StatusDescription(int err, wchar_t *buf, size_t max_chars)
describe the current OS error state.
Definition: unix.cpp:276
uint8_t u8
Definition: types.h:37
FILE * sys_OpenFile(const OsPath &pathname, const char *mode)
open a file like with fopen (but taking an OsPath argument).
Definition: unix.cpp:381
void sys_display_msg(const wchar_t *caption, const wchar_t *msg)
display a message.
Definition: unix.cpp:61
bool sys_IsDebuggerPresent()
Definition: unix.cpp:47
Definition: path.h:79
Status sys_open_url(const std::string &url)
Open the user's default web browser to the given URL.
Definition: unix.cpp:351
i64 Status
Error handling system.
Definition: status.h:169
Status sys_pick_directory(OsPath &path)
Have the user choose a directory via OS dialog.
Definition: wsysdep.cpp:460
Status sys_get_module_filename(void *addr, OsPath &pathname)
determine filename of the module to whom an address belongs.
Definition: wsysdep.cpp:416
OsPath sys_ExecutablePathname()
Definition: bsd.cpp:28
ErrorReactionInternal sys_display_error(const wchar_t *text, size_t flags)
show the error dialog.
Definition: unix.cpp:207
std::wstring sys_WideFromArgv(const char *argv_i)
Definition: unix.cpp:52
ErrorReactionInternal
all choices offered by the error dialog.
Definition: debug.h:153