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