Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sysdep.h
Go to the documentation of this file.
1 /* Copyright (c) 2010 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 #include "lib/debug.h" // ErrorReactionInternal
31 #include "lib/os_path.h"
32 
33 #include <cstdarg> // needed for sys_vswprintf
34 
35 
36 //
37 // output
38 //
39 
40 /**
41  * display a message.
42  *
43  * @param caption title message
44  * @param msg message contents
45  *
46  * implemented as a MessageBox on Win32 and printf on Unix.
47  * called from debug_DisplayMessage.
48  **/
49 extern void sys_display_msg(const wchar_t* caption, const wchar_t* msg);
50 
51 /**
52  * show the error dialog.
53  *
54  * @param text to display (practically unlimited length)
55  * @param flags: see DebugDisplayErrorFlags.
56  * @return ErrorReactionInternal (except ERI_EXIT, which is acted on immediately)
57  *
58  * called from debug_DisplayError unless overridden by means of
59  * ah_display_error.
60  **/
61 extern ErrorReactionInternal sys_display_error(const wchar_t* text, size_t flags);
62 
63 
64 //
65 // misc
66 //
67 
68 /**
69  * @return whether a debugger is attached to the process
70  * (if so, it is safe to use debug_break; otherwise, that would
71  * raise an exception)
72  **/
73 LIB_API bool sys_IsDebuggerPresent();
74 
75 /**
76  * @return a wide string conversion of the platform's encoding of main's argv.
77  *
78  * (NB: wseh.cpp defines a wmain that converts argv to UTF-8 and calls main(),
79  * but only if LIB_STATIC_LINK)
80  **/
81 LIB_API std::wstring sys_WideFromArgv(const char* argv_i);
82 
83 /**
84  * sys_vswprintf: doesn't quite follow the standard for vswprintf, but works
85  * better across compilers:
86  * - handles positional parameters and %lld
87  * - always null-terminates the buffer, if count > 0
88  * - returns -1 on overflow (if the output string (including null) does not fit in the buffer)
89  **/
90 extern int sys_vswprintf(wchar_t* buffer, size_t count, const wchar_t* format, va_list argptr);
91 
92 /**
93  * describe the current OS error state.
94  *
95  * @param err: if not 0, use that as the error code to translate; otherwise,
96  * uses GetLastError or similar.
97  * @param buf output buffer
98  * @param max_chars
99  *
100  * rationale: it is expected to be rare that OS return/error codes are
101  * actually seen by user code, but we leave the possibility open.
102  **/
103 extern Status sys_StatusDescription(int err, wchar_t* buf, size_t max_chars);
104 
105 /**
106  * determine filename of the module to whom an address belongs.
107  *
108  * @param addr
109  * @param pathname Full path to module (unchanged unless INFO::OK is returned).
110  * @return Status
111  *
112  * note: this is useful for handling exceptions in other modules.
113  **/
114 Status sys_get_module_filename(void* addr, OsPath& pathname);
115 
116 /**
117  * @return full pathname of the current executable.
118  *
119  * this is useful for determining installation directory, e.g. for VFS.
120  **/
122 
123 /**
124  * Get the current user's login name.
125  *
126  * @return login name, or empty string on error
127  */
128 extern std::wstring sys_get_user_name();
129 
130 /**
131  * Have the user choose a directory via OS dialog.
132  *
133  * @param path Path's input value determines the starting directory for
134  * faster browsing. if INFO::OK is returned, it receives
135  * chosen directory path.
136  **/
137 extern Status sys_pick_directory(OsPath& path);
138 
139 /**
140  * Open the user's default web browser to the given URL.
141  **/
142 extern Status sys_open_url(const std::string& url);
143 
144 /**
145  * return the largest sector size [bytes] of any storage medium
146  * (HD, optical, etc.) in the system.
147  *
148  * this may be a bit slow to determine (iterates over all drives),
149  * but caches the result so subsequent calls are free.
150  * (caveat: device changes won't be noticed during this program run)
151  *
152  * sector size is relevant because Windows aio requires all IO
153  * buffers, offsets and lengths to be a multiple of it. this requirement
154  * is also carried over into the vfs / file.cpp interfaces for efficiency
155  * (avoids the need for copying to/from align buffers).
156  *
157  * waio uses the sector size to (in some cases) align IOs if
158  * they aren't already, but it's also needed by user code when
159  * aligning their buffers to meet the requirements.
160  *
161  * the largest size is used so that we can read from any drive. while this
162  * is a bit wasteful (more padding) and requires iterating over all drives,
163  * it is the only safe way: this may be called before we know which
164  * drives will be needed, and hardlinks may confuse things.
165  **/
166 extern size_t sys_max_sector_size();
167 
168 /**
169  * generate high-quality random bytes.
170  *
171  * this should only be used with small numbers of bytes, to avoid
172  * hogging the system's entropy.
173  **/
174 LIB_API Status sys_generate_random_bytes(u8* buf, size_t count);
175 
176 /**
177  * get the proxy address for accessing the given HTTP URL.
178  *
179  * this may be very slow (tens of seconds).
180  *
181  * @return INFO::OK on success; INFO::SKIPPED if no proxy found.
182  **/
183 LIB_API Status sys_get_proxy_config(const std::wstring& url, std::wstring& proxy);
184 
185 /**
186  * open a file like with fopen (but taking an OsPath argument).
187  */
188 LIB_API FILE* sys_OpenFile(const OsPath& pathname, const char* mode);
189 
190 /**
191  * directory separation character
192  **/
193 #if OS_WIN
194 # define SYS_DIR_SEP '\\'
195 #else
196 # define SYS_DIR_SEP '/'
197 #endif
198 
199 #endif // #ifndef INCLUDED_SYSDEP
#define u8
Definition: types.h:39
std::wstring sys_get_user_name()
Get the current user&#39;s login name.
Definition: unix.cpp:295
ErrorReactionInternal sys_display_error(const wchar_t *text, size_t flags)
show the error dialog.
Definition: unix.cpp:202
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:338
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:286
FILE * sys_OpenFile(const OsPath &pathname, const char *mode)
open a file like with fopen (but taking an OsPath argument).
Definition: unix.cpp:373
Status sys_StatusDescription(int err, wchar_t *buf, size_t max_chars)
describe the current OS error state.
Definition: unix.cpp:271
Status sys_open_url(const std::string &url)
Open the user&#39;s default web browser to the given URL.
Definition: unix.cpp:343
Definition: path.h:75
void sys_display_msg(const wchar_t *caption, const wchar_t *msg)
display a message.
Definition: unix.cpp:60
Status sys_pick_directory(OsPath &path)
Have the user choose a directory via OS dialog.
Definition: wsysdep.cpp:428
Status sys_get_module_filename(void *addr, OsPath &pathname)
determine filename of the module to whom an address belongs.
Definition: wsysdep.cpp:384
std::wstring sys_WideFromArgv(const char *argv_i)
Definition: unix.cpp:51
i64 Status
Error handling system.
Definition: status.h:171
Status sys_generate_random_bytes(u8 *buf, size_t count)
generate high-quality random bytes.
Definition: unix.cpp:318
bool sys_IsDebuggerPresent()
Definition: unix.cpp:46
OsPath sys_ExecutablePathname()
Definition: bsd.cpp:33
int sys_vswprintf(wchar_t *buffer, size_t count, const wchar_t *format, va_list argptr)
sys_vswprintf: doesn&#39;t quite follow the standard for vswprintf, but works better across compilers: ...
Definition: printf.cpp:30
ErrorReactionInternal
all choices offered by the error dialog.
Definition: debug.h:154