/simpletypesystem/trunk

To get this branch, use:
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>
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
30
#include <uchar.h>
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
31
#include <wchar.h>
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
32
23 by Gustav Hartvigsson
* Fixed some of the build warnings.
33
110 by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub.
34
S_BEGIN_DECLS
21 by Gustav Hartvigsson
Woops!
35
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
36
/** @file
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
37
 * @defgroup Utils Utils
38
 * @addtogroup Utils
39
 * @{
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
40
 * A collect of utility functions.
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
41
 *
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
42
 */
43
39 by Gustav Hartvigsson
* Added "check" target for testing.
44
/**
45
 * Creats a new C string with the correct length from a long string.
46
 * This may be a costly operation.
47
 *
48
 * The restulting string must be freed by caller.
49
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
50
S_EXPORTED
61 by Gustav Hartvigsson
* Made the code more easy to read.
51
char *
52
s_string_new (const char * s);
26 by Gustav Hartvigsson
* Added something to config.h.in, still not sure how it cmake works.
53
39 by Gustav Hartvigsson
* Added "check" target for testing.
54
/**
55
 * Creats a new C string with the correct length using standard fprint style
56
 * format.
57
 *
58
 * The restulting string must be freed by caller.
59
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
60
S_EXPORTED
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
61
schar *
62
s_string_new_fmt (const schar * format, ...);
39 by Gustav Hartvigsson
* Added "check" target for testing.
63
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
64
39 by Gustav Hartvigsson
* Added "check" target for testing.
65
/**
66
 * Same as s_string_new, but with a set length.
67
 *
68
 * The restulting string must be freed by caller
69
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
70
S_EXPORTED
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
71
schar *
72
s_string_new_with_len (const schar * s, size_t len);
73
74
/**
75
 * Are the strings equal?
76
 * @returns TRUE if a and b are equal.
77
 * @returns FALSE if the strings are not equal.
78
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
79
S_EXPORTED
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
80
sboolean
81
s_string_is_equal (const schar * a, const schar * b);
21 by Gustav Hartvigsson
Woops!
82
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
83
/**
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
84
 * Convert a wide (32 bit per char) string to a byte string.
85
 * TODO : Implement.
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
86
 *
87
 * @note The name is a little misleading, this (should) work with
88
 *       Unicode strings, but we do not real checking of unicode validity,
89
 *       and we have no formal support for Unicode above the basics.
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
90
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
91
S_EXPORTED
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
92
schar *
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
93
s_ustring_to_string (const suchar * us);
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
94
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
95
96
/**
97
 * @deprecated Usage of this function is discouraged. Consider using uchar
98
 *             (char32_t) strings instead, and s_ustring_to_string(), for
99
 *             your own sanity.
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
100
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
101
S_EXPORTED
110 by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub.
102
S_DEPRECATED
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
103
schar *
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
104
s_wstring_to_string (const wchar_t * ws);
105
106
39 by Gustav Hartvigsson
* Added "check" target for testing.
107
/**
108
 * Returns a C string with the current time (as run).
109
 *
110
 * The restulting string must be freed by caller.
111
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
112
S_EXPORTED
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
113
schar *
61 by Gustav Hartvigsson
* Made the code more easy to read.
114
s_current_time (void);
39 by Gustav Hartvigsson
* Added "check" target for testing.
115
116
/* strdup is not ISO C, so we have to declare it somewhare, this should work
117
 * even if we do not impliment the function ourselfs.
118
 *
119
 * This just to supress a compiler warning.
120
 */
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
121
#define strdup(s) s_string_new(s)
122
39 by Gustav Hartvigsson
* Added "check" target for testing.
123
124
/** Just a standard print function. */
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
125
#define s_print(p, ...)\
126
  fprintf (stdout, p, ##__VA_ARGS__)
127
128
/**
129
 * Print a yellow warning to stderr.
130
 */
131
#define s_warn_print(p, ...)\
121.1.1 by Gustav Hartvigsson
* added CMake rules for libgc in CMakeLists.txt and a findGC file.
132
  fprintf (stderr, S_COLOR_BOLDYELLOW "[WARN] " p "\n" S_COLOR_RESET, ##__VA_ARGS__)
21 by Gustav Hartvigsson
Woops!
133
26 by Gustav Hartvigsson
* Added something to config.h.in, still not sure how it cmake works.
134
/**
135
 * prints a red message with the prefix [ERR]
136
 */
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
137
#define s_err_print(p, ...)\
121.1.1 by Gustav Hartvigsson
* added CMake rules for libgc in CMakeLists.txt and a findGC file.
138
  fprintf (stderr, S_COLOR_RED "[ERR] " p S_COLOR_RESET "\n", ##__VA_ARGS__)
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
139
39 by Gustav Hartvigsson
* Added "check" target for testing.
140
#if DEBUG
26 by Gustav Hartvigsson
* Added something to config.h.in, still not sure how it cmake works.
141
/**
142
 * debug_print is a function that only prints if compiled with the debug
143
 * flag not unset.
144
 */
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
145
  #define s_dbg_print(M, ...)\
121.1.1 by Gustav Hartvigsson
* added CMake rules for libgc in CMakeLists.txt and a findGC file.
146
    fprintf (stdout, S_COLOR_YELLOW "[DEBUG][%s:%d] " M S_COLOR_RESET "\n", __FILE__, __LINE__, ##__VA_ARGS__)
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
147
#else
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
148
  #define s_dbg_print(M, ...)
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
149
#endif
150
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
151
/**
152
 * Round a up a number to a multiple of an other.
153
 *
154
 * @param num The number to round up.
155
 * @param multiple The multiple to round up to.
156
 *
157
 * See: http://stackoverflow.com/a/9194117
158
 */
159
#define round_up(num, multiple) (((num + multiple - 1) / multiple) * multiple)
160
77 by Gustav Hartvigsson
* More work on SStream... Requiers Mutex suppert :-)
161
82 by Gustav Hartvigsson
* added rotl and rotr macros. cus' why not?
162
/**
163
 * rotate value count steps to the left and rotate the overflow.
164
 *
165
 * @param value The value to rotate.
166
 * @param count The number of bits to shift it.
167
 *
168
 * @warning count must be positive.
169
 */
170
#define rotl(value, count)\
171
  ((value << count) | (value >> ((-count) & (sizeof(value) * 8) - 1)))
172
173
/**
174
 * rotate value count steps to the rights and rotate the overflow.
175
 *
176
 * @param value The value to rotate.
177
 * @param count The number of bits to shift it.
178
 *
179
 * @warning count must be positive.
180
 */
181
#define rotr(value, count)\
182
  ((value >> count) | (value << ((-count) & (sizeof(value) * 8) - 1)))
183
184
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
185
/* -------- TraceBack stuff ---- */
186
187
#define S_STACK_TRACKE_SIZE 128
188
189
#ifdef  __unix__
190
  //#pragma message ("We are a UNIX.")
191
  #include <execinfo.h>
192
  /**
193
   * Get a platform specific traceback.
194
   *
195
   * Works on UNIX's and Windows (Not working).
196
   */
197
  #define print_backtrace() {\
198
    fprintf (stderr, "[BACKTRACE:]\n");\
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
199
    void ** _backtrace_data_ = s_calloc (S_STACK_TRACKE_SIZE, sizeof (void *));\
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
200
    int _backtrace_len_ = backtrace (_backtrace_data_, 10);\
201
    char ** _backtrace_strs_ = backtrace_symbols (_backtrace_data_, _backtrace_len_);\
202
    if (_backtrace_strs_ == NULL) {\
203
      fprintf (stderr, "Could not get backtrace...\n");\
204
    } else {\
205
      for (int i = 0; i < _backtrace_len_; i++) {\
206
        fprintf (stderr, "%s\n", _backtrace_strs_[i]);\
207
      }\
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
208
      s_free (_backtrace_strs_);\
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
209
    }\
210
    fprintf (stderr, "[END BACKTRACE]\n");\
211
  }
212
#elif __WIN32__ || __WIN64__
213
  //#pragma message ("We are a Windows (why, oh why?)")
214
  #include <windows.h>
215
  #include <dbghelp.h>
216
  #include <winbase.h>
217
  #if __WIN64__
218
    #define _PLATFORM_SPECIFIC_DWORD DWORD64
219
  #else
220
    #define _PLATFORM_SPECIFIC_DWORD DWORD
221
  #endif
222
  #define print_backtrace() {\
223
    fprintf (stderr, "[BACKTRACE:]\n");\
224
    void * _backtrace_stack[S_STACK_TRACKE_SIZE];\
225
    HANDLE _backtrace_proc = GetCurrentProcess ();\
226
    SymInitialize (_backtrace_proc, 0, TRUE);\
227
    unsigned short _backtrace_frames = CaptureStackBackTrace ( 0, S_STACK_TRACKE_SIZE, _backtrace_stack, NULL );\
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
228
    SYMBOL_INFO * _backtrace_symbol = s_calloc (1, sizeof (SYMBOL_INFOW) + (sizeof (char) * 256));\
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
229
    _backtrace_symbol->MaxNameLen = 255;\
230
    _backtrace_symbol->SizeOfStruct = sizeof (SYMBOL_INFO);\
231
    for (int i = 0; i <= _backtrace_frames; i++) {\
232
      SymFromAddr (_backtrace_proc, (_PLATFORM_SPECIFIC_DWORD)(_backtrace_stack[i]), 0, _backtrace_symbol);\
233
      fprintf (stderr, "%i: %s - 0x%0X\n", _backtrace_frames - i, _backtrace_symbol->Name, _backtrace_symbol->Address);\
234
    }\
235
    SymCleanup (_backtrace_proc);\
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
236
    s_free (_backtrace_symbol);\
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
237
    fprintf (stderr, "[END BACKTRACE]\n");\
238
  }
239
#else
240
  //#pragma message ("We are not a UNIX or a Windows")
241
  #define print_backtrace() {\
242
    fprintf(stdout, "[Can not get backtrace]\nCurrent Function:" __func__);\
243
    fprintf (stderr, "[END BACKTRACE]\n");\
244
  }
245
#endif
246
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
247
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
248
/** @} */
249
110 by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub.
250
S_BEGIN_DECLS
21 by Gustav Hartvigsson
Woops!
251
252
#endif