CAEN Utility  2.0.2
Utilities for CAEN projects
CAENUtility.h
Go to the documentation of this file.
1 /******************************************************************************
2 *
3 * CAEN SpA - Software Division
4 * Via Vetraia, 11 - 55049 - Viareggio ITALY
5 * +39 0594 388 398 - www.caen.it
6 *
7 *******************************************************************************
8 *
9 * Copyright (C) 2019-2022 CAEN SpA
10 *
11 * This file is part of the CAEN Utility.
12 *
13 * The CAEN Utility is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 3 of the License, or (at your option) any later version.
17 *
18 * The CAEN Utility is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with the CAEN Utility; if not, see
25 * https://www.gnu.org/licenses/.
26 *
27 * SPDX-License-Identifier: LGPL-3.0-or-later
28 *
29 ***************************************************************************/
37 /******************************************************************************
38 * This library includes code from other open source projects.
39 *
40 * - "URL Parse"
41 * Source: https://github.com/Xsoda/url
42 * License: BSD 2-Clause "Simplified" License
43 * Year: 2014
44 * Author: Xsoda
45 *
46 * - "Map"
47 * Source: https://github.com/rxi/map
48 * License: MIT license
49 * Year: 2014
50 * Author: rxi
51 *
52 * - "POSIX Directory Browsing API for Windows"
53 * Source: https://github.com/octopusprime314/ReBoot/
54 * License: Copyright Kevlin Henney, 1997, 2003. All rights reserved.
55 * Permission to use, copy, modify, and distribute this software and its
56 * documentation for any purpose is hereby granted without fee, provided
57 * that this copyright and permissions notice appear in all copies and
58 * derivatives.
59 * This software is supplied "as is" without express or implied warranty.
60 * But that said, if there are any problems please get in touch.
61 * Year: 1997, 2003
62 * Author: Kevlin Henney
63 *
64 * - "XGetopt - A Unix-compatible getopt() for MFC and Win32"
65 * Source: https://www.codeproject.com/Articles/1940/XGetopt-A-Unix-compatible-getopt-for-MFC-and-Win
66 * License: Public domain
67 * Year: 2003
68 * Author: Hans Dietrich
69 *
70 ******************************************************************************/
71 
72 #ifndef CAEN_INCLUDE_CAENUTILITY_H_
73 #define CAEN_INCLUDE_CAENUTILITY_H_
74 
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
78 
79 #include <stddef.h>
80 
81 // Define exports macros (only if the _CAEN_UTILITY_EXPORT macro
82 // is defined the functions are exported in the dll)
83 #ifdef _WIN32
84 /*
85  * __stdcall would introduce name mangling if exporting functions
86  * with __declspec(dllexport). This would make GetProcAddress harder
87  * to use, so it is better to use __cdecl.
88  * Curiously, if using __stdcall with .def files rather than
89  * __declspec(dllexport), there is no name namgling (this is the
90  * approach of CAENVMELib, for example).
91  *
92  * The x86_64 architecture has its own calling convention, that
93  * never introduces name mangling, so it doesn't mattter.
94  *
95  * See https://stackoverflow.com/a/6635067/3287591.
96  */
97 #define CAEN_UTILITY_API __cdecl
98 #ifdef _CAEN_UTILITY_EXPORT
99 #define CAEN_UTILITY_DLLAPI __declspec(dllexport)
100 #else
101 #define CAEN_UTILITY_DLLAPI __declspec(dllimport)
102 #endif
103 #else
104 #define CAEN_UTILITY_API
105 #define CAEN_UTILITY_DLLAPI __attribute__((visibility("default")))
106 #endif
107 
112 #define _c_macro_str_helper(x) #x
113 #define c_macro_str(x) _c_macro_str_helper(x)
114 
116 #define c_unused_parameter(P) ((void)(P))
118 
124 // GCC
125 #if defined(__GNUC__) && !defined(__clang__)
126 #define c_gcc_version (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
127 #define c_compiler_version "GCC v" c_macro_str(__GNUC__) "." c_macro_str(__GNUC_MINOR__) "." c_macro_str(__GNUC_PATCHLEVEL__)
128 #else
129 #define c_gcc_version (-1)
130 #endif
131 
132 // Clang
133 #ifdef __clang__
134 #define c_clang_version (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
135 #define c_compiler_version "Clang v" __clang_version__
136 #else
137 #define c_clang_version (-1)
138 #endif
139 
140 // Microsoft Visual C++
141 #if defined(_MSC_VER) && !defined(__clang__)
142 #define c_msc_version (_MSC_VER)
143 #define c_compiler_version "Visual C++ v" c_macro_str(_MSC_FULL_VER)
144 #else
145 #define c_msc_version (-1)
146 #endif
147 
148 #ifndef c_compiler_version
149 #define c_compiler_version "Unknown"
150 #endif
151 
153 #ifdef _WIN32
155 #define __attribute__(A)
156 #else
157 #define __declspec(A)
158 #endif
159 
161 #ifdef _WIN32
162 #define c_nop NOP_FUNCTION
163 #else
164 #define c_nop ((void)0)
165 #endif
166 
173 #define c_attribute_format(_FMT_,_ARGS_) __attribute__((__format__(__printf__, _FMT_, _ARGS_)))
174 
180 #if c_gcc_version >= 40300 || c_clang_version >= 40000
181 #define c_attribute_alloc_size(...) __attribute__((__alloc_size__(__VA_ARGS__)))
182 #else
183 #define c_attribute_alloc_size(...) __attribute__(())
184 #endif
185 
191 #define c_declspec_allocator __declspec(allocator)
192 
199 #define c_attribute_malloc __attribute__((__malloc__))
200 
205 #define c_attribute_pure __attribute__((__pure__))
206 
213 #define c_attribute_nonnull(...) __attribute__((__nonnull__(__VA_ARGS__)))
214 
219 #if c_gcc_version >= 70000
220 #define c_attribute_fallthrough __attribute__((__fallthrough__))
221 #else
222 #define c_attribute_fallthrough c_nop
223 #endif
224 
227 #if c_gcc_version >= 0 || c_clang_version >= 0
228 #define c_force_inline __attribute__((__always_inline__)) inline
229 #elif c_msc_version >= 0
230 #define c_force_inline __forceinline
231 #else
232 #define c_force_inline inline
233 #endif
234 
240 #if c_gcc_version >= 30400 || c_clang_version >= 40000
241 #define c_nodiscard __attribute__((__warn_unused_result__))
242 #elif c_msc_version >= 1700
243 #define c_nodiscard _Check_return_
244 #else
245 #define c_nodiscard
246 #endif
247 
253 #if c_gcc_version >= 20500 || c_clang_version >= 0
254 #define c_noreturn __attribute__((__noreturn__))
255 #elif c_msc_version >= 1700
256 #define c_noreturn __declspec(noreturn)
257 #else
258 #define c_noreturn
259 #endif
260 
264 #if c_msc_version >= 0
265 #define c_acquires_lock_on_success(m) _When_(return == CAENThread_RetCode_Success, _Acquires_lock_(m))
266 #define c_releases_lock_on_success(m) _When_(return == CAENThread_RetCode_Success, _Releases_lock_(m))
267 #else
268 #define c_acquires_lock_on_success(m)
269 #define c_releases_lock_on_success(m)
270 #endif
271 
275 #if c_msc_version >= 0
276 #define c_use_decl_annotations _Use_decl_annotations_
277 #else
278 #define c_use_decl_annotations
279 #endif
280 
288 #if c_gcc_version >= 0 || c_clang_version >= 0
289 #define c_likely(x) __builtin_expect((x),1)
290 #define c_unlikely(x) __builtin_expect((x),0)
291 #else
292 #define c_likely(x) (x)
293 #define c_unlikely(x) (x)
294 #endif
295 
297 typedef enum {
310  TYPE_CHAR = 11,
314 
315  TYPE_NONE = 255,
316 } c_type_t;
317 
319 typedef enum {
326 
328 typedef void(*c_funcptr_t)(void);
329 
333 CAEN_UTILITY_DLLAPI size_t CAEN_UTILITY_API c_srtrim(char *str, const char *remove);
334 CAEN_UTILITY_DLLAPI size_t CAEN_UTILITY_API c_sltrim(char *str, const char *remove);
335 CAEN_UTILITY_DLLAPI size_t CAEN_UTILITY_API c_strim(char *str, const char *remove);
338 #ifdef __cplusplus
339 }
340 #endif
341 
342 #endif // CAEN_INCLUDE_CAENUTILITY_H_
#define CAEN_UTILITY_API
Definition: CAENUtility.h:104
size_t c_srtrim(char *str, const char *remove)
Definition: CAENUtility.c:55
c_type_t
Type of data.
Definition: CAENUtility.h:298
#define CAEN_UTILITY_DLLAPI
Definition: CAENUtility.h:105
size_t c_strim(char *str, const char *remove)
Definition: CAENUtility.c:75
void(* c_funcptr_t)(void)
Generic function pointer.
Definition: CAENUtility.h:328
c_Utility_ErrorCode_t
Library return codes.
Definition: CAENUtility.h:319
size_t c_sltrim(char *str, const char *remove)
Definition: CAENUtility.c:65