Debian Codegraph @

src/include/util.h

raw
1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2014
3 // David Freese, W1HKJ
4 //
5 // This file is part of flmsg
6 //
7 // flrig is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // flrig is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 // ----------------------------------------------------------------------------
20
21 #ifndef UTIL_H
22 #define UTIL_H
23
24 #include <string>
25 #include "config.h"
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 #ifndef __STDC_FORMAT_MACROS
32 # define __STDC_FORMAT_MACROS 1
33 #endif
34 #include <inttypes.h>
35
36 #ifndef powerof2
37 # define powerof2(n) ((((n) - 1) & (n)) == 0)
38 #endif
39 #ifndef MAX
40 # define MAX(a, b) (((a) > (b)) ? (a) : (b))
41 #endif
42 #ifndef MIN
43 # define MIN(a, b) (((a) < (b)) ? (a) : (b))
44 #endif
45 #ifndef CLAMP
46 # define CLAMP(x, low, high) (((x)>(high))?(high):(((x)<(low))?(low):(x)))
47 #endif
48 #define WCLAMP(x, low, high) (((x)>(high))?(low):(((x)<(low))?(high):(x)))
49
50 #ifdef __GNUC__
51 # if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
52 # define full_memory_barrier() __sync_synchronize()
53 # define read_memory_barrier() full_memory_barrier()
54 # define write_memory_barrier() full_memory_barrier()
55 # elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(__x86_64__)
56 # define full_memory_barrier() asm volatile ("lock; addl $0,0(%%esp)":::"memory")
57 # define read_memory_barrier() full_memory_barrier()
58 # define write_memory_barrier() full_memory_barrier()
59 /*
60 These would be faster on SSE2-capable processors:
61 # define full_memory_barrier() asm volatile ("mfence":::"memory")
62 # define read_memory_barrier() asm volatile ("lfence":::"memory")
63 # define write_memory_barrier() asm volatile ("sfence":::"memory")
64 */
65 # elif defined(__ppc__) || defined(__powerpc__) || defined(__PPC__)
66 # define full_memory_barrier() asm volatile("sync":::"memory")
67 # define read_memory_barrier() full_memory_barrier()
68 # define write_memory_barrier() full_memory_barrier()
69 # else
70 # warning Memory barriers not defined on this system
71 # define full_memory_barrier() ((void)0)
72 # define read_memory_barrier() full_memory_barrier()
73 # define write_memory_barrier() full_memory_barrier()
74 # endif
75 #else
76 # warning Memory barriers not defined on this system
77 # define full_memory_barrier() ((void)0)
78 # define read_memory_barrier() full_memory_barrier()
79 # define write_memory_barrier() full_memory_barrier()
80 #endif
81
82 /* http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html */
83 #if defined(__GNUC__) && (__GNUC__ >= 3)
84 # define likely(x) __builtin_expect (!!(x), 1)
85 # define unlikely(x) __builtin_expect (!!(x), 0)
86 # define used__ __attribute__ ((__used__))
87 # define unused__ __attribute__ ((__unused__))
88 # define must_check__ __attribute__ ((__warn_unused_result__))
89 # define deprecated__ __attribute__ ((__deprecated__))
90 # define noreturn__ __attribute__ ((__noreturn__))
91 # define pure__ __attribute__ ((__pure__))
92 # define const__ __attribute__ ((__const__))
93 # define malloc__ __attribute__ ((__malloc__))
94 # define packed__ __attribute__ ((__packed__))
95 # define inline__ inline __attribute__ ((__always_inline__))
96 # define noinline__ __attribute__ ((__noinline__))
97 # define nonnull__(x) __attribute__ ((__nonnull__(x)))
98 # define format__(type_, index_, first_) __attribute__ ((format(type_, index_, first_)))
99 #else
100 # define likely(x) (x)
101 # define unlikely(x) (x)
102 # define used__
103 # define unused__
104 # define must_check__
105 # define deprecated__
106 # define noreturn__
107 # define pure__
108 # define const__
109 # define malloc__
110 # define packed__
111 # define inline__
112 # define noinline__
113 # define nonnull__(x)
114 # define format__(type_, index_, first_)
115 #endif
116
117 #if defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
118 # define hot__ __attribute__ ((__hot__))
119 # define cold__ __attribute__ ((__cold__))
120 #else
121 # define hot__
122 # define cold__
123 #endif
124
125 #include <stddef.h>
126
127 const__ uint32_t ceil2(uint32_t n);
128 const__ uint32_t floor2(uint32_t n);
129
130 #if !HAVE_STRCASESTR
131 char* strcasestr(const char* haystack, const char* needle);
132 #endif
133
134 #if !HAVE_STRLCPY
135 size_t strlcpy(char* dest, const char* src, size_t size);
136 #endif
137
138 int set_cloexec(int fd, unsigned char v);
139 int set_nonblock(int fd, unsigned char v);
140 int set_nodelay(int fd, unsigned char v);
141 int get_bufsize(int fd, int dir, int* len);
142 int set_bufsize(int fd, int dir, int len);
143
144 unsigned long ver2int(const char* version);
145
146 void save_signals(void);
147 void restore_signals(void);
148
149 void MilliSleep(long msecs);
150
151 #ifdef __cplusplus
152 } // extern "C"
153 #endif
154
155 #ifdef __cplusplus
156 uint32_t simple_hash_data(const unsigned char* buf, size_t len, uint32_t code = 0);
157 uint32_t simple_hash_str(const unsigned char* str, uint32_t code = 0);
158 #endif
159
160 #ifdef __cplusplus
161 const char* str2hex(const unsigned char* str, size_t len);
162 const char* str2hex(const char* str, size_t len = 0);
163 #else
164 const char* str2hex(const unsigned* str, size_t len);
165 #endif
166
167 const char* uint2bin(unsigned u, size_t len);
168 void MilliSleep(long msecs);
169
170 #if !defined(NDEBUG) && defined(deprecated__) && defined(__GNUC__) && !defined(__WIN32__)
171 #include <stdio.h>
172 #include <string.h>
173 deprecated__ typeof(sprintf) sprintf;
174 /* there are far too many of these in the qrz code
175 deprecated__ typeof(strcpy) strcpy;
176 deprecated__ typeof(strcat) strcat;
177 */
178 #endif
179
180 #ifdef __WIN32__
181 # define NOMINMAX 1
182 #endif
183
184 #ifndef __WIN32__
185 # define PRIuSZ "zu"
186 # define PRIdSZ "zd"
187 #else
188 # define PRIuSZ "Iu"
189 # define PRIdSZ "Id"
190 #endif
191
192 #ifdef __WIN32__
193 # define PATH_SEP "\\"
194 #else
195 # define PATH_SEP "/"
196 #endif
197
198 //======================================================================
199
200 extern void ucase(std::string &);
201 extern void strip_spaces(std::string &);
202 extern void strip_leading_zeros(std::string &);
203 extern void strip_lfs(std::string &);
204 extern std::string wordwrap(std::string &s, int cnt);
205
206 #endif /* UTIL_H */
207
208 /*
209 Local Variables:
210 mode: c++
211 c-file-style: "linux"
212 End:
213 */