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