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