summaryrefslogtreecommitdiff
path: root/src/strarray.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2020-05-29 11:21:55 +0100
committerEdward Thomson <ethomson@edwardthomson.com>2020-06-01 22:50:28 +0100
commita9746b306d32cc2d2bc5d446f6f7ae7c7068ba79 (patch)
tree1b315ffef548780a1e6f20f6414284272f6d8eb3 /src/strarray.c
parent629515a8ab3139e07b43c67a6adcb81909b7175f (diff)
downloadlibgit2-a9746b306d32cc2d2bc5d446f6f7ae7c7068ba79.tar.gz
strarray: move to its own file
Diffstat (limited to 'src/strarray.c')
-rw-r--r--src/strarray.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/strarray.c b/src/strarray.c
new file mode 100644
index 000000000..ebd581483
--- /dev/null
+++ b/src/strarray.c
@@ -0,0 +1,56 @@
+/*
+ * 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 "util.h"
+
+#include "common.h"
+
+int git_strarray_copy(git_strarray *tgt, const git_strarray *src)
+{
+ size_t i;
+
+ assert(tgt && src);
+
+ memset(tgt, 0, sizeof(*tgt));
+
+ if (!src->count)
+ return 0;
+
+ tgt->strings = git__calloc(src->count, sizeof(char *));
+ GIT_ERROR_CHECK_ALLOC(tgt->strings);
+
+ for (i = 0; i < src->count; ++i) {
+ if (!src->strings[i])
+ continue;
+
+ tgt->strings[tgt->count] = git__strdup(src->strings[i]);
+ if (!tgt->strings[tgt->count]) {
+ git_strarray_free(tgt);
+ memset(tgt, 0, sizeof(*tgt));
+ return -1;
+ }
+
+ tgt->count++;
+ }
+
+ return 0;
+}
+
+void git_strarray_free(git_strarray *array)
+{
+ size_t i;
+
+ if (array == NULL)
+ return;
+
+ for (i = 0; i < array->count; ++i)
+ git__free(array->strings[i]);
+
+ git__free(array->strings);
+
+ memset(array, 0, sizeof(*array));
+}