/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
126.1.1 by Gustav Hartvigsson
* Using
23
#pragma once
21 by Gustav Hartvigsson
Woops!
24
25
#include "defs.h"
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
26
#include <stddef.h>
26 by Gustav Hartvigsson
* Added something to config.h.in, still not sure how it cmake works.
27
#include "config.h"
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
28
#include <stdio.h>
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
29
#include <uchar.h>
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
30
#include <wchar.h>
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
31
#include<signal.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
/**
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
45
 * Creates a new C string with the correct length from a long string.
39 by Gustav Hartvigsson
* Added "check" target for testing.
46
 * This may be a costly operation.
47
 *
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
48
 * The resulting string must be freed by caller.
39 by Gustav Hartvigsson
* Added "check" target for testing.
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
/**
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
55
 * Creates a new C string with the correct length using standard fprint style
39 by Gustav Hartvigsson
* Added "check" target for testing.
56
 * format.
57
 *
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
58
 * The resulting string must be freed by caller.
39 by Gustav Hartvigsson
* Added "check" target for testing.
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
 *
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
68
 * The resulting string must be freed by caller
39 by Gustav Hartvigsson
* Added "check" target for testing.
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
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
83
84
/**
85
 * Gets the length of a string.
86
 * Normally uses strlen under the hood.
87
 */
88
S_EXPORTED
89
size_t
90
s_string_len (const schar * str);
91
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
92
/**
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
93
 * Get the length of the uchar string.
94
 *
95
 * @warning This is the count of suchar's, not necessarily the number of
96
 * characters in the string.
97
 */
98
S_EXPORTED
99
size_t
100
s_ustring_len (const suchar * us);
101
102
/**
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
103
 * Convert a wide (32 bit per char) string to a byte string.
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
104
 *
105
 * @param us The uchar string to convert to a schar string.
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
106
 *
107
 * @note The name is a little misleading, this (should) work with
108
 *       Unicode strings, but we do not real checking of unicode validity,
109
 *       and we have no formal support for Unicode above the basics.
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
110
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
111
S_EXPORTED
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
112
schar *
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
113
s_ustring_to_string (const suchar * us);
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
114
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
115
116
/**
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
117
 * Convert (8bit per char) schar string to a (32bit per char) suchar string.
118
 *
119
 * @param str The schar string to convert to a suchar string.
120
 */
121
S_EXPORTED
122
suchar *
123
s_string_to_ustring (const schar * str);
124
151.1.2 by Gustav Hartvigsson
* Removed wchar.
125
#if 0
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
126
/**
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
127
 * @deprecated Usage of this function is discouraged. Consider using uchar
128
 *             (char32_t) strings instead, and s_ustring_to_string(), for
129
 *             your own sanity.
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
130
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
131
S_EXPORTED
110 by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub.
132
S_DEPRECATED
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
133
schar *
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
134
s_wstring_to_string (const wchar_t * ws);
151.1.2 by Gustav Hartvigsson
* Removed wchar.
135
#endif
136
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
137
39 by Gustav Hartvigsson
* Added "check" target for testing.
138
/**
139
 * Returns a C string with the current time (as run).
140
 *
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
141
 * The resulting string must be freed by caller.
39 by Gustav Hartvigsson
* Added "check" target for testing.
142
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
143
S_EXPORTED
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
144
schar *
61 by Gustav Hartvigsson
* Made the code more easy to read.
145
s_current_time (void);
39 by Gustav Hartvigsson
* Added "check" target for testing.
146
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
147
/**
148
 * Returns a C string with the current time, millisecond and timezone
149
 * information.
150
 *
151
 * The resulting string must be freed by caller.
152
 */
153
S_EXPORTED
154
schar *
155
s_current_time_full (void);
156
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
157
158
159
160
/*
161
 * Macro to create a defs for the bin search functions.
162
 * Do not use can not be used outside of here.
163
 */
164
#define _BIN_SEARCH_FUNC_NAME(type)\
165
s_binary_search_##type (type list[], size_t first, size_t last, type n)
166
167
168
#define _MAKE_BIN_SEARCH_DEF(type)\
169
S_EXPORTED \
170
sboolean \
171
_BIN_SEARCH_FUNC_NAME(type)
172
173
174
/**
175
 * Does a binary search in an orderd array of values.
176
 *
177
 * @param list The list to search.
178
 * @param first The lowest position to search.
179
 * @param last The last position to search.
180
 * @param n The value to see if it is in the list.
181
 */
182
#define s_binary_search(list, first, last, n) _Generic ((list),\
183
                         sbyte *: s_binary_search_sbyte ,\
184
                        subyte *: s_binary_search_subyte ,\
