source: trunk/athena/lib/athdir/stringlist.c @ 13786

Revision 13786, 4.4 KB checked in by danw, 24 years ago (diff)
convert to autoconf and build with warnings enabled
Line 
1/* Copyright 1998 by the Massachusetts Institute of Technology.
2 *
3 * Permission to use, copy, modify, and distribute this
4 * software and its documentation for any purpose and without
5 * fee is hereby granted, provided that the above copyright
6 * notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting
8 * documentation, and that the name of M.I.T. not be used in
9 * advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.
11 * M.I.T. makes no representations about the suitability of
12 * this software for any purpose.  It is provided "as is"
13 * without express or implied warranty.
14 */
15
16/* This file implements basic string array functions and structures
17 * to be used by software using the athdir library.
18 */
19
20static const char rcsid[] = "$Id: stringlist.c,v 1.2 1999-10-23 19:28:46 danw Exp $";
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include "stringlist.h"
27
28#define CHUNKSIZE 10
29
30/* athdir__add_string
31 *   Add a copy of "string" to "list," and to the front of "list" if
32 *   "front" is false. If "list" is NULL, initialize it as well.
33 */
34int athdir__add_string(string_list **list, char *string, int front)
35{
36  char *newstring;
37  int index;
38
39  /* To add nothing, do nothing. */
40  if (string == NULL)
41    return 0;
42
43  if (list == NULL)
44    return -1;
45
46 /* Initialize the structure if it's NULL. */
47  if (*list == NULL)
48    {
49      *list = malloc(sizeof(string_list));
50
51      if (*list == NULL)
52        return -1;
53
54      (*list)->strings = malloc(CHUNKSIZE * sizeof(char *));
55      if ((*list)->strings == NULL)
56        {
57          free(*list);
58          *list = NULL;
59          return -1;
60        }
61
62      (*list)->alloced = CHUNKSIZE;
63      (*list)->length = 0;
64      (*list)->strings[0] = NULL;
65    }
66
67  /* Allocate more list space if the list is full. */
68  if ((*list)->alloced == (*list)->length + 1) /* must be NULL terminated */
69    {
70      (*list)->strings = realloc((*list)->strings,
71                              ((*list)->alloced + CHUNKSIZE) * sizeof(char *));
72      if ((*list)->strings == NULL)
73        {
74          free(*list);
75          return -1;
76        }
77      (*list)->alloced += CHUNKSIZE;
78    }
79
80  /* Make our own copy of the string. */
81  newstring = malloc(strlen(string) + 1);
82  if (newstring == NULL)
83    return -1;
84  strcpy(newstring, string);
85
86  /* Add the string to the list. */
87  if (front)
88    {
89      (*list)->length++;
90      for (index = (*list)->length; index > 0; index--)
91        (*list)->strings[index] = (*list)->strings[index - 1];
92      (*list)->strings[0] = newstring;
93    }
94  else
95    {
96      (*list)->strings[(*list)->length] = newstring;
97      (*list)->length++;
98      (*list)->strings[(*list)->length] = NULL;
99    }
100
101  return 0;
102}
103
104/* athdir__make_string_array
105 *   Returns a NULL terminated array of strings (or NULL if the list
106 *   is empty) while freeing the surrounding structure. free_string_array
107 *   the array when finished.
108 */
109char **athdir__make_string_array(string_list **list)
110{
111  char **strings = NULL;
112
113  if (list != NULL && *list != NULL)
114    {
115      if ((*list)->strings[0] != NULL)
116        strings = (*list)->strings;
117      else
118        free((*list)->strings);
119
120      free(*list);
121      *list = NULL;
122    }
123
124  return strings;
125}
126
127/* athdir__free_string_array
128 *   Frees a string array generated by athdir__make_string_array.
129 */
130void athdir__free_string_array(char **array)
131{
132  char **ptr;
133
134  if (array != NULL)
135    {
136      for (ptr = array; *ptr != NULL; ptr++)
137        free(*ptr);
138
139      free(array);
140    }
141}
142
143/* athdir__parse_string
144 *   Parse "string" using "sep" as a separator into separate strings, adding
145 *   adding each substring to the end of "list." Nondestructive to string,
146 *   doesn't point to string.
147 */
148int athdir__parse_string(string_list **list, char *string, char sep)
149{
150  char *ptr, *sep_ptr;
151  char *value;
152  int length;
153
154  if (string != NULL)
155    {
156      /* Allocate a buffer big enough for any substring of string. */
157      value = malloc(strlen(string) + 1);
158      if (value == NULL)
159        return -1;
160
161      ptr = string;
162      while (*ptr != '\0')
163        {
164          /* Figure out the length of this substring. */
165          sep_ptr = strchr(ptr, sep);
166          if (sep_ptr == NULL)
167            length = strlen(ptr);
168          else
169            length = sep_ptr - ptr;
170
171          /* Make a copy to be copied by athdir__add_string. */
172          strncpy(value, ptr, length);
173          value[length] = '\0';
174
175          if (athdir__add_string(list, value, 0))
176            {
177              free(value);
178              return -1;
179            }
180
181          ptr += length;
182          if (*ptr == sep)
183            ptr++;
184        }
185
186      free(value);
187    }
188
189  return 0;
190}
Note: See TracBrowser for help on using the repository browser.