CAEN Utility  2.0.2
Utilities for CAEN projects
CAENUtility.c
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 VISUAL_LEAK_DETECTOR_ENABLED
38 #define VISUAL_LEAK_DETECTOR_ENABLED (0)
39 #endif
40 #define IS_VLD_ENABLED (_DEBUG && _WIN32 && _CAEN_UTILITY_EXPORT && VISUAL_LEAK_DETECTOR_ENABLED)
41 #if IS_VLD_ENABLED
42 #include <vld.h>
43 #endif
44 
45 #include <CAENUtility.h>
46 
47 #include <string.h>
48 
49 #include <CAENLogger.h>
50 #include <CAENRandom.h>
51 #include <CAENXMLParser.h>
52 #include <CAENMultiplatform.h>
53 
54 // string right trim
55 size_t c_srtrim(char *str, const char *remove) {
56  size_t l = strlen(str);
57  while (strchr(remove, str[l - 1]) != NULL && l > 0) {
58  str[l - 1] = '\0';
59  l = strlen(str);
60  }
61  return strlen(str);
62 }
63 
64 // string left trim
65 size_t c_sltrim(char *str, const char *remove) {
66  char *ptr = str;
67  while (strchr(remove, ptr[0]) != NULL && strlen(ptr) > 0)
68  ptr++;
69  for (size_t i = 0; i <= strlen(ptr); i++)
70  str[i] = ptr[i];
71  return strlen(str);
72 }
73 
74 // string trim
75 size_t c_strim(char *str, const char *remove) {
76  if (c_srtrim(str, remove) == 0)
77  return 0;
78  if (c_sltrim(str, remove) == 0)
79  return 0;
80  return strlen(str);
81 }
82 
83 static int32_t _initLibrary(void) {
84  // Get time (in ms) to initialize stuff
85  const uint64_t time = c_get_time();
86 
87  // Set logger start time
88  c_lsetst(time);
89 
90 #ifndef _NOUSEXML
91  // Test if the loaded XML version is compatible with the linked one.
92  LIBXML_TEST_VERSION;
93 
94  // Overload the libxml memory functions with the library ones.
95  // A function name, in this context, is converted to the address of the function.
96  xmlMemSetup(c_free, c_malloc, c_realloc, c_strdup);
97 #endif //_NOUSEXML
98 
99  // Initialize random generators
100  c_rand32_init();
101  c_rand64_init();
102 
103  // Seed random generators with time
104  c_rand32_seed((uint32_t)time);
105  c_rand64_seed(time);
106 
108 }
109 
110 static int32_t _closeLibrary(void) {
111 
112 #ifndef _NOUSEXML
113  // Clean XML stuff
115 #endif //_NOUSEXML
116 
117  // Deinit random generators
118  c_rand32_deinit();
119  c_rand64_deinit();
120 
121  // Deinit logger
122  c_ldeinit();
123 
125 }
126 
127 #ifdef _CAEN_UTILITY_EXPORT
128 #ifdef _WIN32
129 /**********************************************************************\
130  DllMain
131 \**********************************************************************/
132 BOOL WINAPI DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
133  UNREFERENCED_PARAMETER(hModule);
134  UNREFERENCED_PARAMETER(lpReserved);
135  switch (ul_reason_for_call) {
136  case DLL_PROCESS_ATTACH:
137  _initLibrary();
138  break;
139  case DLL_PROCESS_DETACH:
140  _closeLibrary();
141  break;
142  case DLL_THREAD_ATTACH:
143  break;
144  case DLL_THREAD_DETACH:
145  break;
146  }
147  return TRUE;
148 }
149 #else
150 /**********************************************************************\
151  CAENUtility_init
152 \**********************************************************************/
153 void __attribute__ ((constructor)) CAENUtility_init(void) {
154  _initLibrary();
155 }
156 /**********************************************************************\
157  CAENUtility_close
158 \**********************************************************************/
159 void __attribute__ ((destructor)) CAENUtility_close(void) {
160  _closeLibrary();
161 }
162 #endif
163 #endif
void c_lsetst(uint64_t time)
Definition: CAENLogger.c:309
void c_free(void *ptr)
int32_t c_rand64_init(void)
Definition: CAENRandom.c:199
void c_rand32_seed(uint32_t seed)
Definition: CAENRandom.c:225
Main header and generic tools.
uint64_t c_get_time(void)
Get time in milliseconds since 00:00:00 UTC, January 1, 1970.
void c_rand32_deinit(void)
Definition: CAENRandom.c:189
size_t c_srtrim(char *str, const char *remove)
Definition: CAENUtility.c:55
Tools to parse an XML file using libxml.
int32_t c_rand32_init(void)
Definition: CAENRandom.c:173
void c_ldeinit(void)
Definition: CAENLogger.c:390
#define TRUE
void * c_malloc(size_t size)
Generic wrappers to platform-dependent functions.
size_t c_strim(char *str, const char *remove)
Definition: CAENUtility.c:75
void c_rand64_deinit(void)
Definition: CAENRandom.c:215
Pseudo-random number generator implemented on MT19937.
static int32_t _closeLibrary(void)
Definition: CAENUtility.c:110
Logger implemented in C.
void c_rand64_seed(uint64_t seed)
Definition: CAENRandom.c:240
void c_xml_parsercleanup(void)
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(). ...
size_t c_sltrim(char *str, const char *remove)
Definition: CAENUtility.c:65
static int32_t _initLibrary(void)
Definition: CAENUtility.c:83