CAEN Utility  2.0.2
Utilities for CAEN projects
CAENMultiplatform.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 #ifndef CAEN_INCLUDE_CAENMULTIPLATFORM_H_
38 #define CAEN_INCLUDE_CAENMULTIPLATFORM_H_
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
45 #include <CAENUtility.h>
46 
53 #ifdef _WIN32
54 #define c_arraysize(A) _countof(A)
55 #else
56 #define c_arraysize(A) (sizeof(A)/sizeof((A)[0])+sizeof(__typeof__(int[1-2*!!__builtin_types_compatible_p(__typeof__(A),__typeof__(&A[0]))]))*0)
57 #include <endian.h>
58 #endif
59 
65 
67 CAEN_UTILITY_DLLAPI void* CAEN_UTILITY_API c_realloc(void *ptr, size_t size);
68 
70 CAEN_UTILITY_DLLAPI void* CAEN_UTILITY_API c_calloc(size_t nmemb, size_t size);
71 
73 
76 
79 CAEN_UTILITY_DLLAPI char* CAEN_UTILITY_API c_strdup(const char *str);
80 
83 CAEN_UTILITY_DLLAPI char* CAEN_UTILITY_API c_strndup(const char *str, size_t size);
91 static inline void* c_zeromem(void* dest, size_t size) {
93  return (dest == NULL) ? dest : memset(dest, 0, size);
94 }
96 static inline void* c_memset(void* dest, int val, size_t size) {
97  return (dest == NULL) ? dest : memset(dest, val, size);
98 }
100 static inline void* c_memcpy(void* __restrict dest, const void* __restrict src, size_t size) {
101  return (dest == NULL || src == NULL) ? dest : memcpy(dest, src, size);
102 }
104 static inline void* c_memmove(void* dest, const void* src, size_t size) {
105  return (dest == NULL || src == NULL) ? dest : memmove(dest, src, size);
106 }
113 
116 
118 
119 CAEN_UTILITY_DLLAPI int32_t CAEN_UTILITY_API c_setProcExec(const char *exec, c_Process_t *proc);
120 
121 CAEN_UTILITY_DLLAPI int32_t CAEN_UTILITY_API c_addProcArg(const char *arg, c_Process_t *proc);
122 
123 CAEN_UTILITY_DLLAPI int32_t CAEN_UTILITY_API c_addProcEnv(const char *varname, const char *varvalue, c_Process_t *proc);
124 
126 
128 
131 
133 
134 CAEN_UTILITY_DLLAPI int32_t CAEN_UTILITY_API c_killProcess(const char *procName);
135 
143 
145 CAEN_UTILITY_DLLAPI int32_t CAEN_UTILITY_API c_dfload(c_libhandle_t hLib, const char* fcnName, c_fcnhandle_t *funcPtr);
146 
158 CAEN_UTILITY_DLLAPI uint32_t CAEN_UTILITY_API c_ctz(uint32_t value);
159 
165 CAEN_UTILITY_DLLAPI uint32_t CAEN_UTILITY_API c_clz(uint32_t value);
166 
172 CAEN_UTILITY_DLLAPI uint32_t CAEN_UTILITY_API c_popcount(uint32_t value);
173 
179 CAEN_UTILITY_DLLAPI uint32_t CAEN_UTILITY_API c_ctz64(uint64_t value);
180 
186 CAEN_UTILITY_DLLAPI uint32_t CAEN_UTILITY_API c_clz64(uint64_t value);
187 
193 CAEN_UTILITY_DLLAPI uint32_t CAEN_UTILITY_API c_popcount64(uint64_t value);
210 CAEN_UTILITY_DLLAPI DIR* c_opendir(const char* name);
211 
213 
216 CAEN_UTILITY_DLLAPI struct dirent* c_readdir(DIR* dir);
217 
219 
220 #ifdef _WIN32 // Windows (deprecated macros)
221 #define opendir(name) c_opendir(name)
222 #define closedir(dir) c_closedir(dir)
223 #define readdir(dir) c_readdir(dir)
224 #define rewinddir(dir) c_rewinddir(dir)
225 #endif
226 
234 
237 
238 #ifndef _WIN32 // Linux (deprecated macros)
239 #define _getch() c_getch()
240 #define _kbhit() c_kbhit()
241 #endif
242 
248 CAEN_UTILITY_DLLAPI int CAEN_UTILITY_API c_access(const char* pathname, int mode);
249 
250 CAEN_UTILITY_DLLAPI char* CAEN_UTILITY_API c_getcwd(char* buf, size_t size);
251 
252 CAEN_UTILITY_DLLAPI int CAEN_UTILITY_API c_chdir(const char* path);
253 
254 CAEN_UTILITY_DLLAPI int CAEN_UTILITY_API c_rmdir(const char* path);
255 
258 
260 CAEN_UTILITY_DLLAPI int CAEN_UTILITY_API c_strcasecmp(const char* s1, const char* s2);
261 
263 CAEN_UTILITY_DLLAPI int CAEN_UTILITY_API c_strncasecmp(const char* s1, const char* s2, size_t n);
267 CAEN_UTILITY_DLLAPI char* CAEN_UTILITY_API c_getExpandedString(const char *filename);
268 
269 #define c_freeExpandedString(str) c_free(str)
270 
273 
274 CAEN_UTILITY_DLLAPI int32_t CAEN_UTILITY_API c_getDirName(char *dest, const char *source);
275 
276 CAEN_UTILITY_DLLAPI int32_t CAEN_UTILITY_API c_getBaseName(char *dest, const char *source);
277 
278 CAEN_UTILITY_DLLAPI int32_t CAEN_UTILITY_API c_remAllFiles(const char *dir);
279 
280 CAEN_UTILITY_DLLAPI int32_t CAEN_UTILITY_API c_mkdir(const char *path);
281 
283 CAEN_UTILITY_DLLAPI int32_t CAEN_UTILITY_API c_mkdirp(const char *dir);
284 
296 CAEN_UTILITY_DLLAPI int64_t CAEN_UTILITY_API c_frexp(double value, int16_t *exponent, int8_t *classification);
297 
306 CAEN_UTILITY_DLLAPI double CAEN_UTILITY_API c_ldexp(int64_t mantissa, int16_t exponent, int8_t classification);
314 
317 
320 
323 
326 
327 #define c_CopySystemTime(src, dest) (*(dest) = *(src))
328 
336 c_nodiscard
337 static inline uint16_t c_hton16(uint16_t x) {
338 #ifdef _WIN32
339  return htons(x);
340 #else
341  return htobe16(x);
342 #endif
343 }
345 static inline uint16_t c_ntoh16(uint16_t x) {
346 #ifdef _WIN32
347  return ntohs(x);
348 #else
349  return be16toh(x);
350 #endif
351 }
353 static inline uint32_t c_hton32(uint32_t x) {
354 #ifdef _WIN32
355  return htonl(x);
356 #else
357  return htobe32(x);
358 #endif
359 }
361 static inline uint32_t c_ntoh32(uint32_t x) {
362 #ifdef _WIN32
363  return ntohl(x);
364 #else
365  return be32toh(x);
366 #endif
367 }
369 static inline uint64_t c_hton64(uint64_t x) {
370 #ifdef _WIN32
371  return htonll(x);
372 #else
373  return htobe64(x);
374 #endif
375 }
377 static inline uint64_t c_ntoh64(uint64_t x) {
378 #ifdef _WIN32
379  return ntohll(x);
380 #else
381  return be64toh(x);
382 #endif
383 }
392 #ifdef _WIN32 // Windows
394 
396 
398 
399 CAEN_UTILITY_DLLAPI extern char *c_mp_optarg;
400 
402 CAEN_UTILITY_DLLAPI int CAEN_UTILITY_API c_mp_getopt(int argc, char *const *argv, const char *options);
403 
404 #else // Linux
405 #define c_mp_opterr opterr
406 #define c_mp_optopt optopt
407 #define c_mp_optind optind
408 #define c_mp_optarg optarg
409 #define c_mp_getopt getopt
410 #endif
411 
412 #define c_opterr c_mp_opterr
413 #define c_optopt c_mp_optopt
414 #define c_optind c_mp_optind
415 #define c_optarg c_mp_optarg
416 #define c_getopt c_mp_getopt
417 
419 #ifdef __cplusplus
420 }
421 #endif
422 
423 #endif // CAEN_INCLUDE_CAENMULTIPLATFORM_H_
int32_t c_pause(void)
int32_t c_getDirName(char *dest, const char *source)
void c_freeProc(c_Process_t *proc)
DIR * c_opendir(const char *name)
Not marked CAEN_UTILITY_API (meaningful only on 32-bit Windows), likely an original sin more than a c...
static void * c_memset(void *dest, int val, size_t size)
Wrapper to memset(dest, val, size), with check for NULL arguments.
int32_t c_dfload(c_libhandle_t hLib, const char *fcnName, c_fcnhandle_t *funcPtr)
static uint64_t c_hton64(uint64_t x)
bool c_checkFileExists(const char *fname)
int32_t c_dlload(const char *libName, c_libhandle_t *libHandle)
#define c_attribute_alloc_size(...)
Definition: CAENUtility.h:183
static void * c_memcpy(void *__restrict dest, const void *__restrict src, size_t size)
Wrapper to memcpy(dest, src, size), with check for NULL arguments.
static uint32_t c_ntoh32(uint32_t x)
#define c_mp_optind
int c_closedir(DIR *dir)
int c_access(const char *pathname, int mode)
int32_t c_remAllFiles(const char *dir)
#define CAEN_UTILITY_API
Definition: CAENUtility.h:104
uint32_t c_clz(uint32_t value)
void c_free(void *ptr)
int c_kbhit(void)
void * c_calloc(size_t nmemb, size_t size)
int32_t c_killProcess(const char *procName)
Main header and generic tools.
int64_t c_getpid(void)
int32_t c_dlclose(c_libhandle_t hLib)
int32_t c_getBaseName(char *dest, const char *source)
Definitions and types for CAENMultiplatform.h.
char * c_getcwd(char *buf, size_t size)
#define c_mp_opterr
void * c_fcnhandle_t
uint32_t c_ctz(uint32_t value)
int32_t c_getCurrentEnvironment(c_environment_t *dest)
uint64_t c_get_time(void)
Get time in milliseconds since 00:00:00 UTC, January 1, 1970.
double c_DiffSystemTime_sec(const c_systemtime_t *t1, const c_systemtime_t *t2)
int c_rmdir(const char *path)
void c_GetSystemTime(c_systemtime_t *t)
uint32_t c_popcount64(uint64_t value)
int32_t c_endProcess(c_Process_t *proc)
int32_t c_addProcEnv(const char *varname, const char *varvalue, c_Process_t *proc)
void c_rewinddir(DIR *dir)
int32_t c_mkdir(const char *path)
int32_t c_setProcExec(const char *exec, c_Process_t *proc)
size_t c_malloc_size(void *ptr)
#define c_attribute_nonnull(...)
Definition: CAENUtility.h:213
#define CAEN_UTILITY_DLLAPI
Definition: CAENUtility.h:105
static c_libhandle_t libHandle
Definition: CAENXMLParser.c:50
int c_strncasecmp(const char *s1, const char *s2, size_t n)
#define c_nodiscard
Definition: CAENUtility.h:245
#define c_attribute_malloc
Definition: CAENUtility.h:199
static uint16_t c_hton16(uint16_t x)
#define c_declspec_allocator
Definition: CAENUtility.h:191
char * c_getExpandedString(const char *filename)
void * c_malloc(size_t size)
double c_ldexp(int64_t mantissa, int16_t exponent, int8_t classification)
int32_t c_addProcArg(const char *arg, c_Process_t *proc)
#define c_mp_optarg
#define c_attribute_pure
Definition: CAENUtility.h:205
c_Process_t * c_newProc(void)
void c_GetLocalTime(c_systemtime_t *t)
#define c_mp_optopt
static uint32_t c_hton32(uint32_t x)
int c_getch(void)
int32_t c_mkdirp(const char *dir)
Same of c_mkdir() but creates also intermediate directories.
static void * c_memmove(void *dest, const void *src, size_t size)
Wrapper to memmove(dest, src, size), with check for NULL arguments.
static uint64_t c_ntoh64(uint64_t x)
int32_t c_startProcess(c_Process_t *proc)
#define c_mp_getopt
int c_chdir(const char *path)
int c_strcasecmp(const char *s1, const char *s2)
int64_t c_frexp(double value, int16_t *exponent, int8_t *classification)
static uint16_t c_ntoh16(uint16_t x)
char * c_strndup(const char *str, size_t size)
Wrapper to strndup(str, size), with check for NULL arguments. To be freed with c_free().
struct dirent * c_readdir(DIR *dir)
Not marked CAEN_UTILITY_API (meaningful only on 32-bit Windows), likely an original sin more than a c...
void * c_realloc(void *ptr, size_t size)
char * c_strdup(const char *str)
Wrapper to strdup(str), with check for NULL arguments. To be freed with c_free(). ...
void * c_libhandle_t
int32_t c_getNCPU(void)
uint32_t c_clz64(uint64_t value)
uint64_t c_getCurrentTimeRepresentation(void)
uint32_t c_ctz64(uint64_t value)
static void * c_zeromem(void *dest, size_t size)
Wrapper to memset(dest, 0, size), with check for NULL arguments.
uint32_t c_popcount(uint32_t value)