/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>
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
30
#include <wchar.h>
23 by Gustav Hartvigsson
* Fixed some of the build warnings.
31
21 by Gustav Hartvigsson
Woops!
32
BEGIN_DECLS
33
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
34
/** @file
35
 * A collect of utility functions.
36
 */
37
39 by Gustav Hartvigsson
* Added "check" target for testing.
38
/**
39
 * Creats a new C string with the correct length from a long string.
40
 * This may be a costly operation.
41
 *
42
 * The restulting string must be freed by caller.
43
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
44
char *
45
s_string_new (const char * s);
26 by Gustav Hartvigsson
* Added something to config.h.in, still not sure how it cmake works.
46
39 by Gustav Hartvigsson
* Added "check" target for testing.
47
/**
48
 * Creats a new C string with the correct length using standard fprint style
49
 * format.
50
 *
51
 * The restulting string must be freed by caller.
52
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
53
char *
54
s_string_new_fmt (const char * format, ...);
39 by Gustav Hartvigsson
* Added "check" target for testing.
55
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
56
39 by Gustav Hartvigsson
* Added "check" target for testing.
57
/**
58
 * Same as s_string_new, but with a set length.
59
 *
60
 * The restulting string must be freed by caller
61
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
62
char *
63
s_string_new_with_len (const char * s, size_t len);
21 by Gustav Hartvigsson
Woops!
64
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
65
/**
66
 * Convert a wide string to a byte string.
67
 */
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
68
char *
69
s_wstring_to_string (const wchar_t * ws);
70
71
#if 0
72
73
wchar_t *
74
s_wstring_new (const wchar_t * s);
75
76
wchar_t *
77
s_wstring_new_fmt (const wchar_t * format, ...);
78
79
wchar_t *
80
s_wstring_new_with_len (const wchar_t * format, size_t len);
81
#endif
82
83
39 by Gustav Hartvigsson
* Added "check" target for testing.
84
/**
85
 * Returns a C string with the current time (as run).
86
 *
87
 * The restulting string must be freed by caller.
88
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
89
char *
90
s_current_time (void);
39 by Gustav Hartvigsson
* Added "check" target for testing.
91
92
#ifndef strdup
93
/* strdup is not ISO C, so we have to declare it somewhare, this should work
94
 * even if we do not impliment the function ourselfs.
95
 *
96
 * This just to supress a compiler warning.
97
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
98
char *
99
strdup(const char *str);
39 by Gustav Hartvigsson
* Added "check" target for testing.
100
#endif
101
102
/** Just a standard print function. */
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
103
#define s_print(p, ...)\
104
  fprintf (stdout, p, ##__VA_ARGS__)
105
106
/**
107
 * Print a yellow warning to stderr.
108
 */
109
#define s_warn_print(p, ...)\
110
  fprintf (stderr, BOLDYELLOW "[WARN] " p "\n" RESET, ##__VA_ARGS__)
21 by Gustav Hartvigsson
Woops!
111
26 by Gustav Hartvigsson
* Added something to config.h.in, still not sure how it cmake works.
112
/**
113
 * prints a red message with the prefix [ERR]
114
 */
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
115
#define s_err_print(p, ...)\
116
  fprintf (stderr, RED "[ERR] " p RESET "\n", ##__VA_ARGS__)
117
39 by Gustav Hartvigsson
* Added "check" target for testing.
118
#if DEBUG
26 by Gustav Hartvigsson
* Added something to config.h.in, still not sure how it cmake works.
119
/**
120
 * debug_print is a function that only prints if compiled with the debug
121
 * flag not unset.
122
 */
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
123
  #define s_dbg_print(M, ...)\
124
    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
125
#else
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
126
  #define s_dbg_print(M, ...) 
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
127
#endif
128
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
129
/**
130
 * Round a up a number to a multiple of an other.
131
 *
132
 * @param num The number to round up.
133
 * @param multiple The multiple to round up to.
134
 *
135
 * See: http://stackoverflow.com/a/9194117
136
 */
137
#define round_up(num, multiple) (((num + multiple - 1) / multiple) * multiple)
138
59 by Gustav Hartvigsson
* Moved print_backtrace() to utils.h
139
/* -------- TraceBack stuff ---- */
140
141
#define S_STACK_TRACKE_SIZE 128
142
143
#ifdef  __unix__
144
  //#pragma message ("We are a UNIX.")
145
  #include <execinfo.h>
146
  /**
147
   * Get a platform specific traceback.
148
   *
149
   * Works on UNIX's and Windows (Not working).
150
   */
151
  #define print_backtrace() {\
152
    fprintf (stderr, "[BACKTRACE:]\n");\
153
    void ** _backtrace_data_ = calloc (S_STACK_TRACKE_SIZE, sizeof (void *));\
154
    int _backtrace_len_ = backtrace (_backtrace_data_, 10);\
155
    char ** _backtrace_strs_ = backtrace_symbols (_backtrace_data_, _backtrace_len_);\
156
    if (_backtrace_strs_ == NULL) {\
157
      fprintf (stderr, "Could not get backtrace...\n");\
158
    } else {\
159
      for (int i = 0; i < _backtrace_len_; i++) {\
160
        fprintf (stderr, "%s\n", _backtrace_strs_[i]);\
161
      }\
162
      free (_backtrace_strs_);\
163
    }\
164
    fprintf (stderr, "[END BACKTRACE]\n");\
165
  }
166
#elif __WIN32__ || __WIN64__
167
  //#pragma message ("We are a Windows (why, oh why?)")
168
  #include <windows.h>
169
  #include <dbghelp.h>
170
  #include <winbase.h>
171
  #if __WIN64__
172
    #define _PLATFORM_SPECIFIC_DWORD DWORD64
173
  #else
174
    #define _PLATFORM_SPECIFIC_DWORD DWORD
175
  #endif
176
  #define print_backtrace() {\
177
    fprintf (stderr, "[BACKTRACE:]\n");\
178
    void * _backtrace_stack[S_STACK_TRACKE_SIZE];\
179
    HANDLE _backtrace_proc = GetCurrentProcess ();\
180
    SymInitialize (_backtrace_proc, 0, TRUE);\
181
    unsigned short _backtrace_frames = CaptureStackBackTrace ( 0, S_STACK_TRACKE_SIZE, _backtrace_stack, NULL );\
182
    SYMBOL_INFO * _backtrace_symbol = calloc (1, sizeof (SYMBOL_INFOW) + (sizeof (char) * 256));\
183
    _backtrace_symbol->MaxNameLen = 255;\
184
    _backtrace_symbol->SizeOfStruct = sizeof (SYMBOL_INFO);\
185
    for (int i = 0; i <= _backtrace_frames; i++) {\
186
      SymFromAddr (_backtrace_proc, (_PLATFORM_SPECIFIC_DWORD)(_backtrace_stack[i]), 0, _backtrace_symbol);\
187
      fprintf (stderr, "%i: %s - 0x%0X\n", _backtrace_frames - i, _backtrace_symbol->Name, _backtrace_symbol->Address);\
188
    }\
189
    SymCleanup (_backtrace_proc);\
190
    free (_backtrace_symbol);\
191
    fprintf (stderr, "[END BACKTRACE]\n");\
192
  }
193
#else
194
  //#pragma message ("We are not a UNIX or a Windows")
195
  #define print_backtrace() {\
196
    fprintf(stdout, "[Can not get backtrace]\nCurrent Function:" __func__);\
197
    fprintf (stderr, "[END BACKTRACE]\n");\
198
  }
199
#endif
200
22 by Gustav Hartvigsson
* Made code compile
201
BEGIN_DECLS
21 by Gustav Hartvigsson
Woops!
202
203
#endif