forked from IW0HDV/extio-hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
184 lines (147 loc) · 3.92 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* @file util.h
* @brief Header files for utility functions
* @author Andrea Montefusco IW0HDV
* @version 0.0
* @date 2013-09-23
*/
/* Copyright (C)
* Andrea Montefusco IW0HDV
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#if !defined __UTIL_H__
#define __UTIL_H__
//
// http://sourceforge.net/p/mingw/mailman/mingw-users/thread/Pine.LNX.4.44.0310310459430.7328-100000@boutell.com/
//
#if defined __MINGW32__
#define SHARED __attribute__((section(".shr"), shared))
#define UNSHARED
#else
#define SHARED
#endif
#if defined _MSC_VER
//#define strdup _strdup
//#define wcstombs wcstombs_s
//#define free(x)
#endif
#if defined __MINGW32__
#define strcpy_s(a,b,c) strcpy(a,c)
#endif
#include <stdio.h>
// generic macro for array size computation
#define ARRAY_SIZE(x) ((sizeof(x))/(sizeof(x[0])))
#define MAX(a,b) (((a)>(b))?(a):(b))
//
// snprintf is not present in VC++ 2010, here there a possible remediation
// See http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010
//
#ifdef _MSC_VER
#include <stdarg.h> /* va_list, va_start, va_arg, va_end */
#define snprintf c99_snprintf
inline int c99_vsnprintf(char* str, size_t size, const char* format, va_list ap)
{
int count = -1;
if (size != 0)
count = _vsnprintf_s(str, size, _TRUNCATE, format, ap);
if (count == -1)
count = _vscprintf(format, ap);
return count;
}
inline int c99_snprintf(char* str, size_t size, const char* format, ...)
{
int count;
va_list ap;
va_start(ap, format);
count = c99_vsnprintf(str, size, format, ap);
va_end(ap);
return count;
}
#endif // _MSC_VER
char *xstrdup(const char *p);
void xstrdel(const char *p, int line = 0);
/*
* Binary constant generator macro
* By Tom Torfs - donated to the public domain
*
*/
/* All macro's evaluate to compile-time constants */
/*
*
*
* helper macros
*
*
*/
/* turn a numeric literal into a hex constant
* (avoids problems with leading zeroes)
* 8-bit constants max value 0x11111111, always fits in unsigned long
*/
#define HEX__(n) 0x##n##LU
// 8-bit conversion function
#define B8__(x) ((x&0x0000000FLU)?1:0) \
+((x&0x000000F0LU)?2:0) \
+((x&0x00000F00LU)?4:0) \
+((x&0x0000F000LU)?8:0) \
+((x&0x000F0000LU)?16:0) \
+((x&0x00F00000LU)?32:0) \
+((x&0x0F000000LU)?64:0) \
+((x&0xF0000000LU)?128:0)
/*
*
*
* user macros
*
*
*/
// for upto 8-bit binary constants
#define B8(d) ((unsigned char)B8__(HEX__(d)))
// for upto 16-bit binary constants, MSB first
#define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8) \
+ B8(dlsb))
// for upto 32-bit binary constants, MSB first
#define B32(dmsb,db2,db3,dlsb) (((unsigned long)B8(dmsb)<<24) \
+ ((unsigned long)B8(db2)<<16) \
+ ((unsigned long)B8(db3)<<8) \
+ B8(dlsb))
/*
* Sample usage:
*
* B8(01010101) = 85
* B16(10101010,01010101) = 43605
* B32(10000000,11111111,10101010,01010101) = 2164238933
*
*/
/*
* set/clear bit
* http://stackoverflow.com/questions/2978408/macros-to-set-and-clear-bits
*/
#define SET_BIT(p,n) ((p) |= (1 << (n)))
#define CLR_BIT(p,n) ((p) &= ~((1) << (n)))
#if _WIN32
void ErrorLog(const char* lpszFunction);
#endif
struct MsgAllocatorImpl;
class MsgAllocator {
public:
MsgAllocator();
~MsgAllocator();
char * xstrdup(const char *);
void xstrdel(const char *, int);
private:
MsgAllocatorImpl *pi;
};
#endif /* _UTIL_H */