-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjr_dynarray.cpp
More file actions
133 lines (118 loc) · 4.78 KB
/
Copy pathjr_dynarray.cpp
File metadata and controls
133 lines (118 loc) · 4.78 KB
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
//File: DynamicArrayHelper.cpp
//Created by: S. Schutten, schutten@hotmail.com
// #define DEBUG
#include <stdlib.h>
//#include <wiring.h>
#include "jr.h"
#include "jr_dynarray.h"
/*
DynamicArrayHelper dynamicarrayhelper;
http://playground.arduino.cc/Code/DynamicArrayHelper
void ProtocolBase::SetPulse(unsigned int *& pulsebuffer, byte & pulsebufferlength, byte pulsepos, unsigned int value)
{
->dynamicarrayhelper.SetElementInArray( (void *&)pulsebuffer, &value , pulsepos , pulsebufferlength , sizeof(unsigned int) );
}
*/
bool DynamicArrayHelper::SetArrayLength(void *& array, int newelements , int & elements , byte elementsize)
{
DEBUG_PRINT();
// calculate the size of the buffer to be able to store the number of elements
int newbuffersize = newelements * elementsize;
// calculate the size of the current buffer
int currentbuffersize = elements * elementsize;
// allocate enough memory for "newelements" number of elements
JR_PRINTDECV("cur",currentbuffersize);
JR_PRINTDECV("new",newbuffersize);
void * newarray = malloc ( newbuffersize );
// did memory allocation fail?
if (newarray==0)
{ // yes, failed
JR_PRINTLNF("FALSE - no memory");
return false;
}
// copy the data from the old array, to the new array
for (int idx=0;idx < currentbuffersize ;idx++)
{
((byte*)newarray)[idx] = ((byte *)array)[idx];
JR_PRINTF("m");
}
JR_PRINTF(" array copied:OK");
JR_PRINTF(" ["); JR_PRINTHEX((unsigned long) array);JR_PRINTF("--"); JR_PRINTHEX((unsigned long) newarray);JR_PRINTF("] ");
// free the original array
if (array!=0)
{
free(array);
JR_PRINTF(" orig free:OK");
}
// clear the newly allocated memory space
for (int idx= currentbuffersize;idx < newbuffersize;idx++)
{
((byte *)newarray)[idx] = 0;
JR_PRINTF("c");
}
// Store the number of elements the memory is allocated for
elements = newelements;
// set the array to the newly created array
array = newarray;
// success
JR_PRINTLNF(" TRUE");
return true;
}
bool DynamicArrayHelper::SetElementInArray(void *& array, void * element, int elementindex , int & elements , byte elementsize) {
// is the elementindex outside the range of the current array length?
if (elementindex >= elements)
{ // Yes
// Can the array length be changed?
if (!SetArrayLength( array , elementindex + 1 , elements , elementsize))
{ // no, probably out-of-memory or memory corruption
return false;
}
}
// Is there enough memory allocated for another element?
if ( elements > elementindex )
{ // Yes
int elementstartpos = (elementindex * elementsize);
// Copy-in the element data into the array
for (int idx=0;idx<elementsize;idx++)
{
((byte *)array)[elementstartpos + idx] = ((byte*)element)[idx];
}
} else
{
return false;
}
return true;
}
bool DynamicArrayHelper::AddToArray(void *& array, void * element, int & elements , byte elementsize) {
byte elementindex = elements;
return SetElementInArray( array , element , elementindex , elements , elementsize);
}
bool DynamicArrayHelper::RemoveFromArray(void *& array, int elementindex, int & elements , byte elementsize) {
// calculate the size of the buffer without 1 element
int newbuffersize = (elements-1) * elementsize;
int currentbuffersize = (elements * elementsize);
// allocate memory for the new array
void * newarray = malloc ( newbuffersize );
// did the memory allocation fail?
if (newarray==0)
{ // Yes, failed
return false;
}
// copy the elements in the array before the element-to-be-removed
for (int idx=0;idx<(elementindex*elementsize);idx++)
{
((byte*)newarray)[idx] = ((byte*)array)[idx];
}
// copy the elements in the array after the element-to-be-removed
for (int idx=((elementindex+1)*elementsize);idx < currentbuffersize;idx++)
{
((byte*)newarray)[idx-elementsize] = ((byte*)array)[idx];
}
if (array!=0)
{
free(array);
}
elements--;
array = newarray;
return true;
}