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