OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
strlist.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004, Remik Ziemlinski <first d0t surname att n0aa d0t g0v>
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2, or (at your option)
7  any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; see the file COPYING.
16  If not, write to the Free Software Foundation,
17  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19 
20 #include "strlist.h"
21 
22 int newstringlist(char*** list, int* n, int size) {
23  int i;
24  if (*list != NULL) return EXIT_FAILURE;
25 
26  *list = XMALLOC(char*, size);
27  for (i = 0; i < size; ++i) {
28  (*list)[i] = NULL;
29  }
30 
31  *n = size;
32 
33  return EXIT_SUCCESS;
34 }
35 
36 int clearstringlist(char** list, int n) {
37  if ((list == NULL) ||
38  (n <= 0)
39  )
40  return 1;
41 
42  for (; --n >= 0;) {
43  if (list[n] == NULL) continue;
44 
45  XFREE(list[n]);
46  list[n] = NULL;
47  }
48 
49  return EXIT_SUCCESS;
50 }
51 
52 /* Input 'list' cannot be NULL. Call 'newstringlist(list,n,256)' beforehand. */
53 void getstringlist(char *optarg, char*** list, int* nitems) {
54  char *p;
55  int i = 0;
56  p = optarg;
57  *nitems = 1;
58 
59  /* find # comma-delimited vars */
60  while (*p++) {
61  if (*p == ',') ++(*nitems);
62  }
63 
64  /*list = XMALLOC(char*, *nitems);*/
65 
66  if (*list == NULL) {
67  return;
68  }
69 
70  /*plist = *list;*/
71 
72  /* copy var names into list */
73  for (p = (char*) strtok(optarg, ",");
74  p != NULL;
75  p = (char*) strtok((char*) NULL, ",")) {
76  (*list)[i] = XMALLOC(char, strlen(p) + 1);
77 
78  if ((*list)[i] == NULL) { /* Abnormal failure. */
79  fprintf(stderr, "ERROR: Failed to allocate memory for string.\n");
80  exit(1);
81  }
82 
83  strcpy((*list)[i], p);
84  ++i;
85  }
86 }
87 
88 void freestringlist(char*** list, int nitems) {
89  if (*list == NULL) return;
90 
91  for (; nitems > 0; --nitems) {
92  XFREE((*list)[nitems - 1]);
93  }
94 
95  XFREE(*list);
96  *list = NULL;
97 }
98 
99 void printstrlist(char** list, int n, FILE* f) {
100  int i;
101  for (i = 0; i < n; ++i)
102  fprintf(f, "%s ", list[i]);
103 
104  fprintf(f, "\n");
105 }
106 
107 int instringlist(char** list, char* str, int nitems) {
108  int i;
109  if ((list == NULL) || (str == NULL)) return 0;
110 
111  /* printf("instringlist entered\n"); */
112 
113  for (i = 0; i < nitems; ++i) {
114  /* printf("instringlist: testing if list[%d] is NULL.\n", i);
115  */
116  if (list[i] == NULL)
117  continue;
118 
119  /*
120  printf("instringlist: list[i] not NULL\n");
121  printf("instringlist: comparing \"%s\" with \"%s\".\n", list[i], str);
122  */
123  if (strcmp(list[i], str) == 0)
124  return 1;
125  }
126  return 0;
127 }
128 
129 int addstringtolist(char** list, char* string, int nitems) {
130  int i;
131  if (string == NULL) return EXIT_FAILED;
132  if (list == NULL) return EXIT_FAILED;
133 
134  /* printf("Adding string to list");*/
135  for (i = 0; i < nitems; ++i) {
136  if (list[i] == NULL) {
137  /* Empty slot, so fill it. */
138  list[i] = XMALLOC(char, strlen(string) + 1);
139  strcpy(list[i], string);
140  /* printf(" to slot %d.\n", i); */
141  break;
142  }
143  }
144 
145  if (i >= nitems)
146  return EXIT_FAILED;
147  else
148  return EXIT_SUCCESS;
149 }
150 
151 /* Returns new pointer with adjusted size. */
152 int appendstringtolist(char*** list, const char* string, int *nitems) {
153  int newsize;
154  char** newlist = NULL;
155  if (string == NULL) return EXIT_FAILED;
156  if (*list == NULL) return EXIT_FAILED;
157 
158  newstringlist(&newlist, &newsize, *nitems + 1);
159  copystrlist(*list, newlist, *nitems, newsize);
160 
161  newlist[newsize - 1] = (char*) malloc(sizeof (char)*(strlen(string) + 1));
162  strcpy(newlist[newsize - 1], string);
163 
164  freestringlist(list, *nitems);
165  *list = newlist;
166  *nitems = newsize;
167 
168  return EXIT_SUCCESS;
169 }
170 
171 int strlistu(char** list1, char** list2, char** listunion, int n1, int n2, int nu) {
172  int i;
173 
174  /* printf("strlistu entered.\n"); */
175 
176  if (listunion == NULL) {
177  return EXIT_FAILED;
178  }
179 
180  for (i = 0; i < n1; ++i) {
181  if (list1[i] == NULL) continue;
182  //printf("strlistu: testing if string \"%s\" already in union.\n", list1[i]);
183  if (!instringlist(listunion, list1[i], nu)) {
184  //printf("strlistu: string \"%s\" not found, trying to add.\n", list1[i]);
185  addstringtolist(listunion, list1[i], nu);
186  }
187  }
188  for (i = 0; i < n2; ++i) {
189  if (list2[i] == NULL) continue;
190  if (!instringlist(listunion, list2[i], nu)) {
191  addstringtolist(listunion, list2[i], nu);
192  }
193  }
194 
195  return EXIT_SUCCESS;
196 }
197 
198 int strlistsd(char** list1, char** list2, char** listdiff, int n1, int n2, int nsd) {
199  int i;
200 
201  if (listdiff == NULL) {
202  return EXIT_FAILED;
203  }
204 
205  if (n1 == 0) {
206  return EXIT_SUCCESS;
207  } else if (n2 == 0) {
208  /* exclude list is empty, return complete list1 */
209  for (i = 0; i < n1; ++i)
210  addstringtolist(listdiff, list1[i], nsd);
211  } else {
212  for (i = 0; i < n1; ++i) {
213  if (!instringlist(list2, list1[i], n2)) {
214  addstringtolist(listdiff, list1[i], nsd);
215  }
216  }
217  }
218  return EXIT_SUCCESS;
219 }
220 
221 int copystrlist(char** listsrc, char** listdst, int nsrc, int ndst) {
222  int i;
223 
224  if (nsrc > ndst)
225  return EXIT_FAILED;
226 
227  for (i = 0; i < ndst; ++i) {
228  XFREE(listdst[i]);
229  listdst[i] = NULL;
230  }
231 
232  for (i = 0; i < nsrc; ++i) {
233  if (listsrc[i] == NULL) continue;
234 
235  listdst[i] = XMALLOC(char, strlen(listsrc[i]) + 1);
236  strcpy(listdst[i], listsrc[i]);
237  }
238 
239  return EXIT_SUCCESS;
240 }
241 
242 int getnumstrlist(char** list, int nlist) {
243  int n = 0;
244  int i;
245 
246  for (i = 0; i < nlist; ++i) {
247  if ((list[i] != NULL) && (strlen(list[i]) > 0))
248  ++n;
249  }
250 
251  return n;
252 }
int addstringtolist(char **list, char *string, int nitems)
Definition: strlist.c:129
int strlistsd(char **list1, char **list2, char **listdiff, int n1, int n2, int nsd)
Definition: strlist.c:198
#define EXIT_SUCCESS
Definition: GEO_basic.h:72
list(APPEND LIBS ${PGSTK_LIBRARIES}) add_executable(atteph_info_modis atteph_info_modis.c) target_link_libraries(atteph_info_modis $
Definition: CMakeLists.txt:7
int clearstringlist(char **list, int n)
Definition: strlist.c:36
#define NULL
Definition: decode_rs.h:63
int getnumstrlist(char **list, int nlist)
Definition: strlist.c:242
int copystrlist(char **listsrc, char **listdst, int nsrc, int ndst)
Definition: strlist.c:221
int newstringlist(char ***list, int *n, int size)
Definition: strlist.c:22
void freestringlist(char ***list, int nitems)
Definition: strlist.c:88
int instringlist(char **list, char *str, int nitems)
Definition: strlist.c:107
double precision function f(R1)
Definition: tmd.lp.f:1454
int appendstringtolist(char ***list, const char *string, int *nitems)
Definition: strlist.c:152
void getstringlist(char *optarg, char ***list, int *nitems)
Definition: strlist.c:53
#define XFREE(stale)
Definition: xmalloc.h:42
const char * str
Definition: l1c_msi.cpp:35
#define XMALLOC(type, num)
Definition: xmalloc.h:36
void printstrlist(char **list, int n, FILE *f)
Definition: strlist.c:99
int strlistu(char **list1, char **list2, char **listunion, int n1, int n2, int nu)
Definition: strlist.c:171
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")
float p[MODELMAX]
Definition: atrem_corl1.h:131
#define EXIT_FAILED
Definition: common.h:86