185
                        sshort *: s_binary_search_sshort ,\
186
                       sushort *: s_binary_search_sushort ,\
187
                          sint *: s_binary_search_sint ,\
188
                         suint *: s_binary_search_suint ,\
189
                         slong *: s_binary_search_slong ,\
190
                        sulong *: s_binary_search_sulong ,\
191
                        sfloat *: s_binary_search_sfloat ,\
192
                       sdouble *: s_binary_search_sdouble ,\
193
                    squadruple *: s_binary_search_squadruple\
194
                        )(list, first, last, n)
195
196
197
198
_MAKE_BIN_SEARCH_DEF(sbyte);
199
200
_MAKE_BIN_SEARCH_DEF(subyte);
201
202
_MAKE_BIN_SEARCH_DEF(sshort);
203
204
_MAKE_BIN_SEARCH_DEF(sushort);
205
206
_MAKE_BIN_SEARCH_DEF(sint);
207
208
_MAKE_BIN_SEARCH_DEF(suint);
209
210
_MAKE_BIN_SEARCH_DEF(slong);
211
212
_MAKE_BIN_SEARCH_DEF(sulong);
213
214
_MAKE_BIN_SEARCH_DEF(sfloat);
215
216
_MAKE_BIN_SEARCH_DEF(sdouble);
217
218
_MAKE_BIN_SEARCH_DEF(squadruple);
219
220
221
222
223
#undef _BIN_SEARCH_FUNC_NAME
224
#undef _MAKE_BIN_SEARCH_DEF
225
#undef _BIN_SEARCH_GENERIC_FN
226
227
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
228
#if 0
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
229
/* strdup is not ISO C, so we have to declare it somewhere, this should work
230
 * even if we do not implement the function ourself.
39 by Gustav Hartvigsson
* Added "check" target for testing.
231
 *
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
232
 * This just to suppress a compiler warning.
39 by Gustav Hartvigsson
* Added "check" target for testing.
233
 */
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
234
#define strdup(s) s_string_new(s)
235
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
236
#endif /* if 0 */
39 by Gustav Hartvigsson
* Added "check" target for testing.
237
238
/** Just a standard print function. */
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
239
#define s_print(p, ...)\
240
  fprintf (stdout, p, ##__VA_ARGS__)
241
242
/**
243
 * Print a yellow warning to stderr.
244
 */
245
#define s_warn_print(p, ...)\
121.1.1 by Gustav Hartvigsson
* added CMake rules for libgc in CMakeLists.txt and a findGC file.
246
  fprintf (stderr, S_COLOR_BOLDYELLOW "[WARN] " p "\n" S_COLOR_RESET, ##__VA_ARGS__)
21 by Gustav Hartvigsson
Woops!
247
26 by Gustav Hartvigsson
* Added something to config.h.in, still not sure how it cmake works.
248
/**
249
 * prints a red message with the prefix [ERR]
250
 */
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
251
#define s_err_print(p, ...)\
121.1.1 by Gustav Hartvigsson
* added CMake rules for libgc in CMakeLists.txt and a findGC file.
252
  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
253
39 by Gustav Hartvigsson
* Added "check" target for testing.
254
#if DEBUG
26 by Gustav Hartvigsson
* Added something to config.h.in, still not sure how it cmake works.
255
/**
256
 * debug_print is a function that only prints if compiled with the debug
257
 * flag not unset.
258
 */
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
259
  #define s_dbg_print(M, ...)\
121.1.1 by Gustav Hartvigsson
* added CMake rules for libgc in CMakeLists.txt and a findGC file.
260
    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
261
#else
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
262
  #define s_dbg_print(M, ...)
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
263
#endif
264
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
265
/**
266
 * Round a up a number to a multiple of an other.
267
 *
268
 * @param num The number to round up.
269
 * @param multiple The multiple to round up to.
270
 *
271
 * See: http://stackoverflow.com/a/9194117
272
 */
273
#define round_up(num, multiple) (((num + multiple - 1) / multiple) * multiple)
274
77 by Gustav Hartvigsson
* More work on SStream... Requiers Mutex suppert :-)
275
82 by Gustav Hartvigsson
* added rotl and rotr macros. cus' why not?
276
/**
277
 * rotate value count steps to the left and rotate the overflow.
278
 *
279
 * @param value The value to rotate.
280
 * @param count The number of bits to shift it.
281
 *
282
 * @warning count must be positive.
283
 */
284
#define rotl(value, count)\
285
  ((value << count) | (value >> ((-count) & (sizeof(value) * 8) - 1)))
286
287
/**
288
 * rotate value count steps to the rights and rotate the overflow.
289
 *
290
 * @param value The value to rotate.
291
 * @param count The number of bits to shift it.
292
 *
293
 * @warning count must be positive.
294
 */
295
#define rotr(value, count)\
296
  ((value >> count) | (value << ((-count) & (sizeof(value) * 8) - 1)))
297
298
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
299
/* -------- TraceBack stuff ---- */
300
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
301
/**
302
 * @def print_traceback
303
 * Get a platform specific traceback.
304
 *
305
 * Works on UNIX's and Windows (Not working).
306
 */
307
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
308
#define S_STACK_TRACKE_SIZE 128
309
310
#ifdef  __unix__
311
  //#pragma message ("We are a UNIX.")
312
  #include <execinfo.h>
313
  #define print_backtrace() {\
314
    fprintf (stderr, "[BACKTRACE:]\n");\
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
315
    void ** _backtrace_data_ = s_calloc (S_STACK_TRACKE_SIZE, sizeof (void *));\
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
316
    int _backtrace_len_ = backtrace (_backtrace_data_, 10);\
317
    char ** _backtrace_strs_ = backtrace_symbols (_backtrace_data_, _backtrace_len_);\
318
    if (_backtrace_strs_ == NULL) {\
319
      fprintf (stderr, "Could not get backtrace...\n");\
320
    } else {\
321
      for (int i = 0; i < _backtrace_len_; i++) {\
322
        fprintf (stderr, "%s\n", _backtrace_strs_[i]);\
323
      }\
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
324
      s_free (_backtrace_strs_);\
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
325
    }\
326
    fprintf (stderr, "[END BACKTRACE]\n");\
327
  }
