/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
/**
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
44
 * Creates a new C string with the correct length from a long string.
39 by Gustav Hartvigsson
* Added "check" target for testing.
45
 * This may be a costly operation.
46
 *
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
47
 * The resulting string must be freed by caller.
39 by Gustav Hartvigsson
* Added "check" target for testing.
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
/**
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
54
 * Creates a new C string with the correct length using standard fprint style
39 by Gustav Hartvigsson
* Added "check" target for testing.
55
 * format.
56
 *
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
57
 * The resulting string must be freed by caller.
39 by Gustav Hartvigsson
* Added "check" target for testing.
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
 *
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
67
 * The resulting string must be freed by caller
39 by Gustav Hartvigsson
* Added "check" target for testing.
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
 *
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
138
 * The resulting string must be freed by caller.
39 by Gustav Hartvigsson
* Added "check" target for testing.
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
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
144
/**
145
 * Returns a C string with the current time, millisecond and timezone
146
 * information.
147
 *
148
 * The resulting string must be freed by caller.
149
 */
150
S_EXPORTED
151
schar *
152
s_current_time_full (void);
153
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
154
#if 0
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
155
/* strdup is not ISO C, so we have to declare it somewhere, this should work
156
 * even if we do not implement the function ourself.
39 by Gustav Hartvigsson
* Added "check" target for testing.
157
 *
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
158
 * This just to suppress a compiler warning.
39 by Gustav Hartvigsson
* Added "check" target for testing.
159
 */
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
160
#define strdup(s) s_string_new(s)
161
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
162
#endif /* if 0 */
39 by Gustav Hartvigsson
* Added "check" target for testing.
163
164
/** Just a standard print function. */
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
165
#define s_print(p, ...)\
166
  fprintf (stdout, p, ##__VA_ARGS__)
167
168
/**
169
 * Print a yellow warning to stderr.
170
 */
171
#define s_warn_print(p, ...)\
121.1.1 by Gustav Hartvigsson
* added CMake rules for libgc in CMakeLists.txt and a findGC file.
172
  fprintf (stderr, S_COLOR_BOLDYELLOW "[WARN] " p "\n" S_COLOR_RESET, ##__VA_ARGS__)
21 by Gustav Hartvigsson
Woops!
173
26 by Gustav Hartvigsson
* Added something to config.h.in, still not sure how it cmake works.
174
/**
175
 * prints a red message with the prefix [ERR]
176
 */
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
177
#define s_err_print(p, ...)\
121.1.1 by Gustav Hartvigsson
* added CMake rules for libgc in CMakeLists.txt and a findGC file.
178
  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
179
39 by Gustav Hartvigsson
* Added "check" target for testing.
180
#if DEBUG
26 by Gustav Hartvigsson
* Added something to config.h.in, still not sure how it cmake works.
181
/**
182
 * debug_print is a function that only prints if compiled with the debug
183
 * flag not unset.
184
 */
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
185
  #define s_dbg_print(M, ...)\
121.1.1 by Gustav Hartvigsson
* added CMake rules for libgc in CMakeLists.txt and a findGC file.
186
    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
187
#else
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
188
  #define s_dbg_print(M, ...)
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
189
#endif
190
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
191
/**
192
 * Round a up a number to a multiple of an other.
193
 *
194
 * @param num The number to round up.
195
 * @param multiple The multiple to round up to.
196
 *
197
 * See: http://stackoverflow.com/a/9194117
198
 */
199
#define round_up(num, multiple) (((num + multiple - 1) / multiple) * multiple)
200
77 by Gustav Hartvigsson
* More work on SStream... Requiers Mutex suppert :-)
201
82 by Gustav Hartvigsson
* added rotl and rotr macros. cus' why not?
202
/**
203
 * rotate value count steps to the left and rotate the overflow.
204
 *
205
 * @param value The value to rotate.
206
 * @param count The number of bits to shift it.
207
 *
208
 * @warning count must be positive.
209
 */
210
#define rotl(value, count)\
211
  ((value << count) | (value >> ((-count) & (sizeof(value) * 8) - 1)))
212
213
/**
214
 * rotate value count steps to the rights and rotate the overflow.
215
 *
216
 * @param value The value to rotate.
217
 * @param count The number of bits to shift it.
218
 *
219
 * @warning count must be positive.
220
 */
221
#define rotr(value, count)\
222
  ((value >> count) | (value << ((-count) & (sizeof(value) * 8) - 1)))
223
224
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
225
/* -------- TraceBack stuff ---- */
226
227
#define S_STACK_TRACKE_SIZE 128
228
229
#ifdef  __unix__
230
  //#pragma message ("We are a UNIX.")
231
  #include <execinfo.h>
232
  /**
233
   * Get a platform specific traceback.
234
   *
235
   * Works on UNIX's and Windows (Not working).
236
   */
237
  #define print_backtrace() {\
238
    fprintf (stderr, "[BACKTRACE:]\n");\
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
239
    void ** _backtrace_data_ = s_calloc (S_STACK_TRACKE_SIZE, sizeof (void *));\
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
240
    int _backtrace_len_ = backtrace (_backtrace_data_, 10);\
241
    char ** _backtrace_strs_ = backtrace_symbols (_backtrace_data_, _backtrace_len_);\
242
    if (_backtrace_strs_ == NULL) {\
243
      fprintf (stderr, "Could not get backtrace...\n");\
244
    } else {\
245
      for (int i = 0; i < _backtrace_len_; i++) {\
246
        fprintf (stderr, "%s\n", _backtrace_strs_[i]);\
247
      }\
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
248
      s_free (_backtrace_strs_);\
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
249
    }\
250
    fprintf (stderr, "[END BACKTRACE]\n");\
251
  }
