summaryrefslogtreecommitdiff
path: root/src/util/strlist.c
blob: 8ba476812200cd0f245b09b8bd113583cab74e07 (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
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include <stdio.h>

#include "git2_util.h"
#include "vector.h"
#include "strlist.h"

int git_strlist_copy_with_null(char ***out, const char **in, size_t len)
{
	char **dup;
	size_t new_len, i;

	GIT_ERROR_CHECK_ALLOC_ADD(&new_len, len, 1);

	dup = git__calloc(new_len, sizeof(char *));
	GIT_ERROR_CHECK_ALLOC(dup);

	for (i = 0; i < len; i++) {
		dup[i] = git__strdup(in[i]);
		GIT_ERROR_CHECK_ALLOC(dup[i]);
	}

	*out = dup;
	return 0;
}

bool git_strlist_contains_prefix(
	const char **strings,
	size_t len,
	const char *str,
	size_t n)
{
	size_t i;

	for (i = 0; i < len; i++) {
		if (strncmp(strings[i], str, n) == 0)
			return true;
	}

	return false;
}

bool git_strlist_contains_key(
	const char **strings,
	size_t len,
	const char *key,
	char delimiter)
{
	const char *c;

	for (c = key; *c; c++) {
		if (*c == delimiter)
			break;
	}

	return *c ?
	       git_strlist_contains_prefix(strings, len, key, (c - key)) :
	       false;
}

void git_strlist_free_with_null(char **strings)
{
	char **s;

	if (!strings)
		return;

	for (s = strings; *s; s++)
		git__free(*s);

	git__free(strings);
}