MISR Toolkit  1.5.1
MtkParseFieldname.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkParseFieldname =
4 = =
5 =============================================================================
6 
7  Jet Propulsion Laboratory
8  MISR
9  MISR Toolkit
10 
11  Copyright 2005, California Institute of Technology.
12  ALL RIGHTS RESERVED.
13  U.S. Government Sponsorship acknowledged.
14 
15 ============================================================================*/
16 
17 /* For strcasestr prototype in string.h on Linux64 */
18 #define _GNU_SOURCE
19 
20 #include "MisrUtil.h"
21 #include "MisrError.h"
22 #ifdef __linux__
23 #include <sys/types.h>
24 #endif
25 #ifndef REGEXP_WORKAROUND
26 #include <regex.h>
27 #endif
28 #include <string.h>
29 #include <stdlib.h>
30 #include <limits.h>
31 
37 MTKt_status MtkParseFieldname( const char *fieldname,
38  char **basefieldname,
39  int *ndim,
40  int **dimlist
41 ) {
42  MTKt_status status_code; /* Return status code for error macros */
43 #ifdef REGEXP_WORKAROUND
44  char *endptr = NULL; /* End pointer */
45 #else
46  int reg_status; /* Regular expresssion parser status */
47  regex_t preg; /* Regular expression structure */
48  regmatch_t pm; /* Matched structure */
49 #endif
50  int i = 0; /* Dim index */
51  char c[] = { "[" }; /* Character set */
52  size_t l; /* Length */
53  char buf[MAXSTR]; /* Working buffer */
54  char *bufptr = NULL; /* Pointer to working buffer */
55  char *baseptr = NULL; /* Pointer to basefieldname to return */
56  int *dimptr = NULL; /* Pointer to dimlist to return */
57  char *ptr; /* String pointer */
58 
59  if (fieldname == NULL || basefieldname == NULL ||
60  ndim == NULL || dimlist == NULL)
62 
63  /* --------------------------------------------------- */
64  /* Allocate basefieldname and dimlist output buffers */
65  /* --------------------------------------------------- */
66 
67  baseptr = (char *)calloc(MAXSTR, sizeof(char));
68  if (baseptr == NULL) MTK_ERR_CODE_JUMP(MTK_MALLOC_FAILED);
69 
70  dimptr = (int *)calloc(MAXDIMS, sizeof(int));
71  if (dimptr == NULL) MTK_ERR_CODE_JUMP(MTK_MALLOC_FAILED);
72 
73  /* --------------------------------------------------- */
74  /* Do a regular expression search for extra dimensions */
75  /* --------------------------------------------------- */
76 
77  i = 0;
78  strcpy(buf,fieldname);
79  bufptr = buf;
80 
81 #ifdef REGEXP_WORKAROUND
82  bufptr = strchr(bufptr, '[');
83  while (bufptr != NULL && i < MAXDIMS) {
84  dimptr[i++] = (int)strtol(bufptr + 1, &endptr, 0);
85  if (bufptr + 1 == endptr || *endptr != ']') {
86  i=0;
87  break;
88  }
89  bufptr = strchr(endptr, '[');
90  }
91 #else
92  reg_status = regcomp(&preg, "\\[ *[0-9][0-9]* *\\]", REG_EXTENDED);
93  if (reg_status != 0) MTK_ERR_CODE_JUMP(MTK_FAILURE);
94  reg_status = regexec(&preg, bufptr, 1, &pm, 0);
95  while (reg_status == 0 && i < MAXDIMS) {
96  dimptr[i++] = (int)strtol(bufptr + pm.rm_so + 1, NULL, 0);
97  bufptr += pm.rm_eo;
98  reg_status = regexec(&preg, bufptr, 1, &pm, REG_NOTBOL);
99  }
100  regfree(&preg);
101 #endif
102 
103  *ndim = i;
104  *dimlist = dimptr;
105 
106  /* --------------------------------------------------- */
107  /* Eliminate extra dimensions to isolate basefieldname */
108  /* --------------------------------------------------- */
109 
110  l = strcspn(fieldname, c);
111  strncpy(baseptr, fieldname, l);
112  baseptr[l] = '\0';
113 
114  /* --------------------------------------------------- */
115  /* Reduce virtual/extended fieldname to it's root name */
116  /* --------------------------------------------------- */
117 
118  if (strcasestr(baseptr, "raw ") != NULL)
119  memmove(baseptr, &baseptr[4], strlen(baseptr)-4+1);
120 
121  else if (strcasestr(baseptr, "flag ") != NULL)
122  memmove(baseptr, &baseptr[5], strlen(baseptr)-5+1);
123 
124  else if ((ptr = strcasestr(baseptr, " Radiance")) != NULL)
125  strcpy(ptr, " Radiance/RDQI");
126 
127  else if ((ptr = strcasestr(baseptr, " RDQI")) != NULL)
128  strcpy(ptr, " Radiance/RDQI");
129 
130  else if ((ptr = strcasestr(baseptr, " DN")) != NULL)
131  strcpy(ptr, " Radiance/RDQI");
132 
133  else if ((ptr = strcasestr(baseptr, " Equivalent Reflectance")) != NULL)
134  strcpy(ptr, " Radiance/RDQI");
135 
136  else if ((ptr = strcasestr(baseptr, " Brf")) != NULL)
137  strcpy(ptr, " Radiance/RDQI");
138 
139  *basefieldname = baseptr;
140 
141  return MTK_SUCCESS;
142  ERROR_HANDLE:
143  if (baseptr != NULL) free(baseptr);
144  if (dimptr != NULL) free(dimptr);
145  return status_code;
146 }
MTKt_status MtkParseFieldname(const char *fieldname, char **basefieldname, int *ndim, int **dimlist)
Parses extra dimensions from fieldnames.
regoff_t rm_so
Definition: regex.h:106
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
regoff_t rm_eo
Definition: regex.h:107
LIBTRE_DLL_IMPEXP void regfree(regex_t *preg)
#define REG_EXTENDED
Definition: regex.h:131
#define MAXDIMS
Definition: MisrUtil.h:27
LIBTRE_DLL_IMPEXP int regcomp(regex_t *preg, const char *regex, int cflags)
#define REG_NOTBOL
Definition: regex.h:143
#define MAXSTR
Definition: MisrUtil.h:28
MTKt_status
Definition: MisrError.h:11
HDFFCLIBAPI intf ptr[][4]
Definition: regex.h:100
HDFFCLIBAPI intf * buf
LIBTRE_DLL_IMPEXP int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags)

MISR Toolkit - Copyright © 2005 - 2020 Jet Propulsion Laboratory
Generated on Fri Jun 19 2020 22:49:52