OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
epr_string.c
Go to the documentation of this file.
1 /*
2  * $Id: epr_string.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 <ctype.h>
23 #include <string.h>
24 
25 #include "epr_api.h"
26 #include "epr_core.h"
27 #include "epr_string.h"
28 
29 char* epr_assign_string(char** str_clone, const char* str)
30 {
31  assert(str_clone != NULL);
32  epr_free_string(*str_clone);
33  *str_clone = epr_clone_string(str);
34  return *str_clone;
35 }
36 
37 char* epr_create_string(unsigned int length)
38 {
39  return (char*) calloc(length + 1, sizeof (char));
40 }
41 
42 
43 char* epr_clone_string(const char* str)
44 {
45  char* str_clone = NULL;
46  if (str != NULL) {
47  str_clone = epr_create_string(strlen(str));
48  strcpy(str_clone, str);
49  }
50  return str_clone;
51 }
52 
53 
54 void epr_cut_string(char** sub_str, const char* str, int start, int length)
55 {
56  *sub_str = epr_create_string(length);
57  strncpy(*sub_str, str + start, length);
58  *sub_str[length] = '\0';
59 }
60 
61 
62 char* epr_sub_string(const char* str, int start, int length)
63 {
64  char* str_sub_string = NULL;
65  if (str != NULL) {
66  str_sub_string = epr_create_string(length);
67  strncpy(str_sub_string, str + start, length);
68  str_sub_string[length] = '\0';
69  }
70  return str_sub_string;
71 }
72 
73 
74 /*
75  Function: epr_get_last_err_code
76  Access: private API implementation helper
77  Changelog: 2002/01/05 nf initial version
78  */
91 epr_boolean epr_equal_names(const char* name1, const char* name2)
92 {
93  assert(name1 != NULL);
94  assert(name2 != NULL);
95 
96  return stricmp(name1, name2) == 0;
97 }
98 
99 
100 void epr_free_string(char* str)
101 {
102  if (str == NULL)
103  return;
104  free(str);
105 }
106 
107 /*
108  Function: epr_str_tok
109  Access: private API implementation helper
110  Changelog: 2002/01/10 mp initial version
111  */
122 char* epr_str_tok(const char* str, const char* seps, int* pos)
123 {
124  char* token = NULL;
125  int i, old_pos;
126  int token_len = 0;
127 
128  assert(str != NULL);
129 
130  if (*pos >= (int)strlen(str)) return NULL;
131 
132  old_pos = *pos;
133 
134  for (i = *pos; str[i] != '\0'; i++) {
135  if (strchr(seps, str[i]) != NULL)
136  {
137  token_len = i - *pos;
138  token = epr_create_string(token_len);
139  strncpy(token, str + *pos, token_len);
140  token[token_len] = '\0';
141  *pos = i + 1;
142  return token;
143  }
144  }
145  if (strlen(str) > 0) {
146  if (old_pos == 0) {
147  *pos = i + 1;
148  token = epr_clone_string(str);
149  return token;
150  } else if (old_pos > 0) {
151  token_len = i - old_pos;
152  token = epr_create_string(token_len);
153  strncpy(token, str + *pos, token_len);
154  token[token_len] = '\0';
155  *pos = (int)strlen(str);
156  return token;
157  }
158  }
159  return NULL;
160 }
161 
162 /*
163  Function: epr_str_tok_tok
164  Access: private API implementation helper
165  Changelog: 2002/01/10 mp initial version
166  */
177 char* epr_str_tok_tok(const char* str, const char* seps, const char* exceptions, epr_uint* pos)
178 {
179  char* token = NULL;
180  epr_uint i, old_pos;
181  epr_uint token_len = 0;
182 
183  assert(str != NULL);
184 
185  if (*pos >= (epr_uint)strlen(str)) return NULL;
186 
187  old_pos = *pos;
188 
189  for (i = *pos; str[i] != '\0'; i++) {
190  if (((strchr(seps, str[i]) != NULL)
191  && (i == 0))
192  || ((strchr(seps, str[i]) != NULL)
193  && (i > 0)
194  && (strchr(exceptions, str[i - 1]) == NULL))) {
195  token_len = i - *pos;
196  token = epr_create_string(token_len);
197  strncpy(token, str + *pos, token_len);
198  token[token_len] = '\0';
199  *pos = i + 1;
200  return token;
201  }
202  }
203  if (strlen(str) > 0) {
204  if (old_pos == 0) {
205  *pos = i + 1;
206  token = epr_clone_string(str);
207  return token;
208  } else if (old_pos > 0) {
209  token_len = i - old_pos;
210  token = epr_create_string(token_len);
211  strncpy(token, str + *pos, token_len);
212  token[token_len] = '\0';
213  *pos = (int)strlen(str);
214  return token;
215  }
216  }
217  return NULL;
218 }
219 
220 
222 {
223  int i;
224  char white[] = {" "};
225 
226  for (i = 0; i < (int)strlen(str); i ++) {
227  if (strchr(white, str[i]) == NULL) return i;
228  }
229  return (int)strlen(str);
230 }
231 
232 int epr_find_last_not_white(const char* str)
233 {
234  int i;
235  char white[] = {" "};
236 
237  if ((int)strlen(str) == 0)
238  return -1;
239 
240  for (i = (int)strlen(str) - 1; i >= 0; i --) {
241  if (strchr(white, str[i]) == NULL) return i;
242  }
243  return -1;
244 }
245 
246 
247 char* epr_trim_string(char* str)
248 {
249  int i, i1, i2, n;
250 
251  assert(str != NULL);
252 
253  n = strlen(str);
254  if (n == 0)
255  return str;
256 
257  i1 = -1;
258  for (i = 0; str[i] != '\0'; i++) {
259  if (!isspace(str[i])) {
260  i1 = i;
261  break;
262  }
263  }
264 
265  if (i1 == -1) {
266  str[0] = '\0';
267  return str;
268  }
269 
270  i2 = -1;
271  for (i = n - 1; i >= 0; i--) {
272  if (!isspace(str[i])) {
273  i2 = i;
274  break;
275  }
276  }
277 
278  assert(i1 > -1 && i2 > -1);
279 
280  if (i1 > 0) {
281  int j = 0;
282  for (i = i1; i <= i2; i++) {
283  str[j] = str[i];
284  j++;
285  }
286  }
287 
288  str[i2 - i1 + 1] = '\0';
289 
290  return str;
291 }
292 
293 
295 {
296  int i, i1, n;
297 
298  assert(str != NULL);
299 
300  n = strlen(str);
301  if (n == 0) {
302  return str;
303  }
304 
305  i1 = -1;
306  for (i = n - 1; i >= 0; i--) {
307  if (33 <= str[i] && str[i] <= 126) {
308  i1 = i;
309  break;
310  }
311  }
312 
313  if (i1 == -1) {
314  str[0] = '\0';
315  } else {
316  str[i1+1] = '\0';
317  }
318 
319  return str;
320 }
321 
322 int epr_if_no_letters(const char* str)
323 {
324  int i;
325  char ciffer[] = {"0123456789+- "};
326 
327  for (i = 0; i < (int)strlen(str); i ++) {
328  if (strchr(ciffer, str[i]) == NULL) return 0;
329  }
330  return 1;
331 }
332 
333 
334 int epr_get_positive_int(const char* str)
335 {
336  int i;
337  char ciffer[] = {"0123456789 "};
338 
339  for (i = 0; i < (int)strlen(str); i ++) {
340  if (strchr(ciffer, str[i]) == NULL) return -1;
341  }
342  return atoi(str);
343 }
344 
345 
346 /*'Verdacht': if float-or-double*/
347 int epr_numeral_suspicion(const char* str)
348 {
349  int i;
350  char ciffer[] = {"0123456789+- .eE"};
351 
352  for (i = 0; i < (int)strlen(str); i ++) {
353  if (strchr(ciffer, str[i]) == NULL) return 0;
354  }
355  /*if YES*/
356  return 1;
357 }
358 
359 
360 int epr_if_no_scaling(const char* str)
361 {
362  int result;
363 
364  result = strrchr(str, EPR_IRRELEVANCE_SYMBOL) - str + 1;
365  if (result == 1) return 1;
366  return 0;
367 }
368 
369 
371 {
372  if (*str == NULL) return;
374  *str = NULL;
375 }
376 
377 
378 int stricmp(const char* s1, const char* s2)
379 {
380  int d;
381  assert(s1 != NULL);
382  assert(s2 != NULL);
383  while (*s1 != '\0' && *s2 != '\0') {
384  d = tolower(*s1) - tolower(*s2);
385  if (d != 0) {
386  break;
387  }
388  s1++;
389  s2++;
390  }
391  d = tolower(*s1) - tolower(*s2);
392  return d;
393 }
char * epr_strip_string_r(char *str)
Definition: epr_string.c:294
char * epr_trim_string(char *str)
Definition: epr_string.c:247
int epr_get_positive_int(const char *str)
Definition: epr_string.c:334
int epr_find_first_not_white(const char *str)
Definition: epr_string.c:221
int j
Definition: decode_rs.h:73
unsigned int epr_uint
Definition: epr_api.h:188
void epr_cut_string(char **sub_str, const char *str, int start, int length)
Definition: epr_string.c:54
#define NULL
Definition: decode_rs.h:63
char * epr_sub_string(const char *str, int start, int length)
Definition: epr_string.c:62
float32 * pos
Definition: l1_czcs_hdf.c:35
char * epr_assign_string(char **str_clone, const char *str)
Definition: epr_string.c:29
int epr_boolean
Definition: epr_api.h:185
char * epr_create_string(unsigned int length)
Definition: epr_string.c:37
epr_boolean epr_equal_names(const char *name1, const char *name2)
Definition: epr_string.c:91
int epr_numeral_suspicion(const char *str)
Definition: epr_string.c:347
int epr_find_last_not_white(const char *str)
Definition: epr_string.c:232
char * epr_clone_string(const char *str)
Definition: epr_string.c:43
#define EPR_IRRELEVANCE_SYMBOL
Definition: epr_core.h:57
char * epr_str_tok_tok(const char *str, const char *seps, const char *exceptions, epr_uint *pos)
Definition: epr_string.c:177
#define isspace(c)
const char * str
Definition: l1c_msi.cpp:35
char * epr_str_tok(const char *str, const char *seps, int *pos)
Definition: epr_string.c:122
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 stricmp(const char *s1, const char *s2)
Definition: epr_string.c:378
void epr_free_and_null_string(char **str)
Definition: epr_string.c:370
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")
void epr_free_string(char *str)
Definition: epr_string.c:100
int epr_if_no_letters(const char *str)
Definition: epr_string.c:322
int epr_if_no_scaling(const char *str)
Definition: epr_string.c:360