-
Notifications
You must be signed in to change notification settings - Fork 65
/
parameterVector.c
62 lines (51 loc) · 1.38 KB
/
parameterVector.c
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
#include "pharovm/parameters/parameterVector.h"
#include <stdlib.h>
VMErrorCode
vm_parameter_vector_destroy(VMParameterVector *vector)
{
if(!vector) return VM_ERROR_NULL_POINTER;
free(vector->parameters);
vector->parameters = NULL;
vector->count = 0;
return VM_SUCCESS;
}
VMErrorCode
vm_parameter_vector_insert_from(VMParameterVector *vector, uint32_t count, const char **elements)
{
if(!vector) return VM_ERROR_NULL_POINTER;
uint32_t newVectorCount = vector->count + count;
// Add an addition NULL element, for execv and family.
const char **newVectorData = (const char **)calloc(newVectorCount + 1, sizeof(const char *));
if(!newVectorData)
{
return VM_ERROR_OUT_OF_MEMORY;
}
// Copy the old vector parameters.
for(uint32_t i = 0; i < vector->count; ++i)
{
newVectorData[i] = vector->parameters[i];
}
// Copy the new vector parameters.
for(uint32_t i = 0; i < count; ++i)
{
newVectorData[vector->count + i] = elements[i];
}
// Free the old vector parameters
free(vector->parameters); // Free of NULL is a no-op.
vector->count = newVectorCount;
vector->parameters = newVectorData;
return VM_SUCCESS;
}
bool
vm_parameter_vector_has_element(VMParameterVector *vector, const char *parameter)
{
if(!vector) return false;
for(uint32_t i = 0; i < vector->count; ++i)
{
if(!strcmp(vector->parameters[i], parameter))
{
return true;
}
}
return false;
}