summaryrefslogtreecommitdiff
path: root/UZ7HOUtils.c
blob: 70d27e25d0d6ad8bddd9252d2fd03704d7b61587 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
Copyright (C) 2019-2020 Andrei Kopanchuk UZ7HO

This file is part of QtSoundModem

QtSoundModem is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

QtSoundModem is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with QtSoundModem.  If not, see http://www.gnu.org/licenses

*/

// UZ7HO Soundmodem Port by John Wiseman G8BPQ
#include "UZ7HOStuff.h"

// TStringlist And String emulation Functions

// Dephi seems to mix starting counts at 0 or 1. I'll try making everything
// base zero. 

// Initialise a list

void CreateStringList(TStringList * List)
{
	List->Count = 0;
	List->Items = 0;
}


int Count(TStringList * List)
{
	return List->Count;
}

string * newString()
{
	// Creates and Initialises a string 

	UCHAR * ptr = malloc(sizeof(string));			// Malloc Data separately so it can be ralloc'ed
	string * New = (string *)ptr;
	New->Length = 0;
	New->AllocatedLength = 256;
	New->Data = malloc(256);

	return New;
}

void initString(string * S)
{
	S->Length = 0;
	S->AllocatedLength = 256;
	S->Data = malloc(256);
}

void initTStringList(TStringList* T)
{
	//string * New = newString();

	T->Count = 0;
	T->Items = NULL;

	//Add(T, New);
}



TStringList * newTStringList()
{
	TStringList * T = (TStringList *) malloc(sizeof(TStringList));
	string * New = newString();

	T->Count = 0;
	T->Items = NULL;

	Add(T, New);

	return T;
}


void freeString(string * Msg)
{
	if (Msg->Data)
		free(Msg->Data);

	free(Msg);
}

string * Strings(TStringList * Q, int Index)
{
	// Gets string at index in stringlist

	if (Index >= Q->Count)
		return NULL;

	return Q->Items[Index];
}

void replaceString(TStringList * Q, int Index, string * item)
{
	// Gets string at index in stringlist

	if (Index >= Q->Count)
		return;

	Q->Items[Index] = item;
}

int Add(TStringList * Q, string * Entry)
{
	Q->Items = realloc(Q->Items,(Q->Count + 1) * sizeof(void *));
	Q->Items[Q->Count++] = Entry;

	return (Q->Count);
}


void mydelete(string * Source, int StartChar, int Count)
{
	 //Description
	//The Delete procedure deletes up to Count characters from the passed parameter Source string starting
	//from position StartChar.

	if (StartChar > Source->Length)
		return;

	int left = Source->Length - StartChar;

	if (Count > left)
		Count = left;

	memmove(&Source->Data[StartChar], &Source->Data[StartChar + Count], left - Count);

	Source->Length -= Count;
}
 

void Delete(TStringList * Q, int Index)
{
	// Remove item at Index and move rest up list
	// Index starts at zero
	
	if (Index >= Q->Count)
		return;

	// We should free it, so user must duplicate msg if needed after delete

	freeString(Q->Items[Index]);
//	free(Q->Items[Index]);

	Q->Count--;

	while (Index < Q->Count)
	{
		Q->Items[Index] = Q->Items[Index + 1];
		Index++;
	}
}

void setlength(string * Msg, int Count)
{
	// Set length, allocating more space if needed

	if (Count > Msg->AllocatedLength)
	{
		Msg->AllocatedLength = Count + 256;
		Msg->Data = realloc(Msg->Data, Msg->AllocatedLength);
	}

	Msg->Length = Count;
}

string * mystringAdd(string * Msg, UCHAR * Chars, int Count, char * FILE, int  LINE)
{
	// Add Chars to string 

	if (Count < 0 || Count > 65536)
	{
		printf("stringAdd Strange Count %d called from %s %d\r\n", Count, FILE, LINE);
	}

	if (Msg->Length + Count > Msg->AllocatedLength)
	{
		Msg->AllocatedLength += Count + 256;
		Msg->Data = realloc(Msg->Data, Msg->AllocatedLength);
	}

	if (Msg->Data == 0)
	{
		printf("realloc failed\r\n");
		exit(1);
	}

	memcpy(&Msg->Data[Msg->Length], Chars, Count);
	Msg->Length += Count;
	
	return Msg;
}

void Clear(TStringList * Q)
{
	int i = 0;

	if (Q->Items == NULL)
		return;

	while (Q->Count)
	{
		freeString(Q->Items[i++]);
		Q->Count--;
	}

	free(Q->Items);

	Q->Items = NULL;
}

// procedure move ( const SourcePointer; var DestinationPointer; CopyCount : Integer ) ;
// Description
// The move procedure is a badly named method of copying a section of memory from one place to another.
 
// CopyCount bytes are copied from storage referenced by SourcePointer and written to DestinationPointer
 
void move(UCHAR * SourcePointer, UCHAR * DestinationPointer, int CopyCount)
{
	memmove(DestinationPointer, SourcePointer, CopyCount);
}

void fmove(float * SourcePointer, float * DestinationPointer, int CopyCount)
{
	memmove(DestinationPointer, SourcePointer, CopyCount);
}



//Description
//The copy function has 2 forms. In the first, it creates a new string from part of an existing string. In the second, it creates a new array from part of an existing array.
 
//1.String copy
 
//The first character of a string has index = 1.

//Up to Count characters are copied from the StartChar of the Source string to the returned string.
//Less than Count characters if the end of the Source string is encountered before Count characters have been copied.


string * copy(string * Source, int StartChar, int Count)
{
	string * NewString = newString();
	int end = StartChar + Count;

	if (end > Source->Length)
		Count = Source->Length - StartChar;

	memcpy(NewString->Data, &Source->Data[StartChar], Count);

	NewString->Length = Count;

	return NewString;
}

// Duplicate from > to

void Assign(TStringList * to, TStringList * from)
{
	int i;

	Clear(to);

	if (from->Count == 0)
		return;

	// Duplicate each item

	for (i = 0; i < from->Count; i++)
	{
		string * new = newString();

		stringAdd(new, from->Items[i]->Data, from->Items[i]->Length);
		Add(to, new);
	}
}

string * duplicateString(string * in)
{
	string * new = newString();

	stringAdd(new, in->Data, in->Length);

	return new;
}


double pila(double x)
{
	//x : = frac(x); The frac function returns the fractional part of a floating point number.

	double whole;
	double rem;

	rem = modf(x, &whole);  // returns fraction, writes whole to whole

	if (rem != rem)
		rem = 0;

	if (rem > 0.5)
		rem = 1 - rem;

	return 2 * rem;
}

boolean compareStrings(string * a, string * b)
{
	if (a->Length == b->Length && memcmp(a->Data, b->Data, a->Length) == 0)
		return TRUE;

	return FALSE;
}

// This looks for a string in a stringlist. Returns index if found, otherwise -1

int  my_indexof(TStringList * l, string * s)
{
	int i;

	for (i = 0; i < l->Count; i++)
	{
		// Need to compare count and data - C doesn't allow struct compare

		if (l->Items[i]->Length == s->Length && memcmp(l->Items[i]->Data, s->Data, s->Length) == 0)
			return i;
	}
	return -1;
}