/src/libgit2/src/util/strlist.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) the libgit2 contributors. All rights reserved. |
3 | | * |
4 | | * This file is part of libgit2, distributed under the GNU GPL v2 with |
5 | | * a Linking Exception. For full terms see the included COPYING file. |
6 | | */ |
7 | | |
8 | | #include <stdio.h> |
9 | | |
10 | | #include "git2_util.h" |
11 | | #include "vector.h" |
12 | | #include "strlist.h" |
13 | | |
14 | | int git_strlist_copy(char ***out, const char **in, size_t len) |
15 | 0 | { |
16 | 0 | char **dup; |
17 | 0 | size_t i; |
18 | |
|
19 | 0 | dup = git__calloc(len, sizeof(char *)); |
20 | 0 | GIT_ERROR_CHECK_ALLOC(dup); |
21 | | |
22 | 0 | for (i = 0; i < len; i++) { |
23 | 0 | dup[i] = git__strdup(in[i]); |
24 | 0 | GIT_ERROR_CHECK_ALLOC(dup[i]); |
25 | 0 | } |
26 | | |
27 | 0 | *out = dup; |
28 | 0 | return 0; |
29 | 0 | } |
30 | | |
31 | | int git_strlist_copy_with_null(char ***out, const char **in, size_t len) |
32 | 0 | { |
33 | 0 | char **dup; |
34 | 0 | size_t new_len, i; |
35 | |
|
36 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&new_len, len, 1); |
37 | |
|
38 | 0 | dup = git__calloc(new_len, sizeof(char *)); |
39 | 0 | GIT_ERROR_CHECK_ALLOC(dup); |
40 | | |
41 | 0 | for (i = 0; i < len; i++) { |
42 | 0 | dup[i] = git__strdup(in[i]); |
43 | 0 | GIT_ERROR_CHECK_ALLOC(dup[i]); |
44 | 0 | } |
45 | | |
46 | 0 | *out = dup; |
47 | 0 | return 0; |
48 | 0 | } |
49 | | |
50 | | bool git_strlist_contains_prefix( |
51 | | const char **strings, |
52 | | size_t len, |
53 | | const char *str, |
54 | | size_t n) |
55 | 0 | { |
56 | 0 | size_t i; |
57 | |
|
58 | 0 | for (i = 0; i < len; i++) { |
59 | 0 | if (strncmp(strings[i], str, n) == 0) |
60 | 0 | return true; |
61 | 0 | } |
62 | | |
63 | 0 | return false; |
64 | 0 | } |
65 | | |
66 | | bool git_strlist_contains_key( |
67 | | const char **strings, |
68 | | size_t len, |
69 | | const char *key, |
70 | | char delimiter) |
71 | 0 | { |
72 | 0 | const char *c; |
73 | |
|
74 | 0 | for (c = key; *c; c++) { |
75 | 0 | if (*c == delimiter) |
76 | 0 | break; |
77 | 0 | } |
78 | |
|
79 | 0 | return *c ? |
80 | 0 | git_strlist_contains_prefix(strings, len, key, (c - key)) : |
81 | 0 | false; |
82 | 0 | } |
83 | | |
84 | | void git_strlist_free(char **strings, size_t len) |
85 | 1.49k | { |
86 | 1.49k | size_t i; |
87 | | |
88 | 1.49k | if (!strings) |
89 | 1.49k | return; |
90 | | |
91 | 0 | for (i = 0; i < len; i++) |
92 | 0 | git__free(strings[i]); |
93 | |
|
94 | 0 | git__free(strings); |
95 | 0 | } |
96 | | |
97 | | void git_strlist_free_with_null(char **strings) |
98 | 0 | { |
99 | 0 | char **s; |
100 | |
|
101 | 0 | if (!strings) |
102 | 0 | return; |
103 | | |
104 | 0 | for (s = strings; *s; s++) |
105 | 0 | git__free(*s); |
106 | |
|
107 | 0 | git__free(strings); |
108 | 0 | } |