summaryrefslogtreecommitdiff
path: root/plugins/uade2/uade-2.13/src/frontends/common/support.c
blob: 852d4c0e862c6511e0ecf57a59fc3f2f4b6e0a96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>

#include "support.h"
#include "ossupport.h"

/* Zero terminate the current word. Returns -1 is *s == 0 or the next word
   does not exist. Otherwise returns offset to the beginning of next word. */
int skip_and_terminate_word(char *s, int i)
{
	i = skipnws(s, i);
	if (i < 0)
		return -1;

	/* Zero terminate word */
	s[i] = 0;

	i = skipws(s, i + 1);
	if (i < 0)
		return -1;

	return i;
}

char *xbasename(const char *s)
{
  char *t = strrchr(s, (int) '/');
  if (t == NULL) {
	  t = (char *) s;
  } else {
	  t++;
  }
  return t;
}

/*
 * Split a string into 2 whitespace separated fields returned in "key" and
 * "value". If more than 2 fields are found, they are cut off by zero
 * terminating "key" and "value" inside the string. If "value" is not found,
 * *value is set to NULL. If "key" is not found, *key is set to NULL.
 * If something is found, both *key and *value become pointers inside the
 * string s.
 *
 * Return values:
 *  - 0 if neither "key" nor "value" is found
 *  - 1 if only "key" is found
 *  - 2 if both "key" and "value" are found
 */
int get_two_ws_separated_fields(char **key, char **value, char *s)
{
	int i;

	*key = NULL;
	*value = NULL;

	i = skipws(s, 0);   /* Skip initial whitespace */

	if (i < 0)
		return 0;   /* We got nothing */

	*key = s + i;

	i = skip_and_terminate_word(s, i);

	if (i < 0)
		return 1;   /* We got a "key", but not a "value" */

	*value = s + i;

	skip_and_terminate_word(s, i);

	return 2;           /* We got both a "key" and a "value" */
}

/*
 * Skip whitespace characters in string starting from offset i. Returns offset
 * j >= i as the next non-whitespace character offset, or -1 if non-whitespace
 * are not found.
 */
int skipws(const char *s, int i)
{
	while (isspace(s[i]))
		i++;

	if (s[i] == 0)
		return -1;

	return i;
}

/*
 * Skip non-whitespace characters in string starting from offset i. Returns
 * offset j >= i as the next whitespace character offset, or -1 if no
 * whitespace if found.
 */
int skipnws(const char *s, int i)
{
	while (!isspace(s[i]) && s[i] != 0)
		i++;

	if (s[i] == 0)
		return -1;

	return i;
}


/* Split line with respect to white space. */
char **read_and_split_lines(size_t *nitems, size_t *lineno, FILE *f,
			    const char *delim)
{
	char line[UADE_LINESIZE], templine[UADE_LINESIZE];
	char **items = NULL;
	size_t pos;
	char *sp, *s;

	*nitems = 0;

	while (xfgets(line, sizeof line, f) != NULL) {

		if (lineno != NULL)
			(*lineno)++;

		/* Skip, if a comment line */
		if (line[0] == '#')
			continue;

		/* strsep() modifies line that it touches, so we make a copy
		   of it, and then count the number of items on the line */
		strlcpy(templine, line, sizeof(templine));
		sp = templine;
		while ((s = strsep(&sp, delim)) != NULL) {
			if (*s == 0)
				continue;
			(*nitems)++;
		}

		if (*nitems > 0)
			break;
	}

	if (*nitems == 0)
		return NULL;

	if ((items = malloc(sizeof(items[0]) * (*nitems + 1))) == NULL)
		uadeerror("No memory for nws items.\n");

	sp = line;
	pos = 0;
	while ((s = strsep(&sp, delim)) != NULL) {
		if (*s == 0)
			continue;

		if ((items[pos] = strdup(s)) == NULL)
			uadeerror("No memory for an nws item.\n");

		pos++;
	}
	items[pos] = NULL;
	assert(pos == *nitems);

	return items;
}


char *xfgets(char *s, int size, FILE *stream)
{
	char *ret;

	while (1) {
		ret = fgets(s, size, stream);
		if (ret != NULL)
			break;

		if (feof(stream))
			break;
	}

	return ret;
}