/src/netcdf-c/libdispatch/nc.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2022, University Corporation for Atmospheric Research |
3 | | * See netcdf/COPYRIGHT file for copying and redistribution conditions. |
4 | | */ |
5 | | /** |
6 | | * @file |
7 | | * @internal |
8 | | * |
9 | | * This file contains functions that work with the NC struct. There is |
10 | | * an NC struct for every open netCDF file. |
11 | | * |
12 | | * @author Glenn Davis |
13 | | */ |
14 | | |
15 | | #include "config.h" |
16 | | #include <stdlib.h> |
17 | | #include <string.h> |
18 | | #include <assert.h> |
19 | | #ifdef HAVE_UNISTD_H |
20 | | #include <unistd.h> |
21 | | #endif |
22 | | #include "ncdispatch.h" |
23 | | |
24 | | #ifndef nulldup |
25 | | #define nulldup(x) ((x)?strdup(x):(x)) |
26 | | #endif |
27 | | |
28 | | /** This is the default create format for nc_create and nc__create. */ |
29 | | static int default_create_format = NC_FORMAT_CLASSIC; |
30 | | |
31 | | /** |
32 | | * Find the NC struct for an open file, using the ncid. |
33 | | * |
34 | | * @param ncid The ncid to find. |
35 | | * @param ncpp Pointer that gets a pointer to the NC. |
36 | | * |
37 | | * @return ::NC_NOERR No error. |
38 | | * @return ::NC_EBADID ncid not found. |
39 | | * @author Glenn Davis, Dennis Heimbigner |
40 | | */ |
41 | | int |
42 | | NC_check_id(int ncid, NC** ncpp) |
43 | 108 | { |
44 | 108 | NC* nc = find_in_NCList(ncid); |
45 | 108 | if(nc == NULL) return NC_EBADID; |
46 | 108 | if(ncpp) *ncpp = nc; |
47 | 108 | return NC_NOERR; |
48 | 108 | } |
49 | | |
50 | | /** |
51 | | * Free an NC struct and its related resources. Before this is done, |
52 | | * be sure to remove the NC from the open file list with |
53 | | * del_from_NCList(). |
54 | | * |
55 | | * @param ncp Pointer to the NC struct to be freed. |
56 | | * |
57 | | * @author Glenn Davis, Dennis Heimbigner |
58 | | */ |
59 | | void |
60 | | free_NC(NC *ncp) |
61 | 104 | { |
62 | 104 | if(ncp == NULL) |
63 | 0 | return; |
64 | 104 | if(ncp->path) |
65 | 104 | free(ncp->path); |
66 | | /* We assume caller has already cleaned up ncp->dispatchdata */ |
67 | 104 | free(ncp); |
68 | 104 | } |
69 | | |
70 | | /** |
71 | | * Create and initialize a new NC struct. The ncid is assigned later. |
72 | | * |
73 | | * @param dispatcher An pointer to the NC_Dispatch table that should |
74 | | * be used by this NC. |
75 | | * @param path The name of the file. |
76 | | * @param mode The open or create mode. |
77 | | * @param model An NCmodel instance, provided by NC_infermodel(). |
78 | | * @param ncpp A pointer that gets a pointer to the newlly allocacted |
79 | | * and initialized NC struct. |
80 | | * |
81 | | * @return ::NC_NOERR No error. |
82 | | * @return ::NC_ENOMEM Out of memory. |
83 | | * @author Glenn Davis, Dennis Heimbigner |
84 | | */ |
85 | | int |
86 | | new_NC(const NC_Dispatch* dispatcher, const char* path, int mode, NC** ncpp) |
87 | 104 | { |
88 | 104 | NC *ncp = (NC*)calloc(1,sizeof(NC)); |
89 | 104 | if(ncp == NULL) return NC_ENOMEM; |
90 | 104 | ncp->dispatch = dispatcher; |
91 | 104 | ncp->path = nulldup(path); |
92 | 104 | ncp->mode = mode; |
93 | 104 | if(ncp->path == NULL) { /* fail */ |
94 | 0 | free_NC(ncp); |
95 | 0 | return NC_ENOMEM; |
96 | 0 | } |
97 | 104 | if(ncpp) { |
98 | 104 | *ncpp = ncp; |
99 | 104 | } else { |
100 | 0 | free_NC(ncp); |
101 | 0 | } |
102 | 104 | return NC_NOERR; |
103 | 104 | } |
104 | | |
105 | | /** |
106 | | * This function sets a default create flag that will be logically |
107 | | * or'd to whatever flags are passed into nc_create for all future |
108 | | * calls to nc_create. |
109 | | * |
110 | | * @param format The format that should become the default. |
111 | | * @param old_formatp Pointer that gets the previous default. Ignored |
112 | | * if NULL. |
113 | | * |
114 | | * @return ::NC_NOERR No error. |
115 | | * @return ::NC_ENOTBUILD Requested format not built with this install. |
116 | | * @return ::NC_EINVAL Invalid input. |
117 | | * @author Ed Hartnett, Dennis Heimbigner |
118 | | */ |
119 | | int |
120 | | nc_set_default_format(int format, int *old_formatp) |
121 | 0 | { |
122 | | /* Return existing format if desired. */ |
123 | 0 | if (old_formatp) |
124 | 0 | *old_formatp = default_create_format; |
125 | | |
126 | | /* Make sure only valid format is set. */ |
127 | | #ifndef ENABLE_CDF5 |
128 | | if (format == NC_FORMAT_CDF5) |
129 | | return NC_ENOTBUILT; |
130 | | #endif |
131 | | #ifdef USE_HDF5 |
132 | | if (format != NC_FORMAT_CLASSIC && format != NC_FORMAT_64BIT_OFFSET && |
133 | | format != NC_FORMAT_NETCDF4 && format != NC_FORMAT_NETCDF4_CLASSIC && |
134 | | format != NC_FORMAT_CDF5) |
135 | | return NC_EINVAL; |
136 | | #else |
137 | 0 | if (format == NC_FORMAT_NETCDF4 || format == NC_FORMAT_NETCDF4_CLASSIC) |
138 | 0 | return NC_ENOTBUILT; |
139 | 0 | if (format != NC_FORMAT_CLASSIC && format != NC_FORMAT_64BIT_OFFSET && |
140 | 0 | format != NC_FORMAT_CDF5) |
141 | 0 | return NC_EINVAL; |
142 | 0 | #endif |
143 | 0 | default_create_format = format; |
144 | 0 | return NC_NOERR; |
145 | 0 | } |
146 | | |
147 | | /** |
148 | | * Get the current default format. |
149 | | * |
150 | | * @return the default format. |
151 | | * @author Ed Hartnett |
152 | | */ |
153 | | int |
154 | | nc_get_default_format(void) |
155 | 114 | { |
156 | 114 | return default_create_format; |
157 | 114 | } |