OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
parse_file_name.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <stdint.h>
6 
7 #define isdigit(c) (c >= '0' && c <= '9')
8 #define isalpha(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
9 #define isspace(c) (c == ' ' || c == '\t' || c == '\n')
10 
11 int zs_strparts(char str[], char delim[], int maxparts, char *strvec[]);
12 int zs_strip(char str[]);
13 char *zs_substr(char *longstr, char *smlstr);
14 
23 void parse_file_name(const char* inpath, char* outpath) {
24  char str1[FILENAME_MAX], str2[FILENAME_MAX], envvar[128];
25  char *dollar, *ptr, *trans, *parts[128];
26  int32_t numparts, i, index1, index2;
27 
28  strcpy(outpath, "");
29  /*
30  First translate all environment variable3s in the input string
31  */
32  ptr = (char*) &inpath[0];
33  index1 = 0;
34  strcpy(str1, "");
35 
36  while ((dollar = strchr(ptr, '$'))) {
37  strncat(str1, ptr, (dollar - ptr));
38  index1 += (dollar - ptr);
39  str1[index1] = '\0';
40 
41  index2 = 0;
42  ptr = dollar + 1;
43  while (isalpha(*ptr) || isdigit(*ptr) || *ptr == '_') {
44  envvar[index2++] = *ptr;
45  ptr++;
46  }
47  envvar[index2] = '\0';
48  /*
49  Translate the variable
50  */
51  if ((trans = getenv(envvar)) == NULL) /* Environment variable Not Found */ {
52  strcat(str1, "$");
53  strcat(str1, envvar);
54  index1 += (strlen(envvar) + 1);
55  } else {
56  strcat(str1, trans);
57  index1 += strlen(trans);
58  }
59  }
60  strcat(str1, ptr);
61  /*
62  Now all environment variables are translated.
63  Break up the string into parts separated by /
64  */
65  numparts = zs_strparts(str1, "/", 128, parts);
66 
67  for (i = 0; i < numparts; i++)
68  zs_strip(parts[i]);
69  index1 = 0;
70  strcpy(str2, "");
71 
72  if (strlen(parts[0]) == 0) /* either empty or first char is '/' */ {
73  for (i = 0; i < numparts; i++) {
74  if ((strlen(parts[i]) == 0) || (strcmp(parts[i], ".") == 0))
75  index1++;
76  else
77  break;
78  }
79  if ((i == numparts) && (numparts != 1))
80  strcpy(str2, "/");
81  else if ((i < numparts) && (strcmp(parts[i], "..") == 0))
82  strcpy(str2, "/");
83  } else if (*parts[0] == '~') /* get a home directory */ {
84  printf("\nInterpretation of ~ is no longer supported.\n");
85  exit(1);
86  }
87  else if ((strcmp(parts[0], ".") == 0) || (strcmp(parts[0], "..") == 0)) {
88  getcwd(str2, 257);
89  if (strcmp(parts[0], ".") == 0)
90  index1 = 1;
91  }
92 
93  for (i = index1; i < numparts; i++) {
94  if (strcmp(parts[i], "..") == 0) {
95  if ((ptr = strrchr(str2, '/')) == NULL)
96  strcpy(str2, "");
97  else {
98  if (ptr != &str2[0])
99  *ptr = '\0';
100  else
101  strcpy(str2, "/");
102  }
103  } else if (strlen(parts[i]) && (strcmp(parts[i], ".") != 0)) {
104  if ((i != 0) && (strcmp(str2, "/") != 0))
105  strcat(str2, "/");
106  strcat(str2, parts[i]);
107  }
108  }
109 
110  strcpy(outpath, str2);
111  for (i = 0; i < numparts; i++)
112  free(parts[i]);
113  return;
114 }
115 
116 int zs_strparts(str, delim, maxparts, strvec)
117 char str[];
118 char delim[];
119 int maxparts;
120 char *strvec[];
121 {
122  int i;
123  int len;
124  int numparts = 0;
125  char *sptr1;
126  char *sptr2;
127 
128  sptr1 = str;
129  for (i = 0; i < maxparts; i++) {
130  if (*sptr1 == '\0' || sptr1 == zs_substr(sptr1, delim))
131  sptr2 = sptr1;
132  else
133  sptr2 = zs_substr(sptr1 + 1, delim);
134 
135  if (sptr2 == NULL)
136  len = strlen(sptr1);
137  else
138  len = sptr2 - sptr1;
139 
140  strvec[i] = (char *) malloc(len + 1);
141  strncpy(strvec[i], sptr1, len);
142  *(strvec[i] + len) = '\0';
143 
144  numparts++;
145 
146  if (sptr2 == NULL || *sptr2 == '\0')
147  break;
148  sptr1 = sptr2 + 1;
149  }
150 
151  return (numparts);
152 }
153 
155 char str[];
156 {
157  int32_t i, k, l;
158 
159  i = 0;
160  l = strlen(str);
161  if (isspace(str[i])) {
162  while (isspace(str[i])) i++;
163  // strcpy (&str[0], &str[i]);
164  // Change to memcpy() JMG 02/13/12
165  for (k = i; k <= l; k++)
166  memcpy(&str[k - i], &str[k], 1);
167  }
168 
169  i = strlen(str);
170  while (--i >= 0)
171  if (!isspace(str[i]))
172  break;
173 
174  str[i + 1] = '\0';
175 
176  return (0);
177 }
178 
179 char *zs_substr(longstr, smlstr)
180 char *longstr, *smlstr;
181 {
182  char *pos;
183  int llen,
184  slen;
185 
186  slen = strlen(smlstr);
187  llen = strlen(longstr);
188  for (pos = longstr; *pos != '\0'; pos++) {
189  if (slen > llen)
190  return (NULL);
191  if (!strncmp(pos, smlstr, slen))
192  return (pos);
193  llen--;
194  }
195  return (NULL);
196 }
197 
char * zs_substr(char *longstr, char *smlstr)
#define NULL
Definition: decode_rs.h:63
float32 * pos
Definition: l1_czcs_hdf.c:35
#define isdigit(c)
void parse_file_name(const char *inpath, char *outpath)
int zs_strparts(char str[], char delim[], int maxparts, char *strvec[])
#define isspace(c)
int zs_strip(char str[])
const char * str
Definition: l1c_msi.cpp:35
string outpath
Definition: color_dtdb.py:224
#define isalpha(c)
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)
int i
Definition: decode_rs.h:71
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
int k
Definition: decode_rs.h:73