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