1 //------------------------------------------------------------------------------
2 // emStd2.h
3 //
4 // Copyright (C) 2004-2011,2014-2015,2018-2020 Oliver Hamann.
5 //
6 // Homepage: http://eaglemode.sourceforge.net/
7 //
8 // This program is free software: you can redistribute it and/or modify it under
9 // the terms of the GNU General Public License version 3 as published by the
10 // Free Software Foundation.
11 //
12 // This program is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
15 // more details.
16 //
17 // You should have received a copy of the GNU General Public License version 3
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 //------------------------------------------------------------------------------
20
21 #ifndef emStd2_h
22 #define emStd2_h
23
24 #include <sys/stat.h>
25
26 #ifndef emArray_h
27 #include <emCore/emArray.h>
28 #endif
29
30 #ifndef emString_h
31 #include <emCore/emString.h>
32 #endif
33
34
35 //==============================================================================
36 //================================ emException =================================
37 //==============================================================================
38
39 class emException {
40
41 public:
42
43 // Class for an exception.
44
45 emException();
46 // Construct an exception with an empty text.
47
48 emException(const char * format, ...) EM_FUNC_ATTR_PRINTF(2);
49 // Construct an exception with a formatted text.
50 // The arguments are like with printf.
51
52 emException(const emException & exception);
53 // Construct a copied exception.
54
55 virtual ~emException();
56 // Destructor.
57
58 emException & operator = (const emException & exception);
59 // Copy an exception.
60
61 const emString & GetText() const;
62 // Get the text.
63
64 private:
65 emString Text;
66 };
67
68
69 //==============================================================================
70 //=========================== Host, user, process id ===========================
71 //==============================================================================
72
73 emString emGetHostName();
74 // Get the name of the computer.
75
76 emString emGetUserName();
77 // Get the name of the user.
78
79 int emGetProcessId();
80 // Get the identification number of the process.
81
82
83 //==============================================================================
84 //================================ SIMD support ================================
85 //==============================================================================
86
87 // If EM_HAVE_X86_INTRINSICS is non-zero, x86 intrinsics headers can be
88 // included.
89 #ifndef EM_HAVE_X86_INTRINSICS
90 # if defined(__i386__) || defined(__i386) || defined(_M_IX86) || \
91 defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64)
92 # define EM_HAVE_X86_INTRINSICS 1
93 # else
94 # define EM_HAVE_X86_INTRINSICS 0
95 # endif
96 #endif
97
98 bool emCanCpuDoAvx2();
99 // Whether the CPU supports AVX2 instructions (including MMX, SSE <= 4.1
100 // and AVX(1)).
101
102
103 //==============================================================================
104 //=============================== Time functions ===============================
105 //==============================================================================
106
107 void emSleepMS(int millisecs);
108 // Sleep for the given number of milliseconds. Less or equal zero means
109 // to yield the CPU to another process.
110
111 emUInt64 emGetClockMS();
112 // Get a system clock time in milliseconds. It starts anywhere, but it
113 // should never overflow.
114
115 emUInt64 emGetCPUTSC();
116 // Get the state of the time stamp counter (TSC) of the CPU.
117 // IMPORTANT: This only works with certain compiler and hardware.
118 // Use for debugging/development only.
119
120
121 //==============================================================================
122 //================================ Error texts =================================
123 //==============================================================================
124
125 emString emGetErrorText(int errorNumber);
126 // This is like the C function strerror, but it is thread-safe, and on
127 // Windows it also supports error codes returned by GetLastError.
128
129
130 //==============================================================================
131 //============================ Files & directories =============================
132 //==============================================================================
133
134 // em_stat and em_lstat are like stat and lstat from sys/stat.h, but with 64-bit
135 // file size field if possible.
136 #if defined(__linux__) && !defined(__SUNPRO_CC)
137 # define em_stat stat64
138 # define em_lstat lstat64
139 #elif defined(_WIN32)
140 # if defined(_MSC_VER) || defined(__GNUC__) || defined(__WATCOMC__)
141 # define em_stat _stati64
142 # else
143 # define em_stat stat
144 # endif
145 # define em_lstat em_stat
146 #else
147 # define em_stat stat
148 # define em_lstat lstat
149 #endif
150
151
152 emString emGetParentPath(const char * path);
153 // Get the parent path of a path.
154 // Examples:
155 // "" => ""
156 // "/" => "/"
157 // "/x" => "/"
158 // "/x/y" => "/x"
159 // "/x/y///" => "/x"
160
161 emString emGetChildPath(const char * path, const char * name);
162 // Join a path and a name.
163 // Examples:
164 // "/x/y", "z" => "x/y/z"
165 // "/x/y/", "z" => "x/y/z"
166
167 emString emGetAbsolutePath(const emString & path, const char * cwd=NULL);
168 // Get the absolute path of a path. Symbolic links are not resolved. The
169 // argument cwd can be used to simulate another current working
170 // directory.
171
172 const char * emGetNameInPath(const char * path);
173 // Return a pointer to the last name in a path (similar to basename).
174
175 const char * emGetExtensionInPath(const char * path);
176 // Return a pointer to the extension including the dot of the last name
177 // in a path.
178
179 bool emIsExistingPath(const char * path);
180 bool emIsReadablePath(const char * path);
181 bool emIsWritablePath(const char * path);
182 bool emIsDirectory(const char * path);
183 bool emIsRegularFile(const char * path);
184 bool emIsSymLinkPath(const char * path);
185 bool emIsHiddenPath(const char * path);
186 // Ask whether the given file path exists, whether it is readable and so
187 // on.
188
189 emUInt64 emTryGetFileSize(const char * path);
190 // Get the size of a file.
191
192 time_t emTryGetFileTime(const char * path);
193 // Get the last modification time of a file.
194
195 emString emGetCurrentDirectory();
196 // Get the absolute path of the current working directory.
197
198 typedef void * emDirHandle;
199 emDirHandle emTryOpenDir(const char * path);
200 emString emTryReadDir(emDirHandle dirHandle);
201 void emCloseDir(emDirHandle dirHandle);
202 // Read a directory step by step. An empty string indicates the end.
203
204 emArray<emString> emTryLoadDir(const char * path);
205 // Read a directory at once.
206
207 emArray<char> emTryLoadFile(const char * path);
208 void emTrySaveFile(const char * path,
209 const char * data, int len);
210 void emTrySaveFile(const char * path,
211 const emArray<char> & data);
212 // Read or write a file at once.
213
214 void emTryMakeDirectories(const char * path, int mode=0777);
215 // Create a directory and its ancestors, as far as they do not exist.
216 // On Windows, the mode argument is ignored.
217
218 void emTryRemoveFile(const char * path);
219 // Delete a file.
220
221 void emTryRemoveDirectory(const char * path);
222 // Delete an empty directory.
223
224 void emTryRemoveFileOrTree(const char * path, bool force=false);
225 // Delete a file or a directory recursively. force=true means to defeat
226 // file permissions if possible.
227
228 void emTryCopyFileOrTree(const char * targetPath, const char * sourcePath);
229 // Copy a file or a directory recursively. This does not copy any file
230 // attributes (maybe a future version will do so).
231
232
233 //==============================================================================
234 //=========================== Dynamic link libraries ===========================
235 //==============================================================================
236
237 typedef void * emLibHandle;
238 // Data type for a handle on an opened dynamic library.
239
240 emLibHandle emTryOpenLib(const char * libName, bool isFilename);
241 // Open a dynamic library.
242 // Arguments:
243 // libName - Name of the dynamic library.
244 // isFilename - false if libName is just a pure name, which has to be
245 // extended for making a file name (e.g. "Test" =>
246 // "libTest.so" or "Test.dll"). true if the name is
247 // already a file name or a file path.
248 // Returns:
249 // An abstract handle for the opened library.
250
251 void * emTryResolveSymbolFromLib(emLibHandle handle, const char * symbol);
252 // Get the address of a symbol in a dynamic library.
253 // Hint: C++ symbols have a compiler specific encoding. Best is to use
254 // C symbols only.
255
256 void emCloseLib(emLibHandle handle);
257 // Close a dynamic library.
258
259 void * emTryResolveSymbol(const char * libName, bool isFilename,
260 const char * symbol);
261 // Similar to emTryOpenLib plus emTryResolveSymbolFromLib, but the
262 // library is never closed.
263
264
265 //==============================================================================
266 //=========================== Pseudo random numbers ============================
267 //==============================================================================
268
269 int emGetIntRandom (int minimum, int maximum);
270 unsigned int emGetUIntRandom (unsigned int minimum, unsigned int maximum);
271 emInt64 emGetInt64Random (emInt64 minimum, emInt64 maximum);
272 emUInt64 emGetUInt64Random(emUInt64 minimum, emUInt64 maximum);
273 double emGetDblRandom (double minimum, double maximum);
274 // Get a pseudo random number within the given range.
275 // Cryptographically, this is absolutely not secure.
276
277
278 //==============================================================================
279 //========================== Checksums and hash codes ==========================
280 //==============================================================================
281
282 emUInt32 emCalcAdler32(const char * src, int srcLen, emUInt32 start=1);
283 emUInt32 emCalcCRC32(const char * src, int srcLen, emUInt32 start=0);
284 emUInt64 emCalcCRC64(const char * src, int srcLen, emUInt64 start=0);
285 // Calculate an Adler-32, CRC-32 or CRC-64 "checksum" from some data.
286 // Cryptographically, this is absolutely not secure.
287 // Arguments:
288 // src - Pointer to source data.
289 // srcLen - Number of bytes in the source.
290 // start - Start value (could be the result from a previous call).
291 // Returns:
292 // The CRC value.
293
294 int emCalcHashCode(const char * str, int start=0);
295 // Calculate another kind of "checksum" from a string (simple & fast).
296 // Cryptographically, this is absolutely not secure.
297 // Arguments:
298 // src - The string.
299 // start - Start value (could be the result from a previous call).
300 // Returns:
301 // The hash code.
302
303 emString emCalcHashName(const char * src, int srcLen, int hashLen);
304 // Calculate an any-length hash code ("checksum") from some data. The
305 // result is a character string consisting of letters and digits. It's
306 // okay to ignore letter case in comparisons. Cryptographically, this is
307 // absolutely not secure.
308 // Arguments:
309 // src - Pointer to source data.
310 // srcLen - Number of bytes in the source.
311 // hashLen - Length of the resulting hash name.
312 // Returns:
313 // The hash name. It has hashLen characters and consists of
314 // letters (A-Z) and digits (0-9) only.
315
316
317 //==============================================================================
318 //============================== Implementations ===============================
319 //==============================================================================
320
emException()321 inline emException::emException()
322 {
323 }
324
emException(const emException & exception)325 inline emException::emException(const emException & exception)
326 : Text(exception.Text)
327 {
328 }
329
330 inline emException & emException::operator = (const emException & exception)
331 {
332 Text=exception.Text;
333 return *this;
334 }
335
GetText()336 inline const emString & emException::GetText() const
337 {
338 return Text;
339 }
340
341
342 #endif
343