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