OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
epr_field.c
Go to the documentation of this file.
1 /*
2  * $Id: epr_field.c,v 1.1.1.1 2004-10-28 19:22:23 norman Exp $
3  *
4  * Copyright (C) 2002 by Brockmann Consult (info@brockmann-consult.de)
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation. This program is distributed in the hope it will
9  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
10  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16  */
17 
18 #include <assert.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "epr_api.h"
25 #include "epr_core.h"
26 #include "epr_string.h"
27 #include "epr_ptrarray.h"
28 #include "epr_swap.h"
29 #include "epr_field.h"
30 #include "epr_record.h"
31 #include "epr_param.h"
32 #include "epr_dsd.h"
33 #include "epr_msph.h"
34 #include "epr_band.h"
35 #include "epr_bitmask.h"
36 
37 
52 EPR_SFieldInfo* epr_create_field_info(EPR_EDataTypeId data_type_id, char* description, char* field_name, epr_uint num_elems, epr_uint num_bytes, epr_uint more_count, char* unit)
53 {
54  EPR_SFieldInfo* field_info = NULL;
55  //epr_uint data_type_size;
56 
57  field_info = (EPR_SFieldInfo*) calloc(1, sizeof (EPR_SFieldInfo));
58  if (field_info == NULL)
59  {
61  "epr_create_field_info: out of memory");
62  return NULL;
63  }
64 
65  epr_assign_string(&field_info->name, field_name);
66  if (field_info->name == NULL)
67  {
69  "epr_create_field_info: out of memory");
70  return NULL;
71  }
72 
73  if (description == NULL)
74  {
75  field_info->description = NULL;
76  } else
77  epr_assign_string(&field_info->description, description);
78 
79  if (unit == NULL)
80  {
81  field_info->unit = NULL;
82  } else
83  epr_assign_string(&field_info->unit, unit);
84 
85  //data_type_size = epr_get_data_type_size(data_type_id);
86 
87  field_info->num_elems = num_elems;
88 
89 
90  /* IMPORTANT !!!!it's one or the other !!!!*/
91  field_info->tot_size = num_elems * num_bytes * more_count;
92  /*for the case data_type_size=num_bytes*/
93  /*this is not true for the case "spare" (1!=60)*/
94  /* field_info->tot_size = num_elems * data_type_size * more_count;*/
95 
96 
97  field_info->data_type_id = data_type_id;
98 
99  return field_info;
100 }
101 
102 
111 void epr_free_field_info(EPR_SFieldInfo* field_info)
112 {
113  if (field_info == NULL)
114  return;
115 
116  epr_free_string(field_info->name);
117  field_info->name = NULL;
118 
119  epr_free_string(field_info->description);
120  field_info->description = NULL;
121 
122  epr_free_string(field_info->unit);
123  field_info->unit = NULL;
124 
125  field_info->num_elems = 0;
126  field_info->tot_size = 0;
127  field_info->data_type_id = 0;
128 
129  free(field_info);
130 }
131 
132 
133 /*
134  Function: epr_create_field
135  Access: public API
136  Changelog: 2002/01/23 mp initial version
137  */
147 EPR_SField* epr_create_field(EPR_SFieldInfo* field_info)
148 {
149  EPR_SField* field = NULL;
150  epr_uint data_type_size = 0;
151 
152  field = (EPR_SField*) calloc(1, sizeof(EPR_SField));
153  if (field == NULL)
154  {
156  "epr_create_field: out of memory");
157  return NULL;
158  }
159  field->magic = EPR_MAGIC_FIELD;
160 
161 
162  data_type_size = epr_get_data_type_size(field_info->data_type_id);
163  if (data_type_size == 0)
164  {
166  "epr_create_field: illegal field_info data type identifier");
167  return NULL;
168  }
169  field->info = field_info;
170 
171  if (field_info->num_elems == 0)
172  {
173  free(field);
175  "epr_create_field: field_info->num_elems is zero");
176  return NULL;
177  }
178 
179  if (field_info->data_type_id == e_tid_spare) {
180  field->elems = calloc(field_info->tot_size, 1 /*byte*/);
181  } else {
182  if (field_info->data_type_id == e_tid_string) {
183  /*
184  Note that string always are considered as one single element,
185  so we get the total number of characters from tot_size.
186  Addidionally we reserve an extra character for the trailing zero (terminator),
187  */
188  field->elems = calloc(field_info->tot_size + 1, sizeof (char));
189  } else {
190  field->elems = calloc(field_info->num_elems, data_type_size);
191  }
192  }
193  if (field->elems == NULL)
194  {
195  free(field);
197  "epr_create_field: out of memory");
198  return NULL;
199  }
200 
201 /*
202  if (field->info->data_type_id == e_tid_string) {
203  printf("string field: name=%s, num_elems=%d, tot_size=%d\n", field->info->name, field->info->num_elems, field->info->tot_size);
204  }
205 */
206  return field;
207 }
208 
209 
218 void epr_free_field(EPR_SField* field)
219 {
220  if (field == NULL)
221  return;
222 
223  /* Do NOT free field->info since many fields can
224  share the same field->info! */
225  field->info = NULL;
226 
227  if (field->elems != NULL)
228  {
229  free(field->elems);
230  field->elems = NULL;
231  }
232 
233  free(field);
234 }
235 
247 const EPR_SField* epr_get_field(const EPR_SRecord* record, const char* field_name)
248 {
249  EPR_SField* field;
250  epr_uint field_index;
251 
252  epr_clear_err();
253 
254  if (record == NULL) {
256  "epr_get_field: record must not be NULL");
257  return NULL;
258  }
259  if (field_name == NULL) {
261  "epr_get_field: field_name must not be NULL");
262  return NULL;
263  }
264 
265  for (field_index = 0; field_index < record->num_fields; field_index++) {
266  field = record->fields[field_index];
267  if (strcmp(field_name, field->info->name) == 0) {
268  return field;
269  }
270  }
272  "epr_get_field: field not found");
273  return NULL;
274 }
275 
276 
277 epr_uint epr_get_field_num_elems(const EPR_SField* field)
278 {
279  epr_clear_err();
280 
281  if (field == NULL) {
283  "epr_get_field_num_elems: field-identifier must not be NULL");
284  return 0;
285  }
286  return field->info->num_elems;
287 }
288 
289 const char* epr_get_field_name(const EPR_SField* field)
290 {
291  epr_clear_err();
292 
293  if (field == NULL) {
295  "epr_get_field_name: field-identifier must not be NULL");
296  return NULL;
297  }
298  return field->info->name;
299 }
300 
301 
302 EPR_EDataTypeId epr_get_field_type(const EPR_SField* field)
303 {
304  epr_clear_err();
305 
306  if (field == NULL) {
308  "epr_get_field_type: field-identifier must not be NULL");
309  return e_tid_unknown;
310  }
311  return field->info->data_type_id;
312 }
313 
314 const char* epr_get_field_unit(const EPR_SField* field)
315 {
316  epr_clear_err();
317 
318  if (field == NULL) {
320  "epr_get_field_unit: field-identifier must not be NULL");
321  return NULL;
322  }
323  return field->info->unit;
324 }
325 
326 const char* epr_get_field_description(const EPR_SField* field)
327 {
328  epr_clear_err();
329 
330  if (field == NULL) {
332  "epr_get_field_description: field-identifier must not be NULL");
333  return NULL;
334  }
335  return field->info->description;
336 }
EPR_SField * epr_create_field(EPR_SFieldInfo *field_info)
Definition: epr_field.c:147
void epr_free_field(EPR_SField *field)
Definition: epr_field.c:218
@ e_err_out_of_memory
Definition: epr_api.h:83
void epr_free_field_info(EPR_SFieldInfo *field_info)
Definition: epr_field.c:111
@ e_err_illegal_arg
Definition: epr_api.h:81
epr_uint epr_get_data_type_size(EPR_EDataTypeId data_type_id)
Definition: epr_core.c:148
unsigned int epr_uint
Definition: epr_api.h:188
#define NULL
Definition: decode_rs.h:63
char * epr_assign_string(char **str_clone, const char *str)
Definition: epr_string.c:29
const EPR_SField * epr_get_field(const EPR_SRecord *record, const char *field_name)
Definition: epr_field.c:247
EPR_EDataTypeId epr_get_field_type(const EPR_SField *field)
Definition: epr_field.c:302
@ e_err_illegal_data_type
Definition: epr_api.h:86
@ e_err_null_pointer
Definition: epr_api.h:80
@ e_err_invalid_value
Definition: epr_api.h:108
const char * epr_get_field_description(const EPR_SField *field)
Definition: epr_field.c:326
@ e_tid_spare
Definition: epr_api.h:66
@ e_err_invalid_record_name
Definition: epr_api.h:104
EPR_SFieldInfo * epr_create_field_info(EPR_EDataTypeId data_type_id, char *description, char *field_name, epr_uint num_elems, epr_uint num_bytes, epr_uint more_count, char *unit)
Definition: epr_field.c:52
void epr_clear_err()
Definition: epr_core.c:247
void epr_set_err(EPR_EErrCode err_code, const char *err_message)
Definition: epr_core.c:221
description
Definition: setup.py:16
@ e_tid_string
Definition: epr_api.h:64
const char * epr_get_field_unit(const EPR_SField *field)
Definition: epr_field.c:314
enum EPR_DataTypeId EPR_EDataTypeId
Definition: epr_api.h:161
#define EPR_MAGIC_FIELD
Definition: epr_api.h:198
@ e_tid_unknown
Definition: epr_api.h:46
epr_uint epr_get_field_num_elems(const EPR_SField *field)
Definition: epr_field.c:277
const char * epr_get_field_name(const EPR_SField *field)
Definition: epr_field.c:289
void epr_free_string(char *str)
Definition: epr_string.c:100