Coverage Report

Created: 2023-05-28 06:42

/src/netcdf-c/liblib/nc_initialize.c
Line
Count
Source (jump to first uncovered line)
1
/*********************************************************************
2
 *   Copyright 2018, UCAR/Unidata
3
 *   See netcdf/COPYRIGHT file for copying and redistribution conditions.
4
 *********************************************************************/
5
6
#include "config.h"
7
8
#ifdef USE_PARALLEL
9
#include <mpi.h>
10
#endif
11
12
#include "ncdispatch.h"
13
14
extern int NC3_initialize(void);
15
extern int NC3_finalize(void);
16
17
#ifdef USE_NETCDF4
18
#include "nc4internal.h"
19
extern int NC4_initialize(void);
20
extern int NC4_finalize(void);
21
#endif
22
23
#ifdef USE_HDF5
24
#include "hdf5internal.h"
25
extern int NC_HDF5_initialize(void);
26
extern int NC_HDF5_finalize(void);
27
#endif
28
29
#ifdef ENABLE_DAP2
30
extern int NCD2_initialize(void);
31
extern int NCD2_finalize(void);
32
#endif
33
34
#ifdef ENABLE_DAP4
35
extern int NCD4_initialize(void);
36
extern int NCD4_finalize(void);
37
#endif
38
39
#ifdef USE_PNETCDF
40
extern int NCP_initialize(void);
41
extern int NCP_finalize(void);
42
#endif
43
44
#ifdef USE_HDF4
45
extern int NC_HDF4_initialize(void);
46
extern int NC_HDF4_finalize(void);
47
#endif
48
49
#ifdef ENABLE_S3
50
EXTERNL int NC_s3sdkinitialize(void);
51
EXTERNL int NC_s3sdkfinalize(void);
52
#endif
53
54
#ifdef _MSC_VER
55
#include <io.h>
56
#include <fcntl.h>
57
#endif
58
59
int NC_initialized = 0;
60
int NC_finalized = 1;
61
62
#ifdef ENABLE_ATEXIT_FINALIZE
63
/* Provide the void function to give to atexit() */
64
static void
65
finalize_atexit(void)
66
1
{
67
1
    (void)nc_finalize();
68
1
}
69
#endif
70
71
/**
72
This procedure invokes all defined
73
initializers, and there is an initializer
74
for every known dispatch table.
75
So if you modify the format of NC_Dispatch,
76
then you need to fix it everywhere.
77
It also initializes appropriate external libraries.
78
*/
79
80
int
81
nc_initialize()
82
1
{
83
1
    int stat = NC_NOERR;
84
85
1
    if(NC_initialized) return NC_NOERR;
86
1
    NC_initialized = 1;
87
1
    NC_finalized = 0;
88
89
    /* Do general initialization */
90
1
    if((stat = NCDISPATCH_initialize())) goto done;
91
92
    /* Initialize each active protocol */
93
1
    if((stat = NC3_initialize())) goto done;
94
#ifdef ENABLE_DAP
95
    if((stat = NCD2_initialize())) goto done;
96
#endif
97
#ifdef ENABLE_DAP4
98
    if((stat = NCD4_initialize())) goto done;
99
#endif
100
#ifdef USE_PNETCDF
101
    if((stat = NCP_initialize())) goto done;
102
#endif
103
1
#ifdef USE_NETCDF4
104
1
    if((stat = NC4_initialize())) goto done;
105
1
#endif /* USE_NETCDF4 */
106
#ifdef USE_HDF5
107
    if((stat = NC_HDF5_initialize())) goto done;
108
#endif
109
#ifdef USE_HDF4
110
    if((stat = NC_HDF4_initialize())) goto done;
111
#endif
112
#ifdef ENABLE_S3
113
    if((stat = NC_s3sdkinitialize())) goto done;
114
#endif
115
1
#ifdef ENABLE_NCZARR
116
1
    if((stat = NCZ_initialize())) goto done;
117
1
#endif
118
119
1
#ifdef ENABLE_ATEXIT_FINALIZE
120
    /* Use atexit() to invoke nc_finalize */
121
1
    if(atexit(finalize_atexit))
122
0
  fprintf(stderr,"atexit failed\n");
123
1
#endif
124
125
1
done:
126
1
    return stat;
127
1
}
128
129
/**
130
This procedure invokes all defined
131
finalizers, and there should be one
132
for every known dispatch table.
133
So if you modify the format of NC_Dispatch,
134
then you need to fix it everywhere.
135
It also finalizes appropriate external libraries.
136
*/
137
138
int
139
nc_finalize(void)
140
1
{
141
1
    int stat = NC_NOERR;
142
1
    int failed = stat;
143
144
1
    if(NC_finalized) goto done;
145
1
    NC_initialized = 0;
146
1
    NC_finalized = 1;
147
148
    /* Finalize each active protocol */
149
150
#ifdef ENABLE_DAP2
151
    if((stat = NCD2_finalize())) failed = stat;
152
#endif
153
#ifdef ENABLE_DAP4
154
    if((stat = NCD4_finalize())) failed = stat;
155
#endif
156
157
#ifdef USE_PNETCDF
158
    if((stat = NCP_finalize())) failed = stat;
159
#endif
160
161
#ifdef USE_HDF4
162
    if((stat = NC_HDF4_finalize())) failed = stat;
163
#endif /* USE_HDF4 */
164
165
1
#ifdef USE_NETCDF4
166
1
    if((stat = NC4_finalize())) failed = stat;
167
1
#endif /* USE_NETCDF4 */
168
169
#ifdef USE_HDF5
170
    if((stat = NC_HDF5_finalize())) failed = stat;
171
#endif
172
173
1
#ifdef ENABLE_NCZARR
174
1
    if((stat = NCZ_finalize())) failed = stat;
175
1
#endif
176
177
#ifdef ENABLE_S3
178
    if((stat = NC_s3sdkfinalize())) failed = stat;
179
#endif
180
181
1
    if((stat = NC3_finalize())) failed = stat;
182
183
    /* Do general finalization */
184
1
    if((stat = NCDISPATCH_finalize())) failed = stat;
185
186
1
done:
187
1
    if(failed) fprintf(stderr,"nc_finalize failed: %d\n",failed);
188
1
    return failed;
189
1
}