summaryrefslogtreecommitdiffstats
path: root/libopkg/parse_util.c
blob: e01b12427efee895f38a173e4360346864af7e4f (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
/* parse_util.c - the opkg package management system

   Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>

   Steven M. Ayer
   Copyright (C) 2002 Compaq Computer Corporation

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
*/

#include <ctype.h>

#include "opkg_utils.h"
#include "libbb/libbb.h"

#include "parse_util.h"

int
is_field(const char *type, const char *line)
{
	if (!strncmp(line, type, strlen(type)))
		return 1;
	return 0;
}

char *
parse_simple(const char *type, const char *line)
{
	return trim_xstrdup(line + strlen(type) + 1);
}

/*
 * Parse a comma separated string into an array.
 */
char **
parse_list(const char *raw, unsigned int *count, const char sep, int skip_field)
{
	char **depends = NULL;
	const char *start, *end;
	int line_count = 0;

	/* skip past the "Field:" marker */
	if (!skip_field) {
	while (*raw && *raw != ':')
		raw++;
	raw++;
	}

	if (line_is_blank(raw)) {
		*count = line_count;
		return NULL;
	}

	while (*raw) {
		depends = xrealloc(depends, sizeof(char *) * (line_count + 1));
	
		while (isspace(*raw))
			raw++;

		start = raw;
		while (*raw != sep && *raw)
			raw++;
		end = raw;

		while (end > start && isspace(*end))
			end--;

		if (sep == ' ')
			end++;

		depends[line_count] = xstrndup(start, end-start);

        	line_count++;
		if (*raw == sep)
		    raw++;
	}

	*count = line_count;
	return depends;
}