From 449b23e18889d916075d37527cb21834976babb2 Mon Sep 17 00:00:00 2001 From: javiplx@gmail.com Date: Thu, 07 Apr 2011 11:17:27 -0400 Subject: Create parse_util with some of the non-public functions from pkg_parse.c git-svn-id: http://opkg.googlecode.com/svn/trunk@611 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358 --- (limited to 'libopkg/parse_util.c') diff --git a/libopkg/parse_util.c b/libopkg/parse_util.c new file mode 100644 index 0000000..e01b124 --- /dev/null +++ b/libopkg/parse_util.c @@ -0,0 +1,88 @@ +/* parse_util.c - the opkg package management system + + Copyright (C) 2009 Ubiq Technologies + + 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 + +#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; +} -- cgit v0.9.1