252
#elif __WIN32__ || __WIN64__
253
  //#pragma message ("We are a Windows (why, oh why?)")
254
  #include <windows.h>
255
  #include <dbghelp.h>
256
  #include <winbase.h>
257
  #if __WIN64__
258
    #define _PLATFORM_SPECIFIC_DWORD DWORD64
259
  #else
260
    #define _PLATFORM_SPECIFIC_DWORD DWORD
261
  #endif
262
  #define print_backtrace() {\
263
    fprintf (stderr, "[BACKTRACE:]\n");\
264
    void * _backtrace_stack[S_STACK_TRACKE_SIZE];\
265
    HANDLE _backtrace_proc = GetCurrentProcess ();\
266
    SymInitialize (_backtrace_proc, 0, TRUE);\
267
    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.
268
    SYMBOL_INFO * _backtrace_symbol = s_calloc (1, sizeof (SYMBOL_INFOW) + (sizeof (char) * 256));\
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
269
    _backtrace_symbol->MaxNameLen = 255;\
270
    _backtrace_symbol->SizeOfStruct = sizeof (SYMBOL_INFO);\
271
    for (int i = 0; i <= _backtrace_frames; i++) {\
272
      SymFromAddr (_backtrace_proc, (_PLATFORM_SPECIFIC_DWORD)(_backtrace_stack[i]), 0, _backtrace_symbol);\
273
      fprintf (stderr, "%i: %s - 0x%0X\n", _backtrace_frames - i, _backtrace_symbol->Name, _backtrace_symbol->Address);\
274
    }\
275
    SymCleanup (_backtrace_proc);\
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
276
    s_free (_backtrace_symbol);\
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
277
    fprintf (stderr, "[END BACKTRACE]\n");\
278
  }
279
#else
280
  //#pragma message ("We are not a UNIX or a Windows")
281
  #define print_backtrace() {\
282
    fprintf(stdout, "[Can not get backtrace]\nCurrent Function:" __func__);\
283
    fprintf (stderr, "[END BACKTRACE]\n");\
284
  }
285
#endif
286
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
287
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
288
/** @} */
289
110 by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub.
290
S_BEGIN_DECLS