aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/utils/addressList.c
blob: 4f51a11bfe677197ae572ad0ed6415aacf11a688 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <src/config.h>
#include <stdio.h>

#include <parc/assert/parc_Assert.h>

#include <src/utils/addressList.h>

#include <parc/algol/parc_ArrayList.h>
#include <parc/algol/parc_Buffer.h>
#include <parc/algol/parc_Memory.h>

struct address_list {
  PARCArrayList *listOfAddress;
};

static void _addressListFreeAddress(void **addressVoidPtr) {
  Address **addressPtr = (Address **)addressVoidPtr;
  addressDestroy(addressPtr);
}

AddressList *addressListCreate() {
  AddressList *list = parcMemory_AllocateAndClear(sizeof(AddressList));
  parcAssertNotNull(list, "parcMemory_AllocateAndClear(%zu) returned NULL",
                    sizeof(AddressList));
  list->listOfAddress = parcArrayList_Create(_addressListFreeAddress);
  parcAssertNotNull(list->listOfAddress, "Got null from parcArrayList_Create");

  return list;
}

void addressListDestroy(AddressList **addressListPtr) {
  parcAssertNotNull(addressListPtr,
                    "Parameter must be non-null double pointer");
  parcAssertNotNull(*addressListPtr,
                    "Parameter must dereference to non-null pointer");
  AddressList *list = *addressListPtr;

  parcArrayList_Destroy(&list->listOfAddress);
  parcMemory_Deallocate((void **)&list);
  *addressListPtr = NULL;
}

AddressList *addressListAppend(AddressList *list, Address *address) {
  parcAssertNotNull(list, "Parameter list must be non-null");
  parcAssertNotNull(address, "Parameter address must be non-null");

  parcArrayList_Add(list->listOfAddress, (PARCObject *)address);
  return list;
}

AddressList *addressListCopy(const AddressList *original) {
  parcAssertNotNull(original, "Parameter must be non-null");

  AddressList *copy = addressListCreate();
  for (int i = 0; i < parcArrayList_Size(original->listOfAddress); i++) {
    Address *address = (Address *)parcArrayList_Get(original->listOfAddress, i);
    parcArrayList_Add(copy->listOfAddress, (PARCObject *)addressCopy(address));
  }

  return copy;
}

bool addressListEquals(const AddressList *a, const AddressList *b) {
  parcAssertNotNull(a, "Parameter a must be non-null");
  parcAssertNotNull(b, "Parameter b must be non-null");

  if (a == b) {
    return true;
  }

  if (parcArrayList_Size(a->listOfAddress) !=
      parcArrayList_Size(b->listOfAddress)) {
    return false;
  }

  for (size_t i = 0; i < parcArrayList_Size(a->listOfAddress); i++) {
    const Address *addr_a = (Address *)parcArrayList_Get(a->listOfAddress, i);
    const Address *addr_b = (Address *)parcArrayList_Get(b->listOfAddress, i);
    if (!addressEquals(addr_a, addr_b)) {
      return false;
    }
  }
  return true;
}

size_t addressListLength(const AddressList *list) {
  parcAssertNotNull(list, "Parameter must be non-null");
  return parcArrayList_Size(list->listOfAddress);
}

const Address *addressListGetItem(const AddressList *list, size_t item) {
  parcAssertNotNull(list, "Parameter must be non-null");
  parcAssertTrue(item < addressListLength(list),
                 "Asked for item %zu beyond end of list %zu", item,
                 addressListLength(list));

  return (Address *)parcArrayList_Get(list->listOfAddress, item);
}

char *addressListToString(const AddressList *list) {
  PARCBufferComposer *composer = parcBufferComposer_Create();

  for (size_t i = 0; i < addressListLength(list); i++) {
    char *addressString = addressToString(addressListGetItem(list, i));
    parcBufferComposer_PutString(composer, addressString);
    if (i < (addressListLength(list) - 1)) {
      parcBufferComposer_PutString(composer, " ");
    }
    parcMemory_Deallocate((void **)&addressString);
  }

  PARCBuffer *buffer = parcBufferComposer_ProduceBuffer(composer);
  char *result = parcBuffer_ToString(buffer);
  parcBuffer_Release(&buffer);
  parcBufferComposer_Release(&composer);

  return result;
}