Create parse_util with some of the non-public functions from pkg_parse.c
[project/opkg-lede.git] / libopkg / parse_util.c
1 /* parse_util.c - the opkg package management system
2
3 Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
4
5 Steven M. Ayer
6 Copyright (C) 2002 Compaq Computer Corporation
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17 */
18
19 #include <ctype.h>
20
21 #include "opkg_utils.h"
22 #include "libbb/libbb.h"
23
24 #include "parse_util.h"
25
26 int
27 is_field(const char *type, const char *line)
28 {
29 if (!strncmp(line, type, strlen(type)))
30 return 1;
31 return 0;
32 }
33
34 char *
35 parse_simple(const char *type, const char *line)
36 {
37 return trim_xstrdup(line + strlen(type) + 1);
38 }
39
40 /*
41 * Parse a comma separated string into an array.
42 */
43 char **
44 parse_list(const char *raw, unsigned int *count, const char sep, int skip_field)
45 {
46 char **depends = NULL;
47 const char *start, *end;
48 int line_count = 0;
49
50 /* skip past the "Field:" marker */
51 if (!skip_field) {
52 while (*raw && *raw != ':')
53 raw++;
54 raw++;
55 }
56
57 if (line_is_blank(raw)) {
58 *count = line_count;
59 return NULL;
60 }
61
62 while (*raw) {
63 depends = xrealloc(depends, sizeof(char *) * (line_count + 1));
64
65 while (isspace(*raw))
66 raw++;
67
68 start = raw;
69 while (*raw != sep && *raw)
70 raw++;
71 end = raw;
72
73 while (end > start && isspace(*end))
74 end--;
75
76 if (sep == ' ')
77 end++;
78
79 depends[line_count] = xstrndup(start, end-start);
80
81 line_count++;
82 if (*raw == sep)
83 raw++;
84 }
85
86 *count = line_count;
87 return depends;
88 }