328
#elif __WIN32__ || __WIN64__
329
  //#pragma message ("We are a Windows (why, oh why?)")
330
  #include <windows.h>
331
  #include <dbghelp.h>
332
  #include <winbase.h>
333
  #if __WIN64__
334
    #define _PLATFORM_SPECIFIC_DWORD DWORD64
335
  #else
336
    #define _PLATFORM_SPECIFIC_DWORD DWORD
337
  #endif
338
  #define print_backtrace() {\
339
    fprintf (stderr, "[BACKTRACE:]\n");\
340
    void * _backtrace_stack[S_STACK_TRACKE_SIZE];\
341
    HANDLE _backtrace_proc = GetCurrentProcess ();\
342
    SymInitialize (_backtrace_proc, 0, TRUE);\
343
    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.
344
    SYMBOL_INFO * _backtrace_symbol = s_calloc (1, sizeof (SYMBOL_INFOW) + (sizeof (char) * 256));\
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
345
    _backtrace_symbol->MaxNameLen = 255;\
346
    _backtrace_symbol->SizeOfStruct = sizeof (SYMBOL_INFO);\
347
    for (int i = 0; i <= _backtrace_frames; i++) {\
348
      SymFromAddr (_backtrace_proc, (_PLATFORM_SPECIFIC_DWORD)(_backtrace_stack[i]), 0, _backtrace_symbol);\
349
      fprintf (stderr, "%i: %s - 0x%0X\n", _backtrace_frames - i, _backtrace_symbol->Name, _backtrace_symbol->Address);\
350
    }\
351
    SymCleanup (_backtrace_proc);\
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
352
    s_free (_backtrace_symbol);\
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
353
    fprintf (stderr, "[END BACKTRACE]\n");\
354
  }
355
#else
356
  //#pragma message ("We are not a UNIX or a Windows")
357
  #define print_backtrace() {\
358
    fprintf(stdout, "[Can not get backtrace]\nCurrent Function:" __func__);\
359
    fprintf (stderr, "[END BACKTRACE]\n");\
360
  }
361
#endif
362
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
363
/* ************************************************************************** 
364
 * Signal handlers
365
 * ************************************************************************** */
366
367
/**
368
 * @defgroup Sighand Signal handlers
369
 * @brief Signal handlers that can be uned in your code. (Use with care).
370
 * @{
371
 */
372
373
/**
374
 * Signal handler for segmentation faults.
375
 *
376
 * This can be hooked up if you want to print a traceback of what has happened
377
 * to cause the segmentatino fault.
378
 *
379
 * in the mainfunction add the following:
380
 * @code{c}
381
int
382
main (char ** argv, int argc) {
383
  // stuffs
384
  // code
385
  
386
  signal (SIGSEGV, s_sig_segfault);
387
  
388
  // start main loop or what ever.
389
  
390
} 
391
 * @endcode
392
 */
393
void
394
s_sig_segfault (int);
395
396
/** @} */
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
397
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
398
/** @} */
399
110 by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub.
400
S_BEGIN_DECLS
146 by Gustav Hartvigsson
* Reorderd includes in SimpleTypeSystem.h
401