OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
ias_odl_get_field.c
Go to the documentation of this file.
1 /******************************************************************************
2 
3 UNIT NAME: ias_odl_get_field.c
4 
5 PURPOSE: Get the requested ODL field and convert it, if necessary.
6 
7 RETURN VALUE:
8  Type = int
9 
10 Value Description
11 -------------- ----------------------------------------
12 SUCCESS Found and converted field(s) into
13  requested type
14 IAS_ODL_NOT_ENOUGH_MEMORY_SUPPLIED Not enough memory passed in
15 IAS_ODL_NOT_FOUND Group/label not found
16 IAS_ODL_INVALID_DATA_TYPE Data type mismatch
17 ERROR Fatal error
18 
19 ******************************************************************************/
20 
21 #include "lablib3.h"
22 #include "ias_odl.h"
23 #include "ias_logging.h"
24 
25 extern char ODLErrorMessage[]; /* External Variables */
26 
27 /* function prototype */
29 (
30  void *p_destination, /* I/O: attribute to convert */
31  IAS_ODL_TYPE parm_type, /* I: ODL data type */
32  char *kvalue /* I: Value to convert */
33 );
34 
36 (
37  void *p_MemoryAddr, /* I/O: List of attributes to retrieve */
38  int MemorySize, /* I: mem size of attributes */
39  IAS_ODL_TYPE ValueType, /* I: ODL data type */
40  IAS_OBJ_DESC *p_ODLTree, /* I: ODL tree */
41  const char *p_ClassName, /* I: Group/Object name */
42  const char *p_LabelName, /* I: Field to get */
43  int *p_Count /* I: number of values in attribute */
44 )
45 {
46  OBJDESC *p_lp; /* Object Descriptor */
47  KEYWORD *p_kw; /* Keyword Name */
48  char *p_kwv; /* Keyword Value */
49  char *p_keyword; /* Copy of the Keyword Value */
50  int i; /* loop counter */
51  char *p_word; /* word to convert */
52  int ret_code = 0; /* function return value */
53 
54  *p_Count = 0;
55 
56  if ( (p_LabelName == NULL) || (strlen(p_LabelName) == 0) )
57  {
58  IAS_LOG_ERROR("Attribute name missing");
59  return ERROR;
60  }
61 
62  if ((p_lp = OdlFindObjDesc(p_ODLTree, p_ClassName, p_LabelName, NULL, 1,
64  {
65  /* it's possible that we have NULL for a group(p_ClassName), that's OK;
66  no need to log an error, just return */
67  return IAS_ODL_NOT_FOUND;
68  }
69 
70  if ((p_kw = OdlFindKwd(p_lp, p_LabelName, NULL, 1 ,ODL_RECURSIVE_DOWN))
71  == NULL)
72  {
73  if ((long)strlen(ODLErrorMessage) <= 1 )
74  {
76  IAS_LOG_ERROR("Keyword '%s' not found", p_LabelName);
77  }
78  return IAS_ODL_NOT_FOUND;
79  }
80 
81  if ((p_kwv = OdlGetKwdValue(p_kw)) == NULL)
82  {
83  if ((long)strlen(ODLErrorMessage) <= 1 )
84  {
86  IAS_LOG_ERROR("Keyword %s not found", p_LabelName);
87  }
88  return IAS_ODL_NOT_FOUND;
89  }
90  if ((p_keyword= malloc(strlen(p_kwv)+1)) == NULL)
91  {
92  IAS_LOG_ERROR("Malloc error");
93  return ERROR;
94  }
95  (void)strcpy(p_keyword,p_kwv);
96 
97  /* When working with a set or a sequence, all of the newline characters
98  should be removed from the incoming string. The ODL library is placing
99  newlines into these strings around character 2025 even though they don't
100  exist in the ODL file. */
101  if ((OdlGetKwdValueType(p_kw) == ODL_SET) ||
102  (OdlGetKwdValueType(p_kw) == ODL_SEQUENCE))
103  {
104  char * p_newline;
105 
106  while ((p_newline = strchr(p_keyword, '\n')) != NULL)
107  {
108  while (*p_newline != '\0')
109  {
110  *p_newline = *(p_newline + 1);
111  p_newline++;
112  }
113  }
114  }
115 
116  if (OdlGetKwdValueType(p_kw) == ODL_SET)
117  {
118  p_word = strtok(p_keyword,"(,) \"\n");
119  while(p_word != NULL)
120  {
121  if (strlen(p_word))
122  {
123  switch (ValueType)
124  {
125  case IAS_ODL_Long :
126  MemorySize -= sizeof(long);
127  if (MemorySize < 0 )
128  {
129  IAS_LOG_ERROR("Input ODL value overflows allocated "
130  "space ");
131  free(p_keyword);
133  }
134  if ( ( ret_code = convert_string(p_MemoryAddr,
135  ValueType,p_word) ) != SUCCESS )
136  {
137  IAS_LOG_ERROR("Converting %s keyword's value "
138  "%s",p_LabelName, p_word);
139  free(p_keyword);
140  return ret_code;
141  }
142  p_MemoryAddr = (long *)p_MemoryAddr + 1;
143  break;
144 
145  case IAS_ODL_Int :
146  MemorySize -= sizeof(int);
147  if (MemorySize < 0 )
148  {
149  IAS_LOG_ERROR("Input ODL value overflows allocated "
150  "space ");
151  free(p_keyword);
153  }
154  if ( ( ret_code = convert_string(p_MemoryAddr,
155  ValueType,p_word) ) != SUCCESS )
156  {
157  IAS_LOG_ERROR("Converting %s keyword's value "
158  "%s",p_LabelName, p_word);
159  free(p_keyword);
160  return ret_code;
161  }
162  p_MemoryAddr = (int *)p_MemoryAddr + 1;
163  break;
164 
165  case IAS_ODL_Float :
166  MemorySize -= sizeof(float);
167  if (MemorySize < 0 )
168  {
169  IAS_LOG_ERROR("Input ODL value overflows allocated "
170  "space ");
171  free(p_keyword);
173  }
174  if ( ( ret_code = convert_string(p_MemoryAddr,
175  ValueType,p_word) ) != SUCCESS )
176  {
177  IAS_LOG_ERROR("Converting %s keyword's value "
178  "%s",p_LabelName, p_word);
179  free(p_keyword);
180  return ret_code;
181  }
182  p_MemoryAddr = (float *)p_MemoryAddr + 1;
183  break;
184 
185  case IAS_ODL_Double :
186  case IAS_ODL_Sci_Not :
187  MemorySize -= sizeof(double);
188  if (MemorySize < 0 )
189  {
190  IAS_LOG_ERROR("Input ODL value overflows allocated "
191  "space ");
192  free(p_keyword);
194  }
195  if ( ( ret_code = convert_string(p_MemoryAddr,
196  ValueType,p_word) ) != SUCCESS )
197  {
198  IAS_LOG_ERROR("Converting %s keyword's value "
199  "%s",p_LabelName, p_word);
200  free(p_keyword);
201  return ret_code;
202  }
203  p_MemoryAddr = (double *)p_MemoryAddr + 1;
204  break;
205 
206  case IAS_ODL_ArrayOfString :
207  MemorySize -= sizeof(char *);
208  if (MemorySize < 0 )
209  {
210  IAS_LOG_ERROR("Input ODL value overflows allocated "
211  "space ");
212  free(p_keyword);
214  }
215  if ( ( ret_code = convert_string(p_MemoryAddr,
216  ValueType,p_word) ) != SUCCESS )
217  {
218  for( i=0;i<*p_Count;i++)
219  free((char *)p_MemoryAddr + i);
220  IAS_LOG_ERROR("Converting %s keyword's value "
221  "%s",p_LabelName, p_word);
222  free(p_keyword);
223  return ret_code;
224  }
225  p_MemoryAddr = (char *)p_MemoryAddr + sizeof(char *);
226  break;
227 
228  default:
229  (void)strncpy( p_MemoryAddr, p_word, MemorySize);
230  IAS_LOG_ERROR("Type IAS_ODL_String is not valid for arrays "
231  "of strings");
232  free(p_keyword);
233  return ERROR;
234  }
235  *p_Count += 1;
236  p_word = strtok(NULL,",() \"\n");
237  }
238  }
239  }
240  else if (OdlGetKwdValueType(p_kw) == ODL_SEQUENCE)
241  {
242  p_word = strtok(p_keyword,"{,} \"\n");
243  while(p_word != NULL)
244  {
245  if (strlen(p_word))
246  {
247  switch (ValueType)
248  {
249  case IAS_ODL_ArrayOfString :
250  MemorySize -= sizeof(char *);
251  if (MemorySize < 0 )
252  {
253  IAS_LOG_ERROR("Input ODL value overflows allocated "
254  "space ");
255  free(p_keyword);
257  }
258  if ( ( ret_code = convert_string(p_MemoryAddr,
259  ValueType,p_word) ) != SUCCESS )
260  {
261  for( i=0;i<*p_Count;i++)
262  free((char *)p_MemoryAddr + i);
263  IAS_LOG_ERROR("Converting %s keyword's value "
264  "%s", p_LabelName, p_word);
265  free(p_keyword);
266  return ret_code;
267  }
268  p_MemoryAddr = (char *)p_MemoryAddr + sizeof(char *);
269  break;
270 
271  default:
272  (void)strncpy( p_MemoryAddr, p_word, MemorySize);
273  IAS_LOG_ERROR("An ODL sequence is of type "
274  "IAS_ODL_ArrayOfString only");
275  free(p_keyword);
276  return ERROR;
277  }
278  *p_Count += 1;
279  p_word = strtok(NULL,",{} \"\n");
280  }
281  }
282  }
283  else
284  {
285  if (ValueType == IAS_ODL_String)
286  {
287  char * p_start = p_kwv;
288  int len = strlen(p_kwv);
289 
290  /* If we are working with a double quoted string, we'll need to
291  remove the double quotes in the string that is passed back.
292  Adjust the length of the string and the starting point for
293  copying, if necessary. */
294  if (p_kwv[0] == '\"')
295  {
296  len -= 2;
297  p_start++;
298  }
299 
300  if (MemorySize < (len + 1))
301  {
302  IAS_LOG_ERROR("Input ODL value overflows allocated space");
303  (void)strncpy(p_MemoryAddr, p_start, MemorySize);
304  ((char *)p_MemoryAddr)[MemorySize-1] = '\0';
305  free(p_keyword);
307  }
308 
309  (void)strncpy(p_MemoryAddr, p_start, len);
310  ((char *)p_MemoryAddr)[len] = '\0';
311  }
312  else
313  {
314  switch(ValueType)
315  {
316  case IAS_ODL_Long:
317  MemorySize -= sizeof(long);
318  break;
319 
320  case IAS_ODL_Int:
321  MemorySize -= sizeof(int);
322  break;
323 
324  case IAS_ODL_Float:
325  MemorySize -= sizeof(float);
326  break;
327 
328  case IAS_ODL_Double:
329  case IAS_ODL_Sci_Not:
330  MemorySize -= sizeof(double);
331  break;
332 
333  default:
334  IAS_LOG_ERROR("Invalid Type specified");
335  free(p_keyword);
336  return ERROR;
337  }
338 
339  if (MemorySize < 0 )
340  {
341  IAS_LOG_ERROR("Input ODL value overflows allocated space ");
342  free(p_keyword);
343  return ERROR;
344  }
345 
346  if ( ( ret_code = convert_string(p_MemoryAddr,ValueType,
347  p_kwv) ) != SUCCESS )
348  {
349  IAS_LOG_ERROR("Converting %s keyword's value %s",
350  p_LabelName,p_kwv);
351  free(p_keyword);
352  return ret_code;
353  }
354  }
355  *p_Count += 1;
356  }
357  free(p_keyword);
358  return SUCCESS;
359 }
360 
361 
362 /******************************************************************************
363 
364 UNIT NAME: convert_string.c
365 
366 PURPOSE: To convert the string to the new type using the memory address given by
367  caller
368 
369 INVOCATION METHOD:
370  status = convert_string(p_destination, parm_type, kvalue)
371 
372 RETURN VALUE:
373  Type = int
374 
375 Value Description
376 --------------- ------------------------------------------------
377 SUCCESS Converted the String in to requested type
378 ERROR Allocation error or unknown parm_type
379 IAS_ODL_INVALID_DATA_TYPE Invalid data type
380 
381 ******************************************************************************/
382 int convert_string
383 (
384  void *p_destination, /* I/O: converted value location */
385  IAS_ODL_TYPE parm_type, /* I; ODL data type */
386  char *kvalue) /* I: word to convert */
387 {
388  char *p_endptr;
389  char *p_a;
390 
391  switch(parm_type)
392  {
393  case IAS_ODL_Long:
394  (*(long *)p_destination) = strtol(kvalue,&p_endptr,10);
395  if (*p_endptr != '\0')
396  {
398  }
399  break;
400 
401  case IAS_ODL_Int:
402  (*(int *)p_destination) = (int)strtol(kvalue,&p_endptr,10);
403  if (*p_endptr != '\0')
404  {
406  }
407  break;
408 
409  case IAS_ODL_Float:
410  (*(float *)p_destination) = (float)strtod(kvalue,&p_endptr);
411  if (*p_endptr != '\0')
412  {
414  }
415  break;
416 
417  case IAS_ODL_Sci_Not:
418  case IAS_ODL_Double:
419  (*(double *)p_destination) = strtod(kvalue,&p_endptr);
420  if (*p_endptr != '\0')
421  {
423  }
424  break;
425 
427  p_a = malloc(strlen(kvalue)+1);
428  if (!p_a)
429  {
430  IAS_LOG_ERROR("Allocating Memory ... ");
431  return ERROR;
432  }
433  (void)strcpy(p_a,kvalue);
434  (*(char **)p_destination) = p_a;
435  break;
436 
437  default:
438  IAS_LOG_ERROR("Invalid Conversion type %s ...", kvalue);
439  return ERROR;
440  }
441 
442  return SUCCESS;
443 }
444 
#define SUCCESS
Definition: ObpgReadGrid.h:15
#define IAS_LOG_ERROR(format,...)
Definition: ias_logging.h:96
OBJDESC * OdlFindObjDesc(OBJDESC *start_object, const char *object_class, const char *keyword_name, char *keyword_value, unsigned long object_position, unsigned short search_scope)
Definition: lablib3.c:536
@ IAS_ODL_Float
Definition: ias_odl.h:34
KEYWORD * OdlFindKwd(OBJDESC *start_object, const char *keyword_name, char *keyword_value, unsigned long keyword_position, unsigned short search_scope)
Definition: lablib3.c:1452
#define NULL
Definition: decode_rs.h:63
int convert_string(void *p_destination, IAS_ODL_TYPE parm_type, char *kvalue)
int ias_odl_get_field(void *p_MemoryAddr, int MemorySize, IAS_ODL_TYPE ValueType, IAS_OBJ_DESC *p_ODLTree, const char *p_ClassName, const char *p_LabelName, int *p_Count)
@ IAS_ODL_Long
Definition: ias_odl.h:33
#define IAS_ODL_NOT_ENOUGH_MEMORY_SUPPLIED
Definition: ias_odl.h:8
char * OdlGetKwdValue(KEYWORD *keyword)
Definition: lablib3.c:2131
integer, parameter double
#define ODL_SET
Definition: lablib3.h:60
@ IAS_ODL_Double
Definition: ias_odl.h:29
@ IAS_ODL_ArrayOfString
Definition: ias_odl.h:30
unsigned short OdlGetKwdValueType(KEYWORD *keyword)
Definition: lablib3.c:2280
char ODLErrorMessage[]
Definition: lablib3.c:3061
PARAM_TYPE_NONE Default value No parameter is buried in the product name name_prefix is case insensitive string compared to the product name PARAM_TYPE_VIS_WAVE The visible wavelength bands from the sensor are buried in the product name The product name is compared by appending and name_suffix ie aph_412_giop where prod_ix will be set to PARAM_TYPE_IR_WAVE same search method as PARAM_TYPE_VIS_WAVE except only wavelength above are looped through but prod_ix is still based ie aph_2_giop for the second and prod_ix set to PARAM_TYPE_INT name_prefix is compared with the beginning of the product name If name_suffix is not empty the it must match the end of the product name The characters right after the prefix are read as an integer and prod_ix is set to that number strncpy(l2prod->name_prefix, "myprod", UNITLEN)
#define ODL_SEQUENCE
Definition: lablib3.h:59
#define IAS_ODL_INVALID_DATA_TYPE
Definition: ias_odl.h:10
#define IAS_ODL_NOT_FOUND
Definition: ias_odl.h:9
int i
Definition: decode_rs.h:71
@ IAS_ODL_Int
Definition: ias_odl.h:32
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
IAS_ODL_TYPE
Definition: ias_odl.h:27
@ IAS_ODL_Sci_Not
Definition: ias_odl.h:35
#define ERROR
Definition: ancil.h:24
@ IAS_ODL_String
Definition: ias_odl.h:31
#define ODL_RECURSIVE_DOWN
Definition: lablib3.h:39