bzr branch
http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
|
21
by Gustav Hartvigsson
Woops! |
1 |
/*
|
2 |
Copyright (c) 2013-2014 Gustav Hartvigsson
|
|
3 |
||
4 |
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5 |
of this software and associated documentation files (the "Software"), to deal
|
|
6 |
in the Software without restriction, including without limitation the rights
|
|
7 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8 |
copies of the Software, and to permit persons to whom the Software is
|
|
9 |
furnished to do so, subject to the following conditions:
|
|
10 |
||
11 |
The above copyright notice and this permission notice shall be included in
|
|
12 |
all copies or substantial portions of the Software.
|
|
13 |
||
14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20 |
THE SOFTWARE.
|
|
21 |
*/
|
|
22 |
||
23 |
#ifndef __H_UTILS__
|
|
24 |
#define __H_UTILS__
|
|
25 |
||
26 |
#include "defs.h" |
|
|
32
by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt |
27 |
#include <stddef.h> |
|
26
by Gustav Hartvigsson
* Added something to config.h.in, still not sure how it cmake works. |
28 |
#include "config.h" |
|
32
by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt |
29 |
#include <stdio.h> |
|
23
by Gustav Hartvigsson
* Fixed some of the build warnings. |
30 |
|
|
21
by Gustav Hartvigsson
Woops! |
31 |
BEGIN_DECLS
|
32 |
||
|
47
by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h |
33 |
/** @file
|
34 |
* A collect of utility functions.
|
|
35 |
*/
|
|
36 |
||
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
37 |
/**
|
38 |
* Creats a new C string with the correct length from a long string.
|
|
39 |
* This may be a costly operation.
|
|
40 |
*
|
|
41 |
* The restulting string must be freed by caller.
|
|
42 |
*/
|
|
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
43 |
char * |
44 |
s_string_new (const char * s); |
|
|
26
by Gustav Hartvigsson
* Added something to config.h.in, still not sure how it cmake works. |
45 |
|
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
46 |
/**
|
47 |
* Creats a new C string with the correct length using standard fprint style
|
|
48 |
* format.
|
|
49 |
*
|
|
50 |
* The restulting string must be freed by caller.
|
|
51 |
*/
|
|
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
52 |
char * |
53 |
s_string_new_fmt (const char * format, ...); |
|
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
54 |
|
55 |
/**
|
|
56 |
* Same as s_string_new, but with a set length.
|
|
57 |
*
|
|
58 |
* The restulting string must be freed by caller
|
|
59 |
*/
|
|
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
60 |
char * |
61 |
s_string_new_with_len (const char * s, size_t len); |
|
|
21
by Gustav Hartvigsson
Woops! |
62 |
|
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
63 |
/**
|
64 |
* Returns a C string with the current time (as run).
|
|
65 |
*
|
|
66 |
* The restulting string must be freed by caller.
|
|
67 |
*/
|
|
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
68 |
char * |
69 |
s_current_time (void); |
|
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
70 |
|
71 |
#ifndef strdup
|
|
72 |
/* strdup is not ISO C, so we have to declare it somewhare, this should work
|
|
73 |
* even if we do not impliment the function ourselfs.
|
|
74 |
*
|
|
75 |
* This just to supress a compiler warning.
|
|
76 |
*/
|
|
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
77 |
char * |
78 |
strdup(const char *str); |
|
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
79 |
#endif
|
80 |
||
81 |
/** Just a standard print function. */
|
|
|
32
by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt |
82 |
#define s_print(p, ...)\
|
83 |
fprintf (stdout, p, ##__VA_ARGS__)
|
|
84 |
||
85 |
/**
|
|
86 |
* Print a yellow warning to stderr.
|
|
87 |
*/
|
|
88 |
#define s_warn_print(p, ...)\
|
|
89 |
fprintf (stderr, BOLDYELLOW "[WARN] " p "\n" RESET, ##__VA_ARGS__)
|
|
|
21
by Gustav Hartvigsson
Woops! |
90 |
|
|
26
by Gustav Hartvigsson
* Added something to config.h.in, still not sure how it cmake works. |
91 |
/**
|
92 |
* prints a red message with the prefix [ERR]
|
|
93 |
*/
|
|
|
32
by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt |
94 |
#define s_err_print(p, ...)\
|
95 |
fprintf (stderr, RED "[ERR] " p RESET "\n", ##__VA_ARGS__)
|
|
96 |
||
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
97 |
#if DEBUG
|
|
26
by Gustav Hartvigsson
* Added something to config.h.in, still not sure how it cmake works. |
98 |
/**
|
99 |
* debug_print is a function that only prints if compiled with the debug
|
|
100 |
* flag not unset.
|
|
101 |
*/
|
|
|
47
by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h |
102 |
#define s_dbg_print(M, ...)\ |
103 |
fprintf (stdout, YELLOW "[DEBUG][%s:%d] " M RESET "\n", __FILE__, __LINE__, ##__VA_ARGS__)
|
|
|
32
by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt |
104 |
#else
|
|
47
by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h |
105 |
#define s_dbg_print(M, ...) |
|
32
by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt |
106 |
#endif
|
107 |
||
|
47
by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h |
108 |
/**
|
109 |
* Round a up a number to a multiple of an other.
|
|
110 |
*
|
|
111 |
* @param num The number to round up.
|
|
112 |
* @param multiple The multiple to round up to.
|
|
113 |
*
|
|
114 |
* See: http://stackoverflow.com/a/9194117
|
|
115 |
*/
|
|
116 |
#define round_up(num, multiple) (((num + multiple - 1) / multiple) * multiple)
|
|
117 |
||
|
59
by Gustav Hartvigsson
* Moved print_backtrace() to utils.h |
118 |
/* -------- TraceBack stuff ---- */
|
119 |
||
120 |
#define S_STACK_TRACKE_SIZE 128
|
|
121 |
||
122 |
#ifdef __unix__
|
|
123 |
//#pragma message ("We are a UNIX.") |
|
124 |
#include <execinfo.h> |
|
125 |
/** |
|
126 |
* Get a platform specific traceback.
|
|
127 |
*
|
|
128 |
* Works on UNIX's and Windows (Not working).
|
|
129 |
*/
|
|
130 |
#define print_backtrace() {\ |
|
131 |
fprintf (stderr, "[BACKTRACE:]\n");\
|
|
132 |
void ** _backtrace_data_ = calloc (S_STACK_TRACKE_SIZE, sizeof (void *));\
|
|
133 |
int _backtrace_len_ = backtrace (_backtrace_data_, 10);\
|
|
134 |
char ** _backtrace_strs_ = backtrace_symbols (_backtrace_data_, _backtrace_len_);\
|
|
135 |
if (_backtrace_strs_ == NULL) {\
|
|
136 |
fprintf (stderr, "Could not get backtrace...\n");\
|
|
137 |
} else {\
|
|
138 |
for (int i = 0; i < _backtrace_len_; i++) {\
|
|
139 |
fprintf (stderr, "%s\n", _backtrace_strs_[i]);\
|
|
140 |
}\
|
|
141 |
free (_backtrace_strs_);\
|
|
142 |
}\
|
|
143 |
fprintf (stderr, "[END BACKTRACE]\n");\
|
|
144 |
}
|
|
145 |
#elif __WIN32__ || __WIN64__
|
|
146 |
//#pragma message ("We are a Windows (why, oh why?)") |
|
147 |
#include <windows.h> |
|
148 |
#include <dbghelp.h> |
|
149 |
#include <winbase.h> |
|
150 |
#if __WIN64__ |
|
151 |
#define _PLATFORM_SPECIFIC_DWORD DWORD64 |
|
152 |
#else |
|
153 |
#define _PLATFORM_SPECIFIC_DWORD DWORD |
|
154 |
#endif |
|
155 |
#define print_backtrace() {\ |
|
156 |
fprintf (stderr, "[BACKTRACE:]\n");\
|
|
157 |
void * _backtrace_stack[S_STACK_TRACKE_SIZE];\
|
|
158 |
HANDLE _backtrace_proc = GetCurrentProcess ();\
|
|
159 |
SymInitialize (_backtrace_proc, 0, TRUE);\
|
|
160 |
unsigned short _backtrace_frames = CaptureStackBackTrace ( 0, S_STACK_TRACKE_SIZE, _backtrace_stack, NULL );\
|
|
161 |
SYMBOL_INFO * _backtrace_symbol = calloc (1, sizeof (SYMBOL_INFOW) + (sizeof (char) * 256));\
|
|
162 |
_backtrace_symbol->MaxNameLen = 255;\
|
|
163 |
_backtrace_symbol->SizeOfStruct = sizeof (SYMBOL_INFO);\
|
|
164 |
for (int i = 0; i <= _backtrace_frames; i++) {\
|
|
165 |
SymFromAddr (_backtrace_proc, (_PLATFORM_SPECIFIC_DWORD)(_backtrace_stack[i]), 0, _backtrace_symbol);\
|
|
166 |
fprintf (stderr, "%i: %s - 0x%0X\n", _backtrace_frames - i, _backtrace_symbol->Name, _backtrace_symbol->Address);\
|
|
167 |
}\
|
|
168 |
SymCleanup (_backtrace_proc);\
|
|
169 |
free (_backtrace_symbol);\
|
|
170 |
fprintf (stderr, "[END BACKTRACE]\n");\
|
|
171 |
}
|
|
172 |
#else
|
|
173 |
//#pragma message ("We are not a UNIX or a Windows") |
|
174 |
#define print_backtrace() {\ |
|
175 |
fprintf(stdout, "[Can not get backtrace]\nCurrent Function:" __func__);\
|
|
176 |
fprintf (stderr, "[END BACKTRACE]\n");\
|
|
177 |
}
|
|
178 |
#endif
|
|
179 |
||
|
22
by Gustav Hartvigsson
* Made code compile |
180 |
BEGIN_DECLS
|
|
21
by Gustav Hartvigsson
Woops! |
181 |
|
182 |
#endif
|