]> oss.titaniummirror.com Git - repo_shell.git/blobdiff - stra.c
Code for supporting git ACLs. acl_test is useful.
[repo_shell.git] / stra.c
diff --git a/stra.c b/stra.c
new file mode 100644 (file)
index 0000000..4e2f04b
--- /dev/null
+++ b/stra.c
@@ -0,0 +1,100 @@
+/*
+ * Copyright © 2012, Titanium Mirror, Inc.
+ * All Rights Reserved.
+ *
+ * This document is the proprietary and confidential property of
+ * Titanium Mirror, Inc.  All use, distribution, reproduction or re-distribution
+ * is disallowed without the prior express written consent of
+ * Titanium Mirror, Inc.
+ */
+
+/*
+ * A dynamic string array implementation.
+ *
+ * @author R. Steve McKown <rsmckown@gmail.com>
+ */
+
+#include <stdlib.h>
+#include "utility.h"
+#include "stra.h"
+
+void stra_init(stra_t *stra, size_t size)
+{
+  if (!stra)
+    die("stra_destroy: stra NULL reference");
+  stra->items = xmalloc(sizeof(char*) * size);
+  stra->count = 0;
+  stra->size = size;
+}
+
+void stra_destroy(stra_t *stra)
+{
+  unsigned i;
+
+  if (!stra)
+    die("stra_destroy: stra NULL reference");
+  for (i = 0; i < stra->count; i++)
+    free(stra->items[i]);
+  if (stra->size) {
+    free(stra->items);
+    stra->size = 0;
+  }
+}
+
+int stra_add(stra_t *stra, const char *item)
+{
+  if (!stra)
+    die("stra_add: stra NULL reference");
+  if (!item)
+    die("stra_in: item is NULL");
+  if (stra->count > stra->size)
+    die("str_add: bad count=%u, size=%u\n", stra->count, stra->size);
+  else if (stra->count == stra->size) {
+    /* Expand items[] */
+    if (stra->size)
+      stra->size *= 2;
+    else
+      stra->size = 1;
+    stra->items = xrealloc(stra->items, stra->size);
+  }
+  stra->items[stra->count] = xstrdup(item);
+  return stra->count++;
+}
+
+int stra_find(stra_t *stra, const char *item)
+{
+  unsigned i;
+
+  if (!stra)
+    die("stra_in: stra NULL reference");
+  if (!item)
+    die("stra_in: item is NULL");
+  for (i = 0; i < stra->count; i++) {
+    if (!strcmp(stra->items[i], item))
+      return i;
+  }
+  return -1;
+}
+
+size_t stra_count(stra_t *stra)
+{
+  if (!stra)
+    die("stra_count: stra NULL reference");
+  return stra->count;
+}
+
+size_t stra_size(stra_t *stra)
+{
+  if (!stra)
+    die("stra_size: stra NULL reference");
+  return stra->size;
+}
+
+char *stra_item(stra_t *stra, size_t ele)
+{
+  if (!stra)
+    die("stra_item: stra NULL reference");
+  if (ele > stra->count)
+    die("stra_item: ele out of range (%u > %u)", ele, stra->count);
+  return stra->items[ele];
+}