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